C# 中的旋转矩阵绘制出奇怪的形状
我正在尝试围绕圆的圆周平移一个点。
为了测试我的代码,我只需平移该点,并为每个角度增量从中心到平移点绘制一条线。
它看起来应该像一个自行车车轮,但它看起来更像一个铲子。奇怪的是,无论我在数学函数中使用弧度还是度数,看起来都是一样的。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
所以你的数学是正确的。您可以使用该矩阵计算旋转。
但你的编码是错误的!
在使用它来计算
sy
之前,您已经将sx
更改为旋转值!您按顺序对 x 和 y 执行计算,而在矩阵中,它们是同时完成的。所以你的 y 坐标计算不正确,使用剪切坐标系,就像......
你需要这样做:
So your maths is right. You do calculate the rotation using this matrix.
But your coding is wrong!
You have already changed
sx
to the rotated value, before you use it to calculatesy
!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:
我认为这纯粹是一个数学问题。尝试:
I think it's purely a mathematical issue. Try: