BLOG.CSHARPHELPER.COM: Rotate a drawing around a point other than the origin in C#
Rotate a drawing around a point other than the origin in C#
The Matrix class represents a rotation, scaling, translation, or skewing transformation. 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. The DrawArrow method just draws a polygon and isn't shown here.
// 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); e.Graphics.FillEllipse(Brushes.Red, 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); }
Comments