Rotate a point by another point in 2D

C#MathRotationAngle

C# Problem Overview


I want to know how to work out the new co-ordinates for a point when rotated by an angle relative to another point.

I have a block arrow and want to rotate it by an angle theta relative to a point in the middle of the base of the arrow.

This is required to allow me to draw a polygon between 2 onscreen controls. I can't use and rotate an image.

From what I have considered so far what complicates the matter further is that the origin of a screen is in the top left hand corner.

C# Solutions


Solution 1 - C#

If you rotate point (px, py) around point (ox, oy) by angle theta you'll get:

p'x = cos(theta) * (px-ox) - sin(theta) * (py-oy) + ox
p'y = sin(theta) * (px-ox) + cos(theta) * (py-oy) + oy

Solution 2 - C#

If you are using GDI+ to do that, you can use Transform methods of the Graphics object:

graphics.TranslateTransform(point of origin);
graphics.RotateTransform(rotation angle);

Then draw the actual stuff.

Solution 3 - C#

If you have the System.Windows.Media namespace available, then you can use the built in transformations:

    using System.Windows.Media;
    
    var transform = new RotateTransform() {Angle = angleInDegrees, CenterX = center.X, CenterY = center.Y};
    var transformedPoint = transform.Transform(point);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJamesView Question on Stackoverflow
Solution 1 - C#Sophie AlpertView Answer on Stackoverflow
Solution 2 - C#mmxView Answer on Stackoverflow
Solution 3 - C#thumbmunkeysView Answer on Stackoverflow