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

发布于 2024-11-30 11:16:46 字数 370 浏览 1 评论 0原文

我有一个方法是绘制一个多边形,然后将该多边形向右旋转 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);

提前致谢。

I have a method which is to draw a polygon, and then rotate that polygon 90 degrees to the right so that its original top point is now pointing towards the right.

This is the code to draw the polygon(triangle) how ever I'm lost on how to rotate this.

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

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

〆一缕阳光ご 2024-12-07 11:16:46

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

您可以使用TransformPoints 方法仅旋转点

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

You can use TransformPoints method of Matrix class to just rotate the points

独闯女儿国 2024-12-07 11:16:46

请参阅这篇内容丰富的维基百科文章,了解旋转矩阵的详细解释。当旋转 90 度时,我们注意到 cos 90 会崩溃为零,产生以下简单变换,其中 x'y' 是旋转后的坐标,< em>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 等人< /em>

See this informative Wikipedia article for a great explanation of rotation matrices. When rotating 90 degrees we note that cos 90 collapses into zero yielding the following simple transformation where x' and y' are your rotated coordinates and x and y are the previous coordinates.

x' = -y
y' = x

Applying this simple replacement on your example yields the following code. I've also used a shorthand collection initializer expression for added readability.

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

I also recommend reading up on linear algebra using for example Anton Rorres, et al.

清风不识月 2024-12-07 11:16:46

如果您旋转每个点,则可以旋转多边形。你还必须找出你的旋转中心O。可能你想使用你的多边形中心作为旋转中心。

You can rotate your polygon if you rotate every point. You also have to find out your rotation center O. Probably you want to use your polygon center as rotation center.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文