Does anyone know how I can rotate a point around another in OpenCV?
I am looking for a function like this:
Point2f rotatePoint(Point2f p1, Point2f center, float angle)
{
/* MAGIC */
}
These are the steps needed to rotate a point around another point by an angle alpha:
The standard equation for rotation is:
x' = xcos(alpha) - ysin(alpha)
y' = xsin(alpha) + ycos(alpha)
Let's take the example of Point(15,5) around Point(2,2) by 45 degrees.
Firstly, translate:
v = (15,5) - (2,2) = (13,3)
Now rotate by 45°:
v = (13*cos 45° - 3*sin 45°, 13*sin 45° + 3*cos 45°) = (7.07.., 11.31..)
And finally, translate back:
v = v + (2,2) = (9.07.., 13.31..)
Note: Angles must be specified in radians, so multiply the number of degrees by Pi / 180
To rotate point p1 = (x1, y1)
around p (x0, y0)
by angle a
:
x2 = ((x1 - x0) * cos(a)) - ((y1 - y0) * sin(a)) + x0;
y2 = ((x1 - x0) * sin(a)) + ((y1 - y0) * cos(a)) + y0;
where (x2, y2)
is the new location of point p1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With