Imagine you have two points in 2d space and you need to rotate one of these points by X degrees with the other point acting as a center.
float distX = Math.abs( centerX -point2X ); float distY = Math.abs( centerY -point2Y ); float dist = FloatMath.sqrt( distX*distX + distY*distY );
So far I just got to finding the distance between the two points... any ideas where should I go from that?
The easiest approach is to compose three transformations:
When you work this all out, you end up with the following transformation (where x
is the desired angle of rotation in radians):
newX = centerX + (point2x-centerX)*Math.cos(x) - (point2y-centerY)*Math.sin(x); newY = centerY + (point2x-centerX)*Math.sin(x) + (point2y-centerY)*Math.cos(x);
Note that this makes the assumption that the angle x
is negative for clockwise rotation (the so-called standard or right-hand orientation for the coordinate system). If that's not the case, then you would need to reverse the sign on the terms involving sin(x)
.
You need a 2-d rotation matrix http://en.wikipedia.org/wiki/Rotation_matrix
Your new point will be
newX = centerX + ( cosX * (point2X-centerX) + sinX * (point2Y -centerY)) newY = centerY + ( -sinX * (point2X-centerX) + cosX * (point2Y -centerY))
because you are rotating clockwise rather than anticlockwise
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