Getting an angle
Blitz3D Forums/Blitz3D Programming/Getting an angle
| ||
Hi, Have variable angle% which can be -ve or +ve and any size. What i want is to return the correct angle between 0 to 359. angle% = angle% mod 360 works fine for +ve angles. angle% = (360 + (angle% mod 360)) mod 360 works for all. Is there a better way (avoiding mod)? Thanks Marg |
| ||
try this: function angle#(ang#) while ang >= 360 ang = ang - 360 wend while ang < 0 ang = ang + 360 wend return ang end function |
| ||
What's wrong with mod? |
| ||
As a side note, generally with angles it's better to work from -180 to +180.... makes things alot easier when using > and < |
| ||
It depends how you want to code. Obviously -180 is not a valid angle technically speaking. |
| ||
personally I don't ususally bother converting angles. They rarely get 'way out of scale' and as long as the maths is sound there should be no problems. I would only convert if I actually needed to display the angle for some reason. |
| ||
well you could just add 180 to the angle... |
| ||
-180 is a valid angle. |
| ||
About as much as 101% is a valid percentage. |
| ||
Any angle -infinity to +infinity as a measurement of rotation is valid mathematically. |
| ||
oh and also, 101% is a completely valid percentage. A percentage is just another way of expressing a fraction, similar to decimal representation. 101% is the same as 1.01 |
| ||
So what's 140% of -768 degrees then smarty pants? - ho ho ;D Great sig by the way. |
| ||
Easy: -1075.2 duh! :O) |
| ||
ps. thanks for the remark - took me at least a day to come up with it :O) |
| ||
angle% = (360 + (angle% mod 360)) mod 360 can be shortened to angle% = (360 + angle%) mod 360 This will remove one mod operation and make it faster. |
| ||
Dreamora, that'll return negative numbers for angles < -360 |