wrap angle?
BlitzMax Forums/BlitzMax Beginners Area/wrap angle?
| ||
Just a quickie. Can't see this anywhere, but I wondered if there was an equivalent to the DB Pro (sorry for swearing)- WRAPVALUE function? This takes an angle and returns it as the 0-360 equivalent; so 370 would return 10; -10 would return 350 etc. Should be relatively easy to write a function to do this if not - I just didn't want to re-invent the wheel if it already exists. Cheers |
| ||
mod the angle with 360? ie. wrap = angle mod 360 note: this doesn't work with negative values, so you'll have to check that first.. |
| ||
Thanks Lars - don't think that quite works for negative angles. For those you would need to subtract the result from 360 (I think). so something: wrap = angle mod 360 if wrap < 0 then wrap = 360 - wrap I'll just use a function for this I guess - just wondered if it was already there, hidden away somewhere. |
| ||
Yeah it doesn't work with negative values. And that's so weird - I've never seen a programming language mod function which did, and yet in mathematics -10 mod 360 is 350 :D Maybe someone could explain why mod is never ever properly implemented like that - It would be more practical, imo. |
| ||
and yet in mathematics -10 mod 360 is 350 :D No it isn't. The remainder of an integer division of 10 by 360, is never 350, regardless of whether one of the elements is negative.If you want to be absolutely 100% sure, use trigonometry. |
| ||
Something like this ?Graphics 800,600,0 Local Angle:Int Repeat Cls DrawRect Angle+GraphicsWidth()/2-20, GraphicsHeight()/2-20, 20, 20 If KeyDown( Key_Right ) Angle = ( Angle + 1 ) Mod 160 End If If KeyDown( Key_Left ) Angle = ( Angle - 1 ) Mod -160 End If DrawText Angle, 5, 5 Flip Until KeyHit( Key_Escape ) |
| ||
Here's my little wrapvalue function. Seems to work well enough with negative numbers.Function F_WrapValue#( v# ) v# = v# / 360.0 Return ( v# - Floor# ( v# ) ) * 360.0 End Function |
| ||
cool - thanks for all your tips guys. |
| ||
FlameDuck: I guess it depends on how you interpret the modulo function. I believe it should return the index of the residue class in which the remainder belongs. Wikipedia says we're both right :) |
| ||
Ugly, but works under all circumstances (off the top of my head, could be minor typos:), not limited to angles, which is a weird thing to do if you ask me:Function QWrap#(value#,mix#,max#) ; A name from AmiBlitz days, I guess the BlitzPC equivalent should be FWrap If max-min=0 Return min Else While (value>max) And (value<min) If value<min Then value=value+(max-min) If value>max Then value=value-(max-min) Wend EndIf Return value End Function |
| ||
not exactly what you're looking for but an alternative non the less. You could have your 360 deg angles from 0 to 255 and do a bit operator to wrap it around. your condition would look like this: angle = (angle + 1) & 255 no if's or divisions.. once you hit 256, it auto wraps to 0. very fast. |