Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

q582gmzhi

macrumors newbie
Original poster
Mar 20, 2014
12
0
Hi,

I am a beginner looking for advice on how I would use addition and subtraction when dealing with compass headings / bearings.

For example lets say I am heading 45 degrees on a compass bearing and I want to find the new heading after deducting 90 degrees.

The answer should be 315 degrees but in standard math it returns -45. (45-90).

Does anyone have an example of how to calculate this.

Thanks in advance.

Daz.....
 
I don't know what language you're programming in, but you need to run a conditional argument to get the result you want, like an "if" statement.

Code:
int degrees = 45;
int degreeChange = -90;

degrees += degreeChange;

if (degrees < 0)
    degrees += 360;
else if(degrees >= 360)
    degrees -= 360;

or something functionally equivalent.
 
Hi,

Thanks for the example, I have just tried it and it works.

Many Thanks

Daz....
 
Just note that that approach falls apart if degreeChange can be > 360! You might be better of using the modulus/remainder operation to make sure it maps on to 0-360.

B
 
Not sure what the modulus/remainder operation is (will have to look it up) but the variable that gets generated and passed for the heading is an arc4random (1 to 360) so it should be OK.

Many Thanks

Daz....
 
Yeah, @balamw is correct. I thought about that after I posted but figured that your example "implied" you would constrain your input to angles < 360.

the modulus function (%) performs integer division and returns the remainder only.

if your logic code was:
Code:
degrees = degrees % 360;

With degrees equal to 721, would return 1. If degrees was equal to -36, it would return -36. You would still need to beef up your logic if you wanted to limit your output to between 0 and 359.

Just play around with all your options, see what works, see what approach "clicks" with you. :)
 
Hi,

I looked up the modulus meaning last night, but because I am restricting the variable to 360 figured I did not need it.

I have now hit another problem I have been trying to get around for the last 24 hours and its this bit of logic below.

This logic works correctly 90% of the time but this exact variable example below does not, I believe it must be to do with the variables values but cannot figure out why, it should return "Its a headwind" but does not.

Example Variables:
Code:
WindDirection=30
ScanToPort = 330
CurrentHeading = 60
ScanToStarboard = 150

Code:
If (WindDirection > ScanToPort && WindDirection <=CurrentHeading){
"Its A Headwind";
}Elseif { (WindDirection => CurrentHeading && WindDirection < ScanToStarboard ){
"Its A Headwind";
} Else {
"Its a tailwind"
}

Thanks Daz.....
 
Last edited by a moderator:
For this bit of code I am defining as:

Headwind = Wind Direction value IS within + 90 degrees or -90 degrees of Current Heading. (or wind direction in 180 degree arc in front of you)

Tailwind = Wind Direction value NOT within + 90 degrees or -90 degrees of Current Heading. (or wind direction in 180 degree arc in behind you)

However, having re-wrote the code below using the values instead of variables I can see the problem!!, the first line will not work because: "If (30> 330 && 30<=60){" only one part of this line which is: "&& 30<=60){" is correct and so the code will jump down to the next line which does not work either and so it ends with its a tailwind.

Back to the drawing board!!!

If (30> 330 && 30<=60){
"Its A Headwind";
}Elseif { (30=> 60 && 30< 150){
"Its A Headwind";
} Else {
"Its a tailwind"
}

Thanks

Daz....
 
Last edited:
Well, you pretty much have the pseudo-code there. You just need to translate it into actual code.

But, you also need to deal with the "cross-over" (for lack of a better word) zone. For example, if your heading is 350º and your wind-direction is 10º, you have a headwind. But, if you simply add or subtract 90º from your heading (i.e. 260-440º)
Code:
if (windDirection > (heading - 90) && windDirection < (heading + 90))
your condition will probably not evaluate as hoped. So, you need to do some additional logic.

P.S. If this is Objective-C you're writing, common coding practice is to start variables with a lower-case letter (windDirection is a variable; WindDirection is a class), so I'd adjusted the code above accordingly.
 
Last edited:
Just note that that approach falls apart if degreeChange can be > 360! You might be better of using the modulus/remainder operation to make sure it maps on to 0-360.

B

Indeed, modulo is better. Something like this:


Code:
newHeading = (heading - 90 + 360) % 360

By adding 360, you make sure the result never goes negative, and by taking it modulo 360, if the result is > 360, it "wraps around".
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.