【问题标题】:Lines rotation problem线旋转问题
【发布时间】:2011-09-09 19:47:41
【问题描述】:

我有两条线是这样画的:

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 度后的线条..

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

【问题讨论】:

    标签: c# math rotation trigonometry system.drawing


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

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

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

      有道理吗?

      【讨论】:

      • :O 真的没有意义,哈哈,你把我弄糊涂了……那么我怎样才能像第一张照片那样让它们保持相同的距离呢?
      • 更改第 8 行和第 12 行以采用 X 差异而不是 Y 差异。
      【解决方案3】:

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

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

      【讨论】:

      • 我想你忘记了一些东西...... Math.Cos(theta0) 给你喜欢 0.413431 然后如果你做 PitCenter.X+Math.Cos(theta0) 它只会画线毫米远跨度>
      • @Danpe:是的,我一直在编辑。我想我现在或多或少已经完成了。
      猜你喜欢
      • 1970-01-01
      • 2013-09-20
      • 2011-04-10
      • 2011-01-12
      • 1970-01-01
      • 2011-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多