Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rounding compass headings?

Status
Not open for further replies.

RascaPR

Technical User
Oct 27, 2007
26
US
Hello all,

As some of you might know, a compass heading is from 000 to 359 degrees. Now here is my problem.

v1 = 6 // Magnetic Heading
v2 = -10 // Magnetic Variation

In order to find out what is the TRUE HEADING, we need to substract v2 from v1 (6 minus -10).

The result will be -4, but -4 is NOT a heading. The result should be 356.

What would be the formula in C# to accomplish this?
 
I made a mistake about the above formula. It should be that you need to ADD v2 to v1...
 
Well, there may be another way...but one way is to add the variation (yes...add because 6 minus (-10) is actually +16). Then do a test to see if the result is greater than or equal to 0 AND less than 361. If it is, then no conversion needed. If it's less than 0, then add 360 to it. If it's greater than 360, then subtract 360. You'll need to add validation to your inputs to make sure the compass reading is valid (inclusively between 0 and 359).
 
Thanks buddy!!! Your reply got me thinking and it's easier than I thought! lol Here is the simple solution:

int v3 = v2 + v1

if (v3 < 0)
{
v3 = v3 + 360;
}

if (v3 > 359)
{
v3 = v3 - 360;
}

resultv3 = v3.ToString("F0").PadLeft(3, '0');

The above worked wonderfully!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top