Skip to main content
 首页 » 编程设计

c#之线条旋转问题

2024年02月23日11yxwkf

我有 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 时的行;

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

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

请您参考如下方法:

您的旋转公式不正确,请看这里 --> 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)));