线旋转问题

时间:2011-09-09 19:47:41

标签: 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

当你看到线路以某种方式遇到..我真的不明白为什么.. 有什么想法吗?

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

你取Y坐标之间的差值并乘以Sin得到X坐标。这是正确的。

但是,在构造新的Y坐标时,也可以获取Y坐标之间的差异。为此,您应该取X坐标之间的差值并乘以Cos。

E.g。第8行和第12行为新点生成相同的Y坐标,因为p.Y - PitCenter.Y与p2.Y - PitCenter.Y相同,因为p.Y = p2.Y。

有道理吗?

答案 2 :(得分:1)

如果我把它分解得更多,我会发现更容易理解。在我看来,你想要同时做太多事情。我认为这会做你正在寻找的东西(我不确定半径定义是否正是你想要的)但是希望足够清楚你理解我的建议。

float RotDegrees = 90.0;              // Centerline angle for wedge
float width = 10.0;                   // Assume a 10 degree wedge

// Center of view
PointF PitCenter = new PointF(picBoxZoomMap.Width / 2,
                              picBoxZoomMap.Height / 2);

// Determine the angle for the wedges in radians
float theta0 = (RotDegrees - width / 2.0) * Math.PI / 180.0;
float theta1 = (RotDegrees + width / 2.0) * Math.PI / 180.0;

// May need to adjust this to satisfy your needs 
float radius = 100.0;

// Determine the endpoints of the new wedge ... Assumes (0,0) is in the upper
// left corner rather than the lower left (where it belongs ;).  If it's in the
// lower left after all, change the subtraction in the Y components to an
// addition
PointF p0 = new PointF( PitCenter.X + radius * Math.Cos(theta0),
                        PitCenter.Y - radius * Math.Sin(theta0) );

PointF p1 = new PointF( PitCenter.X + radius * Math.Cos(theta1),
                        PitCenter.Y - radius * Math.Sin(theta1));

// Draw the lines
zoomgfx.DrawLine(Pens.Red, PitCenter, p0);
zoomgfx.DrawLine(Pens.Red, PitCenter, p1);