Getting the difference between two headers

I have this method for determining the difference between 2 0-360 compass headers.

Although this works to find out how far (as always, with a positive result), I cannot figure out what needs to be done to enter a character in the output file.

Ideally, if the shortest distance from the starting heading to the ending heading is a clockwise movement, I would like error have a positive sign, if the shortest distance between headings includes counterclockwise movement, I would like error have a negative sign.

A few examples of desired I / O

initial - final - error

0 .................... 30 .......... 30

30 .................... 0 .......... -30

360 .................... 1 .......... 1

1 .................... 360 .......... -1

the code:

  /// <summary> /// Calculate the error from a given initial heading to a final heading /// </summary> /// <param name="inital"></param> /// <param name="final"></param> /// <returns></returns> private double GetHeadingError(double initial, double final) { double directionA = final - initial; double directionB = 360 - (final + initial); double error = 0; if (Math.Abs(directionA) < Math.Abs(directionB)) { error = directionA; } else { error = directionB; } return error; } 
+6
c # compass-geolocation 360-degrees
source share
4 answers

Edit: Added check when the difference is exactly 180 degrees. previously it returned 180 or -180 depending on whether the latter was greater or less than the original. I changed it so that it returns a positive 180 in both cases.


So here is my attempt ...

 private static double GetHeadingError(double initial, double final) { if (initial > 360 || initial < 0 || final > 360 || final < 0) { //throw some error } var diff = final - initial; var absDiff = Math.Abs(diff); if (absDiff <= 180) { //Edit 1:27pm return absDiff == 180 ? absDiff : diff; } else if (final > initial) { return absDiff - 360; } else { return 360 - absDiff; } } 
+12
source share

I think your table of desired results is incorrect. Here is my klunky way:

 private double MyGetHeadingError(double initial, double final) { initial += 1000; final += 1000; bool flipped = false; if (initial > final) { double temp; temp = final; final = initial; initial = temp; flipped = true; } double error; if (final - initial > 180) final = final - 360; error = final - initial; if (flipped == true) error = -error; return error; } 
+1
source share

If I understand the question correctly, I think the following code should work:

 private double GetHeadingError(double initial, double final) { if(initial == 360) initial = 0; if(final == 360) final = 0; double clockWise = (final - initial); double counterClockWise = (360 - final + initial); return (Math.Abs(clockWise) <= Math.Abs(counterClockWise)) ? clockWise : -counterClockWise; } 

Basically I look at 360 degrees the same as 0, which I think is fine. This code will give the same results as in the table above. The code does not perform bounds checking; it expects values ​​from 0 to 360.

+1
source share
 Degree_Diff = (MIN(ABS(ENDCOMPASS-STARTCOMPASS),ABS(360-ENDCOMPASS+STARTCOMPASS),ABS(360-STARTCOMPASS+ENDCOMPASS))) 
0
source share

All Articles