Rotate around a point other than the origin in C#


You can use a Matrix object to rotate, scale, translate, or skew a drawing. Its RotateAt method adds a rotation around a specific point to a Matrix.

The following RotateAroundPoint method returns a new Matrix that represents rotation around a specific point.

// Return a rotation matrix to rotate around a point.
private Matrix RotateAroundPoint(float angle, Point center)
    // Translate the point to the origin.
    Matrix result = new Matrix();
    result.RotateAt(angle, center);
    return result;

The following code shows how the program uses this method to draw an arrow, first in its normal position and then rotated.

// Draw an arrow normally and rotated around a point.
private void picCanvas_Paint(object sender, PaintEventArgs e)
    e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

    // Draw the basic arrow.
    DrawArrow(Pens.Blue, e.Graphics);

    // Draw the point of rotation.
    Point center = new Point(50, 70);
        center.X - 3, center.Y - 3, 6, 6);

    // Rotate 30 degrees around the point.
    e.Graphics.Transform = RotateAroundPoint(30, center);

    // Draw the arrow rotated.
    DrawArrow(Pens.Green, e.Graphics);

The DrawArrow method shown in the following code just draws a polygon that defines an arrow.

// Draw an arrow.
private void DrawArrow(Pen pen, Graphics gr)
    Point[] pts = 
        new Point( 50,  50),
        new Point(150,  50),
        new Point(150,  20),
        new Point(200,  70),
        new Point(150, 120),
        new Point(150,  90),
        new Point( 50,  90)
    gr.DrawPolygon(pen, pts);

Download Example   Follow me on Twitter   RSS feed   Donate

This entry was posted in drawing, graphics and tagged , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *