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?
Barry - 28 Feb 2006 15:42 GMT
> I use the following method to rotate a point around the origin.
>
[quoted text clipped - 10 lines]
> 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.
Zerex71 - 28 Feb 2006 15:44 GMT
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
Thomas Weidenfeller - 28 Feb 2006 16:18 GMT
> I use the following method to rotate a point around the origin.
>
[quoted text clipped - 10 lines]
> 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/api/java/awt/geom/AffineTransform.html#getRo
tateInstance(double,%20double,%20double)>
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

Signature
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/
Roedy Green - 28 Feb 2006 18:56 GMT
>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?
see http://mindprod.com/jgloss/affinetransform.html

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.