How to resolve the algorithm Angle difference between two bearings step by step in the Java programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Angle difference between two bearings step by step in the Java programming language

Table of Contents

Problem Statement

Finding the angle between two bearings is often confusing.[1]

Find the angle which is the result of the subtraction b2 - b1, where b1 and b2 are the bearings. Input bearings are expressed in the range   -180   to   +180   degrees. The  result  is also expressed in the range   -180   to   +180   degrees.

Compute the angle for the following pairs:

Allow the input bearings to be any (finite) value.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Angle difference between two bearings step by step in the Java programming language

Java Program to Calculate Angle Difference

Explanation:

This Java program defines a class AngleDifference that provides a method getDifference to calculate the difference between two angles b1 and b2. The angles are expected to be within the range of -180 to +180 degrees.

Method getDifference:

  • It calculates the difference between b2 and b1 and stores it in r.
  • The value of r may go beyond the -180 to +180 range.
  • It handles this by applying the modulo operator (%) with 360.0 to wrap the angle within that range.
  • If r is less than -180, it adds 360 to adjust it to the positive range.
  • If r is greater than or equal to 180, it subtracts 360 to adjust it to the negative range.
  • The adjusted r is returned as the angle difference.

Main Method:

  • The main method demonstrates the getDifference method with various input angles:

    • Input in the range of -180 to +180:
      • Each test case shows the calculated angle difference.
    • Input outside the range of -180 to +180:
      • The method still calculates the angle difference, handling the angles beyond the range.

Example Outputs:

Input in -180 to +180 range
25.0
90.0
-10.0
5.0
80.0
100.0
80.0
100.0
-118.1184
-80.7109
Input in wider range
-67135.74195934214
-161949.67717798996
1183.6341015330045
17962.701144411732

Source code in the java programming language

public class AngleDifference {

    public static double getDifference(double b1, double b2) {
        double r = (b2 - b1) % 360.0;
        if (r < -180.0)
            r += 360.0;
        if (r >= 180.0)
            r -= 360.0;
        return r;
    }

    public static void main(String[] args) {
        System.out.println("Input in -180 to +180 range");
        System.out.println(getDifference(20.0, 45.0));
        System.out.println(getDifference(-45.0, 45.0));
        System.out.println(getDifference(-85.0, 90.0));
        System.out.println(getDifference(-95.0, 90.0));
        System.out.println(getDifference(-45.0, 125.0));
        System.out.println(getDifference(-45.0, 145.0));
        System.out.println(getDifference(-45.0, 125.0));
        System.out.println(getDifference(-45.0, 145.0));
        System.out.println(getDifference(29.4803, -88.6381));
        System.out.println(getDifference(-78.3251, -159.036));

        System.out.println("Input in wider range");
        System.out.println(getDifference(-70099.74233810938, 29840.67437876723));
        System.out.println(getDifference(-165313.6666297357, 33693.9894517456));
        System.out.println(getDifference(1174.8380510598456, -154146.66490124757));
        System.out.println(getDifference(60175.77306795546, 42213.07192354373));
    }
}


  

You may also check:How to resolve the algorithm 100 doors step by step in the ABAP programming language
You may also check:How to resolve the algorithm String matching step by step in the AutoIt programming language
You may also check:How to resolve the algorithm Hello world/Graphical step by step in the newLISP programming language
You may also check:How to resolve the algorithm Sorting algorithms/Merge sort step by step in the Java programming language
You may also check:How to resolve the algorithm Jump anywhere step by step in the BQN programming language