【问题标题】:C# Rotate a Polygon(Triangle)C# 旋转多边形(三角形)
【发布时间】:2011-08-18 09:29:58
【问题描述】:

我有一个方法是绘制一个多边形,然后将该多边形向右旋转 90 度,使其原来的顶点现在指向右侧。

这是绘制多边形(三角形)的代码,但我不知道如何旋转它。

Point[] points = new Point[3];
points[0] = new Point((int)top, (int)top);
points[1] = new Point((int)top - WIDTH / 2, (int)top + HEIGHT);
points[2] = new Point((int)top + WIDTH / 2, (int)top + HEIGHT);
paper.FillPolygon(normalBrush, points);

提前致谢。

【问题讨论】:

  • 您要旋转多边形本身还是只旋转绘制它?
  • 你应该使用三角函数,基本上它是在正确的时间应用一些sin,cos

标签: c# rotation geometry


【解决方案1】:

http://msdn.microsoft.com/en-us/library/s0s56wcf.aspx#Y609

public void RotateExample(PaintEventArgs e)
{
    Pen myPen = new Pen(Color.Blue, 1);
    Pen myPen2 = new Pen(Color.Red, 1);

    // Draw the rectangle to the screen before applying the transform.
    e.Graphics.DrawRectangle(myPen, 150, 50, 200, 100);

    // Create a matrix and rotate it 45 degrees.
    Matrix myMatrix = new Matrix();
    myMatrix.Rotate(45, MatrixOrder.Append);

    // Draw the rectangle to the screen again after applying the

    // transform.
    e.Graphics.Transform = myMatrix;
    e.Graphics.DrawRectangle(myPen2, 150, 50, 200, 100);
}

您可以使用 Matrix 类的TransformPoints 方法来旋转点

【讨论】:

  • +1 @TransformPoints 链接。但是您发布的当前代码可能没有那么有用,是吗?
  • @duedl0r 如果最终他想旋转绘制它,那么他不需要旋转点;只需创建矩阵并将其分配给图形对象。所以我认为是的,它很有用。
  • 是的,你是对的。但前提是这是唯一的对象,对吧?并且可能还有旋转中心的问题。所以你必须把它移动到(x/y),旋转它,然后把它移回来..
  • 他总是可以在绘制该对象后重置变换,也可以使用 matrix.RotateAt 方法而不是 Rotate 方法指定旋转中心
【解决方案2】:

请参阅this informative Wikipedia article 以获得对旋转矩阵的详细说明。当旋转 90 度时,我们注意到 cos 90 折叠为零,从而产生以下简单变换,其中 x'y' 是您的旋转坐标,x 和 y 是之前的坐标。

x' = -y
y' = x

在您的示例中应用这个简单的替换会产生以下代码。为了增加可读性,我还使用了速记集合初始化表达式。

var points = new[]
{
    new Point(-(int) top, (int) top),
    new Point((int) -(top + HEIGHT), (int) top - WIDTH/2),
    new Point((int) -(top + HEIGHT), (int) top + WIDTH/2)
};

paper.FillPolygon(normalBrush, points);

我还建议阅读线性代数,例如使用 Anton Rorres, et al

【讨论】:

    【解决方案3】:

    rotate every point 可以旋转多边形。您还必须找出您的旋转中心 O。可能您想使用多边形中心作为旋转中心。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-11
      • 2012-11-12
      • 1970-01-01
      • 2020-01-25
      相关资源
      最近更新 更多