Baeldung

Java, Spring and Web Development tutorials

 

Calculate the Difference of Two Angle Measures in Java
2025-10-26 12:26 UTC by Nikhil Bhargav

1. Introduction

In this tutorial, we’ll discuss angles, their measuring units, and three ways of measuring the difference between two angles. Furthermore, we’ll provide Java code snippets for each angle difference calculation.

2.  Measure of an Angle

An angle is a measure of rotation between two intersecting lines or planes. Degrees and radians are the two most common units for angles. We visualize degrees in terms of a full circle, as a circle consists of 360 degrees. On the other hand, a radian is the angle subtended at the center of a circle by an arc with a length equal to the radius. A full circle measures 2π radians.

Java’s Math library uses radians for trigonometric functions (e.g., sin, cos, tan). Therefore, we convert degrees to radians using Math.toRadians().

3. Difference Between Two Angles

Here, we’ll show three ways to calculate the differences between two angles, a and b. First, we’ll learn how to calculate the absolute difference between two angles. Then, we’ll see how to calculate the shortest difference between two angles. Finally, we’ll calculate the sign-preserving shortest difference between two angles.

3.1. Absolute Difference

The first method is absolute difference. We define absolute difference as standard positive difference, ∣a-b∣ , with range in [0, 2π]. For example, the difference between 10 and 300 degrees is ∣10−300∣=290 degrees. Here, we ignore the rotation direction and give only the difference’s full magnitude.

Let’s see the implementation:

public static double absoluteDifference(double angle1, double angle2) { 
    return Math.abs(angle1 - angle2); 
}

3.2. Shortest Difference

Now, we move to the shortest difference. The shortest difference is the smallest angle of rotation from a to reach b. For example, the shortest difference between 10 and 300 degrees is 70. This will always lie in the range [0, 180 degrees (or π radians)].

Let’s see the solution. We’ll use a utility method normalizeAngle() to map each angle to the range [0, 360):

public static double normalizeAngle(double angle) { 
    return (angle % 360 + 360) % 360; 
}

Post-normalization, we calculate the absolute difference between the normalized angles and store it in a variable, diff. Then, we subtract diff from 360 and compare the result to diff, returning the minimum of the two:

public static double shortestDifference(double angle1, double angle2) { 
    double diff = absoluteDifference(normalizeAngle(angle1), normalizeAngle(angle2)); 
    return Math.min(diff, 360 - diff); 
}

3.3. Sign-Preserving Shortest Difference

Last but not least, we have sign-preserving shortest distance. In addition to finding the shortest difference between angles a and b, it also preserves the sign to indicate the direction of rotation (clockwise or counterclockwise). For instance, rotating from 10 to 300 degrees is −70 degrees rotation (clockwise) or a 290 degrees rotation (counter-clockwise).

Let’s check out its implementation:

public static double signedShortestDifference(double angle1, double angle2) { 
    double normalizedAngle1 = normalizeAngle(angle1); 
    double normalizedAngle2 = normalizeAngle(angle2); 
    double diff = normalizedAngle2 - normalizedAngle1; 
    if (diff > 180) { 
        return diff - 360; 
    } 
    else if (diff < -180) { 
        return diff + 360; 
    } 
    else { 
        return diff; 
    } 
}

As the above code shows, the signedShortestDifference() calculates the raw difference and then adjusts it by adding or subtracting 360 if it is outside the (−180, 180) range.

4. Conclusion

In this article, we learned various methods to find the difference between two angles and restrict it to a user-defined range. Calculating the delta (difference between) two angles in Java requires a more profound understanding due to their circular nature. As a generic approach, we first normalize both angles. Then, we calculate the difference (absolute, shortest, or sign-preserving shortest difference).

The post Calculate the Difference of Two Angle Measures in Java first appeared on Baeldung.
       

 

Content mobilized by FeedBlitz RSS Services, the premium FeedBurner alternative.