1

I'm basically trying to find how many degrees apart two points of a compass are. For example if a person is facing 270 and their compass is 280 there are 10 degrees between those 2 points. I'd also like a negative number if it's to the left and positive to the right, relative to the 1st heading.

The problem comes when the to headings are 350 and 020 for example. These two points are 30 degrees apart but would give a result of -330.

Below is an example of my code:

function ConvertToRadians(_var)
{
    return _var * (Math.PI/180);
}
function ConvertToDegrees(_var)
{
    return _var * (180/Math.PI);
}
function GetHeadingDiff(_Heading1, _Heading2)
{   
    return ConvertToDegrees((ConvertToRadians(_Heading2) - ConvertToRadians(_Heading1)));
}

$(document).ready(function (){
    $('#process').click(function (e){
        e.preventDefault();
        var Hdg1 = parseFloat($('#heading1').val());
        var Hdg2 = parseFloat($('#heading2').val());
        $('#results').html(GetHeadingDiff(Hdg1,Hdg2));
    });
});

<input id="heading1" type="text" />
<input id="heading2" type="text" />
<button id="process" type="button">Submit</button>
<div id="results">
</div>

I'm sure there is some simple math function I'm missing, I just can't seem to figure it out.

Here is a jsfiddle link http://jsfiddle.net/dTmPn/3/

  • you want the closer of the two then – Shanimal Jun 24 '14 at 16:55
  • What are you saying about a negative number? I don't understand "if it's to the left" or "if it's to the right". There's 2 points, so one is always to the left and one is always to the right. – Reinstate Monica Cellio Jun 24 '14 at 16:56
  • Archer I think he wants the closer of the two – Shanimal Jun 24 '14 at 16:56
  • Yeah, the closer of the two. It would also be awesome if it was relative to the first heading. for example if the first heading was 280 and the second was 270 it would be -10. I suppose I could always write another method for that though. – Timothy von Hollen Jun 24 '14 at 17:00

3 Answers3

14
function GetHeadingDiff(_Heading1, _Heading2)
{   
    return (_Heading2-_Heading1+540) % 360 - 180;
}

Examples:

GetHeadingDiff( 280, 270 )
-10
GetHeadingDiff( 270, 280 )
10
GetHeadingDiff( 350, 20 )
30
GetHeadingDiff( 20, 350 )
-30
Matt
  • 20,108
  • 1
  • 57
  • 70
2

first of, why are you doing back and forth conversion of radians and degrees? look at this. http://jsfiddle.net/dTmPn/5/

var diff = _Heading2-_Heading1;
if(diff>180) diff-=360;
if(diff<-180) diff+=360;
return diff;

is that enough?

user3754372
  • 177
  • 16
0

im not sure how you expect it to work, (which it doesnt i tried 10-30 and got 19.999996) but this would be the general idea based on your code.

sum1 = ConvertToDegrees((ConvertToRadians(_Heading2) - ConvertToRadians(_Heading1)));
sum2 = ConvertToDegrees((ConvertToRadians(_Heading1) - ConvertToRadians(_Heading2)));

return sum1 < sum2 ? sum1 : sum2;

this does you calculation twice in opposite directions and returns the smaller sum.

i would do it like this:

function GetHeadingDiff(s1, s2)
{  
    r1 = (s1 - s2 + 360 ) % 360;
    r2 = (s2 - s1 + 360 ) % 360;

    return r1<r2?r1:r2;
}

fiddle

Math chiller
  • 4,123
  • 6
  • 28
  • 44