5

I need to select a range from 0 to 359.9 degrees in a SQL database.

My in put is in the form of a center angle and a range. For example center=100 range=50 would give me a range of angle>75 angle<125. However if center=0, then the range would be angle<25 and angle>335.

Thus my current algorithm works like this:

minangle = center-range/2
maxangle = center+range/2
if minangle<0, then minangle += 360
if maxangle>0, then maxangle -=360

Then in my query

if minangle<maxangle, I query angle>minangle AND angle<maxangle
if minangle>maxangle, I query angle>minangle OR angle<maxangle  

This approach seems to be a bit convoluted. Is there a better approach?

eng3
  • 431
  • 3
  • 18
  • 2
    use modulo math. `(angle + range) % 360`. the remainder will be the proper 0->360 angle. – Marc B Nov 20 '14 at 21:43
  • 1
    http://stackoverflow.com/questions/9138790/cant-use-modulus-on-doubles . For floats and doubles use fmod. – Captain Giraffe Nov 20 '14 at 21:54
  • That helps with the +360 -360 logic. I was really hoping to have some way to avoid the (minangle>maxangle) and changing from and to or logic – eng3 Nov 20 '14 at 23:37

1 Answers1

0

Change angle range to 0-720. Always keep minAngle < maxAngle.

if(min > max) max += 360;

minAngle = 50 , maxAngle = 200; => ok

minAngle = 200, maxAngle = 50; => minAngle = 200, maxAngle = 360+50 = 410; => ok

although this increases your complexity.

Ali Mofrad
  • 308
  • 5
  • 21
  • I can't change the angle range, thats the input I'm looking for. And yes, it does increase the complexity. Perhaps there isn't a less complex way. – eng3 Nov 27 '14 at 02:21