views:

217

answers:

3

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.

Thanks in advance,

Casey


alt text

+1  A: 

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.

ruibm
I should have made it more clear that I was looking for some pseudo code at a minimum, thanks anyway
Casey
+2  A: 

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}
Andrew McGregor
You should be able to use `t % 360` instead. I'm not sure if the result is implementation dependent for negative values, but on my computer it works.
Georg
Thanks, much appreciated.
Casey
A: 

Why not simply use the modulo operation?

deg = deg % 360

Hippo
I'm not completely sure how to use this... when I tried it didn't yield the same result as Andrew's pseudocode
Casey
You use it like this... if deg is 370, then it becomes 10 after the modulo operation and assignment. If deg is 1000, it becomes 280. It always takes the remainder after division. Andrew's pseudocode fails in the second case.
Vulcan Eager