forked from williamfiset/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PointRotation.java
61 lines (49 loc) · 1.96 KB
/
PointRotation.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* This file shows you how to rotate a point clockwise relative to a fixed point a certain number of
* radians.
*
* <p>Time Complexity: O(1)
*
* @author William Fiset, william.alexandre.fiset@gmail.com
*/
package com.williamfiset.algorithms.geometry;
import static java.lang.Math.*;
import java.awt.geom.Point2D;
public class PointRotation {
// Rotate point 'pt' a certain number of radians clockwise
// relative to some fixed point 'fp'. Note that the angle
// should be specified in radians, not degrees.
public static Point2D rotatePoint(Point2D fp, Point2D pt, double angle) {
double fpx = fp.getX();
double fpy = fp.getY();
double ptx = pt.getX();
double pty = pt.getY();
// Compute the vector <x, y> from the fixed point
// to the point of rotation.
double x = ptx - fpx;
double y = pty - fpy;
// Apply the clockwise rotation matrix to the vector <x, y>
// | cosθ sinθ ||x| | xcosθ + ysinθ |
// | -sinθ cosθ ||y| = | -xsinθ + ycosθ |
double xRotated = x * cos(angle) + y * sin(angle);
double yRotated = y * cos(angle) - x * sin(angle);
// The rotation matrix rotated the vector about the origin, so we
// need to offset it by the point (fpx, fpy) to get the right answer
return new Point2D.Double(fpx + xRotated, fpy + yRotated);
}
public static void main(String[] args) {
// Suppose we want to rotate the point (4,5) about the point
// (3, 5) 45 degrees (pi/4 radians) clockwise 8 times until
// it cycles back to it's original position:
double angle = PI / 4.0;
Point2D fixedPoint = new Point2D.Double(3, 5);
Point2D point = new Point2D.Double(4, 5);
// Prints all 8 rotations
Point2D rotatedPoint = point;
for (int i = 0; i < 8; i++) {
System.out.println("Rotated point is now at: " + rotatedPoint);
rotatedPoint = rotatePoint(fixedPoint, rotatedPoint, angle);
}
System.out.println("Rotated point is now at: " + rotatedPoint);
}
}