I'm looking for a function somewhere in Delphi XE2 similar to Inc()
which allows me to add/subtract a number of degrees from a current number of degrees and result in the new degrees. For example, if I have a point currently at 5 degrees around a circle, and I want to subtract 10, I should not get -5 degrees, but rather 355 (360 - 5). Same as adding past 360 - it should go back to 0 when it reaches 360.
Is there anything like this already in Delphi so I don't have to re-write it? Perhaps in the Math
unit?
And it looks like we've gone 3/4 around the circle. So this angle is going to be 3/4 of 360 degrees. 1/4 of 360 degrees is 90, so three of those is going to be 270 degrees.
A degree is a unit of measurement of an angle. One rotation around a circle is equal to 360 degrees. An angle measured in degrees should always include the degree symbol ∘ or the word "degrees" after the number. For example, 90∘=90 90 ∘ = 90 degrees.
To convert a circle measurement to a degree measurement, multiply the angle by the conversion ratio. The angle in degrees is equal to the circles multiplied by 360.
uses
System.SysUtils,Math;
Function WrapAngle( angle : Double) : Double;
Const
modAngle : Double = 360.0;
begin
Result := angle - modAngle*Floor(angle/modAngle);
end;
begin
WriteLn(FloatToStr(WrapAngle(-5)));
WriteLn(FloatToStr(WrapAngle(5-720)));
WriteLn(FloatToStr(WrapAngle(360)));
ReadLn;
end.
Produces result:
355
5
0
Update:
As @Giel found, in XE3 there is a new function DegNormalize()
which does the job. Even about 25% faster. The trick is to replace the Floor()
call with an Int()
instead, and if the result is negative, add modAngle
to the result.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With