I'm trying to use this method to rotate a point:
AffineTransform at = AffineTransform.getRotateInstance(Math.toRadians(90), 1, 2);
double[] pt = {0, 1};
at.transform(pt, 0, pt, 0, 1);
int x = (int)pt[0];
int y = (int)pt[1];
The expected result is x=0
and y=3
because we are rotating 90* clockwise, however the output is currently x=2
and y=1
;
Why am I not getting the expected result?
The program is doing nothing wrong. By specifying Math.toRadians(90)
as your theta, you are instructing it to rotate counter-clockwise, not clockwise. That is how rotation is defined in trigonometry.
Using -Math.toRadians(90)
would get you the correct result.
Also, as a side note, casting the point values to int
isn't the best idea for checking your code. Double-precision trigonometry is prone to very minor errors, and so a tiny bump in the wrong direction would get you the wrong integers. (I.e., 2.99999999995 would be cast to 2)
(int) Math.round(value)
is much safer