C#旋转多边形(三角形)

Dan*_*Dan 4 c# geometry rotation

我有一个绘制多边形的方法,然后将该多边形向右旋转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);
Run Code Online (Sandbox Code Playgroud)

提前致谢.

Muh*_*han 5

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);
}
Run Code Online (Sandbox Code Playgroud)

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


vid*_*ige 3

请参阅这篇内容丰富的维基百科文章,了解旋转矩阵的详细解释。当旋转 90 度时,我们注意到cos 90折叠为零,产生以下简单变换,其中x'y'是旋转后的坐标,xy是之前的坐标。

x' = -y
y' = x
Run Code Online (Sandbox Code Playgroud)

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

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);
Run Code Online (Sandbox Code Playgroud)

我还建议阅读线性代数,例如Anton Rorres等人