帮助理解函数

发布于 2024-08-28 05:21:17 字数 1703 浏览 8 评论 0原文

下面的函数执行什么操作?

    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;
    }

这就是它与 ATan2 一起使用的方式。我相信传递给 ATan2 的实际值始终是正值。

  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

谢谢大家的回答。对于每个答案都有另一个问题。

为什么他们要“标准化”角度?这是代码的一部分。

        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;

What does the following function perform?

    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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

╰◇生如夏花灿烂 2024-09-04 05:21:17

它以 弧度 为单位规范角度,使其位于区间 [0.. 2pi]2pi 是一个完整的圆,因此角度 x 等于角度 x+2pix+4pi ,x+6pi...

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...

§普罗旺斯的薰衣草 2024-09-04 05:21:17

下面的函数执行什么?

这是一种强制将角度(以弧度表示)从 0 写入到 2π 的非常慢的方法

为什么他们要“标准化”角度? >

因为 ATan() 返回 -π 和 π 之间的角度,而他们希望它在 0 到 2π 之间。

如果这是唯一使用 CleanAngle 的代码,那么由于身份原因不需要它

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

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π)
天涯离梦残月幽梦 2024-09-04 05:21:17

该方法将角度转换为 0..pi*2 范围内的等效角度。

如果您只将正值传递给 Atan2 方法,您只能返回第一象限的角度,即 0..pi/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.

乖不如嘢 2024-09-04 05:21:17

它将以弧度测量的角度归一化到 0 到 2 Pi 的范围内。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文