5

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.

user1118321
  • 25,567
  • 4
  • 55
  • 86
Dan
  • 327
  • 1
  • 3
  • 12

3 Answers3

7

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

Muhammad Hasan Khan
  • 34,648
  • 16
  • 88
  • 131
  • +1 @TransformPoints Link. But the current code you posted is probably not that useful, is it? – duedl0r Aug 18 '11 at 12:28
  • @duedl0r If ultimately he wants to draw it rotated then he doesn't need to rotate the points; just create the matrix and assign it to graphics object. So I think yes it is useful. – Muhammad Hasan Khan Aug 18 '11 at 13:50
  • yes, you're right. But only if this is the only object, right? And there is probably also the problem with the rotation center. So you have to move it to (x/y), rotate it, then move it back.. – duedl0r Aug 18 '11 at 13:57
  • He can always reset the transform after drawing that object and rotation center can also be specified b using matrix.RotateAt method instead of Rotate method – Muhammad Hasan Khan Aug 18 '11 at 14:32
3

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.

zero323
  • 322,348
  • 103
  • 959
  • 935
vidstige
  • 12,492
  • 9
  • 66
  • 110
0

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.

Community
  • 1
  • 1
duedl0r
  • 9,289
  • 3
  • 30
  • 45