Rotate around non-origin pivot point?

S

Si

I use the following method to rotate a point around the origin.

public void rotate(float a) {
a = (float)Math.toRadians((double)a);
float xtemp;
xtemp = (x * (float)Math.cos(a)) - (y * (float)Math.sin(a));
y = (x * (float)Math.sin(a)) + (y * (float)Math.cos(a));
x = xtemp;
}

However I want to rotate the point around a given pivot point e.g. 200,200.

I can't work out how to modify the above code so I can take in two x and y
co-ords to define the pivot point. Any ideas?
 
B

Barry

Si said:
I use the following method to rotate a point around the origin.

public void rotate(float a) {
a = (float)Math.toRadians((double)a);
float xtemp;
xtemp = (x * (float)Math.cos(a)) - (y * (float)Math.sin(a));
y = (x * (float)Math.sin(a)) + (y * (float)Math.cos(a));
x = xtemp;
}

However I want to rotate the point around a given pivot point e.g. 200,200.

I can't work out how to modify the above code so I can take in two x and y
co-ords to define the pivot point. Any ideas?

Simplest way is subtract 200 from x and y, do the rotate then add back
the 200s.
 
Z

Zerex71

Hi there,

This is mathematically a two-step process:
1. Translate point P over to new coordinates (P').
2. Rotate the specified amounts in three rotation directions (yaw,
pitch, and roll).

You could code up a quaternion class to do that if you like. I am
going to be developing one for some software I am working on. Look up
quaternions (www.mathworld.wolfram.com) and look at how to implement
their operations to do exactly what you want.

Mike
 
T

Thomas Weidenfeller

Si said:
I use the following method to rotate a point around the origin.

public void rotate(float a) {
a = (float)Math.toRadians((double)a);
float xtemp;
xtemp = (x * (float)Math.cos(a)) - (y * (float)Math.sin(a));
y = (x * (float)Math.sin(a)) + (y * (float)Math.cos(a));
x = xtemp;
}

However I want to rotate the point around a given pivot point e.g. 200,200.

I can't work out how to modify the above code so I can take in two x and y
co-ords to define the pivot point. Any ideas?

Others have explained the procedure to do it "manually". If you want to
do it the Java way, you could use java.awt.geom.AffineTransform. For the
above problem one would construct an AffineTransform with the
getRotateInstance(double, double, double) method
<http://java.sun.com/j2se/1.5.0/docs...tml#getRotateInstance(double, double, double)>
and apply that instance to the point. E.g.

import java.awt.geom.*;

Point2D original = new Point2D.Double(x, y);
AffineTransform at = getRotateInstance(theta, 200.0, 200.0);
Point2D rotated = at.transform(original, null);

BTW: I would always store all angles in radiants internally in the
application. Repeated conversions between degrees and radiants won't
make the result any more precise.

BTW2: I would also not convert to float, but do and store all
calculations in double to get a little bit more precise results.

BTW3: Don't expect to get the exact original point back when your rotate
the point back. Floating point arithmetic, whether the float or the
double data type, is not precise.

/Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,135
Latest member
VeronaShap
Top