c# - 线条旋转问题

标签 c# math rotation trigonometry system.drawing

我有 2 条线是这样画的:

float Alpha = RotDegrees;
PointF PitCenter = new Point(picBoxZoomMap.Width / 2, picBoxZoomMap.Height / 2);
PointF p = new PointF(PitCenter.X - 20, PitCenter.Y - 250);
PointF p2 = new PointF(PitCenter.X + 20, PitCenter.Y - 250);

zoomgfx.DrawLine(Pens.Red, PitCenter, new PointF(
    (float)((p.Y - PitCenter.Y) * Math.Sin(Alpha * Math.PI / 180) + p.X),
    (float)(PitCenter.Y + (p.Y - PitCenter.Y) * Math.Cos(Alpha * Math.PI / 180))));

zoomgfx.DrawLine(Pens.Red, PitCenter, new PointF(
    (float)((p2.Y - PitCenter.Y) * Math.Sin(Alpha * Math.PI / 180) + p2.X),
    (float)(PitCenter.Y + (p2.Y - PitCenter.Y) * Math.Cos(Alpha * Math.PI / 180))));

这是 Alpha = 0 时的行;

enter image description here

这里是旋转 90 度后的线条..

enter image description here

正如你看到的那样,线条以某种方式相遇......我真的不明白为什么...... 有什么想法吗?

最佳答案

您的旋转公式不正确,请看这里 --> Rotate a point by another point in 2D

把你的代码改成这样,你会得到正确的效果:

PointF PitCenter = new Point(picBoxZoomMap.Width / 2, picBoxZoomMap.Height / 2);
PointF p = new PointF(PitCenter.X - 20, PitCenter.Y - 250);
PointF p2 = new PointF(PitCenter.X + 20, PitCenter.Y - 250);

var AlphaRad = RotDegrees * Math.PI / 180;

zoomgfx.DrawLine(Pens.Red, PitCenter, new PointF(
(float)(Math.Cos(AlphaRad) * (p.X - PitCenter.X) - Math.Sin(AlphaRad) * (p.Y - PitCenter.Y) + PitCenter.X),
(float)(Math.Sin(AlphaRad) * (p.X - PitCenter.X) + Math.Cos(AlphaRad) * (p.Y - PitCenter.Y) + PitCenter.Y)));

zoomgfx.DrawLine(Pens.Red, PitCenter, new PointF(
(float)(Math.Cos(AlphaRad) * (p2.X - PitCenter.X) - Math.Sin(AlphaRad) * (p2.Y - PitCenter.Y) + PitCenter.X),
(float)(Math.Sin(AlphaRad) * (p2.X - PitCenter.X) + Math.Cos(AlphaRad) * (p2.Y - PitCenter.Y) + PitCenter.Y)));

关于c# - 线条旋转问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7366708/

相关文章:

c# - 如何使用 LINQ 从 DataTable 中获取唯一列

c# - 在 .Net 中以编程方式更改 PNG/PSD 图像颜色

javascript - 为什么 Math.pow 比缓存的 Math.pow 更快 (var pow = Math.pow)

c - 交替的加号和减号运算符有什么作用?

c# - 根据相对速度和角度找出玩家按下的键

javascript - 在 google maps api v3 上旋转 .gif 图像?

iphone - 要求 iphone 设备方向刷新

c# - 如何在不将上传的 CSV 文件保存到服务器的情况下读取它?

c# - 如何从 session 对象中读取值

algorithm - 除了 BFS 和 DFS,还有什么算法可以用来判断二分性?