Rotate a point by another point in 2D
Asked Answered
C

4

56

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.

Caseycash answered 24/4, 2009 at 15:55 Comment(0)
B
138

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
Brightman answered 24/4, 2009 at 16:3 Comment(5)
Do you have the 3D version in memory too? :)Prelect
It depends on which library you're using for the trig functions. In C, you need to pass in radians.Brightman
to convert in radians : value * Math.PI / 180;Aught
The answer to the general 3D problem is here.Historiography
This is great code ... but it is not actually in C#!Outgoing
P
7

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.

Prelect answered 24/4, 2009 at 15:58 Comment(0)
U
2

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);
Umbilical answered 2/8, 2014 at 14:49 Comment(0)
P
0

This takes a layout transform command on your image in the WPF, and rotates it the degree you want.

progress_image.LayoutTransform = new RotateTransform(90);
Penley answered 24/9, 2022 at 9:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.