Casey
Casey

Reputation: 1806

Degree range (0-360) wrap around woes

I'm trying to trap the degrees that are X distance (fig: 45) away from a given degree (fig: 15). I'm getting caught up in the 360/0 wrap around. The given degrees are all normalized 0-360. Can someone please show me how to do this? I've included a graphic that illustrates my lacking aptitude.

I swear on Michael Jackson's grave that I scoured Google and StackOverflow for an answer before asking. I realize this is probably a repeat but the answered ones must not be tagged or named appropriately.

alt text

Upvotes: 2

Views: 5877

Answers (3)

TheHippo
TheHippo

Reputation: 63159

Why not simply use the modulo operation?

deg = deg % 360

Upvotes: 3

Andrew McGregor
Andrew McGregor

Reputation: 34662

15+45=60, so you have that side right, but 15-45=-30, so you have to add 360, giving 330.

Basically, you add or subtract 360 each time you go outside [0,360), where I'm using a ) for an open range.

If you want to write the test to colour your markers blue, say h is the heading, o is the offset, and x is the marker we're testing, you're looking for something like, in c-ish pseudocode:

t=h-x
if (t>360) {t-=360}
if (t<0) {t+=360}
if (t<o) {mark blue}

Upvotes: 2

rui
rui

Reputation: 11284

I'm not 100% sure what you want to achieve. I'm assuming what you want is: "For degree X find all degrees that are at most Y degrees away from it give a step of Z degrees between each mark."

That being the case just write a for loop from X-Y to X+Y taking increments of Z. If the current degree value is < 0 then just add 360.

Upvotes: 1

Related Questions