Tricksy little angle limiting thing.
BlitzMax Forums/BlitzMax Programming/Tricksy little angle limiting thing.
| ||
Local largeangle:Int Local limitedangle:Int For largeangle = -900 To 900 Step 10 limitedangle = (360-(-largeangle Mod 360)) Mod 360 Print limitedangle Next Is the above the best way to limit a potentially large angle to return only a 'real' angle in the range 0-360 degrees? I need to reference back to an element in an array based on a collision angle and was a little bit stumped until I thought it through and came up with the above code. I don't know if I went down the wrong path though..? |
| ||
limitedangle=(largeangle+1080) Mod 360 ? |
| ||
limitedangle = largeangle Mod 360 no? *edit* wait. i see that it returns a negative when its negative. however, you could just do: limitedangle = ((largeangle Mod 360) + 360) Mod 360 |
| ||
Whats wrong with Abs()? |
| ||
All you need to do is add a multiple of 360 to the number, to make sure it is always in a positive range, then just wrap it using one Modulo, it will give the right results. Again: LimitedAngle=(LargeAngle+1080) Mod 360 You can't use Abs because it inverts the negative numbers. -10 becomes plus 10 instead of 350 degrees. |
| ||
AngelDaniel: That works OK until LargeAngle goes less than 1080 :-/ I'll stick with the 2x Mod solution for now and see if there is some way of self-limiting largeangle. Thanks though guys, I did need to know I was on the right track at least :D |
| ||
LargeAngle shouldn't go less than 1080 in your example because you said it only ranges from -900 to +900. You can easily fix it, if you're using a bigger range, but just using ANY larger multiple of 360. |