开发者

Help Understanding Function

What does the following function perform?

开发者_StackOverflow社区    public static double CleanAngle(double angle) {

        while (angle < 0)
            angle += 2 * System.Math.PI;

        while (angle > 2 * System.Math.PI)
            angle -= 2 * System.Math.PI;

        return angle;
    }

This is how it is used with ATan2. I believe the actually values passed to ATan2 are always positive.

  static void Main(string[] args) {
        int q = 1;
        //'x- and y-coordinates will always be positive values
        //'therefore, do i need to "clean"?
        foreach (Point  oPoint in new Point[] { new Point(8,20), new Point(-8,20), new Point(8,-20), new Point(-8,-20)}) {
            Debug.WriteLine(Math.Atan2(oPoint.Y, oPoint.X), "unclean " + q.ToString());
            Debug.WriteLine(CleanAngle(Math.Atan2(oPoint.Y, oPoint.X)), "cleaned " + q.ToString());                
            q++;
        }

        //'output
        //'unclean 1: 1.19028994968253
        //'cleaned 1: 1.19028994968253
        //'unclean 2: 1.95130270390726
        //'cleaned 2: 1.95130270390726
        //'unclean 3: -1.19028994968253
        //'cleaned 3: 5.09289535749705
        //'unclean 4: -1.95130270390726
        //'cleaned 4: 4.33188260327232

    }
UPDATE

Thank you all for your answers. For every answer there is another question.

Why would they be "normalizing" the angle? Here is the a portion of the code.

        double _theta = Math.ATan2(oEnd.Y - _start.Y, oEnd.X - _start.X);

        Point oCenter = new Point();
        oCenter.X = (int)(_start.X + _distanceTravelled * Math.Cos(_theta));
        oCenter.Y = (int)(_start.Y + _distanceTravelled * Math.Sin(_theta));

        //'move barrage
        this.Left = oCenter.X - this.Width / 2;
        this.Top = oCenter.Y - this.Height / 2;


It norms an angle in radians so that it is in the interval [0..2pi]. 2pi is a full circle and therefore an angle x is equal to the angles x+2pi, x+4pi, x+6pi...


What does the following function perform?

It's a very slow way of forcing an angle (in radians) to be written from 0 to 2π

Why would they be "normalizing" the angle?

Because ATan() returns an angle between -π and π, and they wanted it between 0 and 2π.

If that is the only code that uses CleanAngle, then it's not needed because of the identities

Cos(θ) = Cos(θ-2π)  
Sin(θ) = Sin(θ-2π)


The method converts an angle into the equivalent angle in the range 0..pi*2.

If you only pass positive values into the Atan2 method, you only get back an angle for the first quadrant, i.e. in the range 0..pi/2. Thus, you don't need to normalise the angle.


It normalizes an angle measured in radians into the range 0 to 2 Pi.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜