How to resolve the algorithm Angle difference between two bearings step by step in the Ruby 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 Ruby 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 Ruby programming language

The provided Ruby code defines a method, getDifference, that calculates and corrects the angular difference between two bearing angles while ensuring the result is within the range of -180 to 180 degrees. Here's how this code works:

  • The getDifference method takes two arguments, b1 and b2, representing two bearing angles in degrees.

  • It calculates the difference between b2 and b1 using the modulo operator (%) with 360.0 to wrap the result within the range of 0 to 360 degrees.

  • Ruby's modulo operator ensures the sign of the result matches the sign of the divisor (which is positive in this case), so there's no need to consider negative cases.

  • The code checks if the result (r) is greater than or equal to 180.0 degrees. If it is, 360.0 degrees is subtracted from r to bring it within the range of -180 to 180 degrees.

  • Finally, the corrected angular difference (r) is returned.

In the if __FILE__ == $PROGRAM_NAME block, the code includes several test cases to demonstrate the functionality of the getDifference method. It prints the results of calculating the difference between various pairs of bearing angles, both within the range of -180 to 180 degrees and beyond.

Here are some sample inputs and outputs:

  • getDifference(20.0, 45.0): Input angles are within the -180 to 180 range. The result is 25.0 degrees.

  • getDifference(-45.0, 45.0): Input angles are within the -180 to 180 range. The result is 90.0 degrees.

  • getDifference(-85.0, 90.0): Input angles are within the -180 to 180 range. The result is 175.0 degrees.

  • getDifference(-95.0, 90.0): Input angles are within the -180 to 180 range. The result is 185.0 degrees.

  • getDifference(-45.0, 125.0): Input angles are beyond the -180 to 180 range. The result is corrected to 80.0 degrees.

  • getDifference(-45.0, 145.0): Input angles are beyond the -180 to 180 range. The result is corrected to 100.0 degrees.

  • getDifference(29.4803, -88.6381): Input angles are within the -180 to 180 range. The result is 118.1184 degrees (approximately).

  • getDifference(-78.3251, -159.036): Input angles are within the -180 to 180 range. The result is 80.7109 degrees (approximately).

  • getDifference(-70099.74233810938, 29840.67437876723): Input angles are beyond the -180 to 180 range. The result is corrected to 100240.41671734212 degrees.

  • getDifference(-165313.6666297357, 33693.9894517456): Input angles are beyond the -180 to 180 range. The result is corrected to 199007.6560814813 degrees.

  • getDifference(1174.8380510598456, -154146.66490124757): Input angles are beyond the -180 to 180 range. The result is corrected to 155321.50305230742 degrees.

  • getDifference(60175.77306795546, 42213.07192354373): Input angles are beyond the -180 to 180 range. The result is corrected to 17962.70114441173 degrees.

Source code in the ruby programming language

def getDifference(b1, b2)
	r = (b2 - b1) % 360.0
	# Ruby modulus has same sign as divisor, which is positive here,
	# so no need to consider negative case
	if r >= 180.0
		r -= 360.0
	end
	return r
end

if __FILE__ == $PROGRAM_NAME
	puts "Input in -180 to +180 range"
	puts getDifference(20.0, 45.0)
	puts getDifference(-45.0, 45.0)
	puts getDifference(-85.0, 90.0)
	puts getDifference(-95.0, 90.0)
	puts getDifference(-45.0, 125.0)
	puts getDifference(-45.0, 145.0)
	puts getDifference(-45.0, 125.0)
	puts getDifference(-45.0, 145.0)
	puts getDifference(29.4803, -88.6381)
	puts getDifference(-78.3251, -159.036)
 
	puts "Input in wider range"
	puts getDifference(-70099.74233810938, 29840.67437876723)
	puts getDifference(-165313.6666297357, 33693.9894517456)
	puts getDifference(1174.8380510598456, -154146.66490124757)
	puts getDifference(60175.77306795546, 42213.07192354373)
end


  

You may also check:How to resolve the algorithm Anonymous recursion step by step in the Fōrmulæ programming language
You may also check:How to resolve the algorithm Fibonacci word step by step in the Nim programming language
You may also check:How to resolve the algorithm Top rank per group step by step in the Prolog programming language
You may also check:How to resolve the algorithm Mutual recursion step by step in the Sather programming language
You may also check:How to resolve the algorithm Parsing/RPN to infix conversion step by step in the JavaScript programming language