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?
Assuming you are usign the Java Graphics2D API, try this code -
where pivot is the point you are rotating around.
Here is a way to rotate any point about any other point in 2D. Note that in 3D this can be used as rotation about z axis, z-coordinate of a point being ingored since it doesn't change. Rotation about x-axis and y-axis in 3D can be also easily implemented.
The code is in JavaScript. The commented lines at the beginning are a test set for the function. They also serve as an example of usage.
You need a 2-d rotation matrix http://en.wikipedia.org/wiki/Rotation_matrix
Your new point will be
because you are rotating clockwise rather than anticlockwise
The easiest approach is to compose three transformations:
When you work this all out, you end up with the following transformation:
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 involvingsin(x)
.Here a version that cares the rotate direction. Right (clockwise) is negative and left (counter clockwise) is positive. You can send a point or a 2d vector and set its primitives in this method (last line) to avoid memory allocation for performance. You may need to replace vector2 and mathutils to libraries you use or to java's built-in point class and you can use math.toradians() instead of mathutils.
Note that this way has more performance than the way you tried in your post. Because you use sqrt that is very costly and in this way converting from degrees to radians managed with a lookup table, if you wonder. And so it has very high performance.
Translate "1" to 0,0
Rotate
x = sin(angle) * r; y = cos(angle) * r;
Translate it back