C# 中的旋转矩阵绘制出奇怪的形状

发布于 2024-12-08 08:45:30 字数 657 浏览 0 评论 0原文

我正在尝试围绕圆的圆周平移一个点。

为了测试我的代码,我只需平移该点,并为每个角度增量从中心到平移点绘制一条线。

它看起来应该像一个自行车车轮,但它看起来更像一个铲子。奇怪的是,无论我在数学函数中使用弧度还是度数,看起来都是一样的。

for (double degrees = 0; degrees <= 360; degrees += 1)
{
    double radians = Math.PI * degrees / 180.0;
    Console.Write("degrees = " + degrees + " radians = " + radians);
    double sx = 0.0;
    double sy = -100.0;
    sx = ((sx * Math.Cos(radians)) + (sy * Math.Sin(radians)));
    sy = (-(sx * Math.Sin(radians)) + (sy * Math.Cos(radians)));

    Console.WriteLine(", sx = " + sx + ", sy = " + sy);

    g.DrawLine(new Pen(Brushes.GhostWhite), 200, 200, (int)sx+200, (int)sy+200);
}

I'm trying to translate a point around the circumference of a circle.

To test my code I just translate the point and draw a line from the centre to the translated point for each angle increment.

This should look like a bike wheel, however it looks more like a spade. Oddly, it appears the same whether I used radians or degrees in the Math functions.

for (double degrees = 0; degrees <= 360; degrees += 1)
{
    double radians = Math.PI * degrees / 180.0;
    Console.Write("degrees = " + degrees + " radians = " + radians);
    double sx = 0.0;
    double sy = -100.0;
    sx = ((sx * Math.Cos(radians)) + (sy * Math.Sin(radians)));
    sy = (-(sx * Math.Sin(radians)) + (sy * Math.Cos(radians)));

    Console.WriteLine(", sx = " + sx + ", sy = " + sy);

    g.DrawLine(new Pen(Brushes.GhostWhite), 200, 200, (int)sx+200, (int)sy+200);
}

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

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

发布评论

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

评论(3

往日情怀 2024-12-15 08:45:30

所以你的数学是正确的。您可以使用该矩阵计算旋转。

[cos(r)  sin(r)]
[-sin(r) cos(r)]

但你的编码是错误的!

在使用它来计算 sy 之前,您已经将 sx 更改为旋转值!

您按顺序对 x 和 y 执行计算,而在矩阵中,它们是同时完成的。所以你的 y 坐标计算不正确,使用剪切坐标系,就像......

你需要这样做:

double sx = 0.0;
double sy = -100.0;
double nsx = ((sx * Math.Cos(radians)) + (sy * Math.Sin(radians)));
double nsy = (-(sx * Math.Sin(radians)) + (sy * Math.Cos(radians)));

g.DrawLine(new Pen(Brushes.GhostWhite), 200, 200, (int)nsx+200, (int)nsy+200);

So your maths is right. You do calculate the rotation using this matrix.

[cos(r)  sin(r)]
[-sin(r) cos(r)]

But your coding is wrong!

You have already changed sx to the rotated value, before you use it to calculate sy!

You are performing calculations on x and y sequentially, whereas in a matrix, they are done simultaneously. So your y coordinate is computed incorrectly, using a sheared coordinate system, as it were...

You need to do this:

double sx = 0.0;
double sy = -100.0;
double nsx = ((sx * Math.Cos(radians)) + (sy * Math.Sin(radians)));
double nsy = (-(sx * Math.Sin(radians)) + (sy * Math.Cos(radians)));

g.DrawLine(new Pen(Brushes.GhostWhite), 200, 200, (int)nsx+200, (int)nsy+200);
驱逐舰岛风号 2024-12-15 08:45:30
    double radius = 200;

    for (double degrees = 0; degrees <= 360; degrees += 1)
    {
        double radians = Math.PI * degrees / 180.0;

        int sx = (int) (200 +  radius * Math.Cos(radians));
        int sy = (int) (200 +  radius * Math.Sin(radians));

        g.DrawLine(new Pen(Brushes.GhostWhite), 200, 200, sx, sy);
    }
    double radius = 200;

    for (double degrees = 0; degrees <= 360; degrees += 1)
    {
        double radians = Math.PI * degrees / 180.0;

        int sx = (int) (200 +  radius * Math.Cos(radians));
        int sy = (int) (200 +  radius * Math.Sin(radians));

        g.DrawLine(new Pen(Brushes.GhostWhite), 200, 200, sx, sy);
    }
霞映澄塘 2024-12-15 08:45:30

我认为这纯粹是一个数学问题。尝试:

double radius = 100;
double sx = radius * Math.Cos(radians);
double sy = radius * Math.Sin(radians);

I think it's purely a mathematical issue. Try:

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