Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a function to shift degrees of a circle past 0?

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?

like image 952
Jerry Dodge Avatar asked Nov 10 '12 23:11

Jerry Dodge


People also ask

How many degrees are in an angle that turns through 3/4 of a circle?

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.

What is a degree in unit circle?

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.

How do you find the degree of a circle?

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.


1 Answers

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.

like image 98
LU RD Avatar answered Sep 20 '22 01:09

LU RD