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

时间:2011-10-04 23:33:33

标签: c# .net graphics rotation

我正试图围绕圆周转换一个点。

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

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

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

3 个答案:

答案 0 :(得分:3)

所以数学是对的。您可以使用此矩阵计算旋转。

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

但你的编码错了!

在您使用sx计算sy之前,您已将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); 更改为旋转值!

您正在顺序执行x和y的计算,而在矩阵中,它们是同时完成的。因此,使用剪切坐标系统错误地计算y坐标,因为它是......

你需要这样做:

{{1}}

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

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