1 | package org.expeditee.core;
|
---|
2 |
|
---|
3 | /**
|
---|
4 | * Represents a (x,y)-coordinate in 2-dimensional space. Coordinates are
|
---|
5 | * integral.
|
---|
6 | *
|
---|
7 | * @author cts16
|
---|
8 | */
|
---|
9 | public class Point {
|
---|
10 |
|
---|
11 | /** The origin of the coordinate system. */
|
---|
12 | public static final Point ORIGIN = new Point(0, 0);
|
---|
13 |
|
---|
14 | /** The x-coordinate of this point. */
|
---|
15 | private int x;
|
---|
16 | /** The y-coordinate of this point. */
|
---|
17 | private int y;
|
---|
18 |
|
---|
19 | /** Default constructor creates a point at the origin. */
|
---|
20 | public Point() {
|
---|
21 | this(0, 0);
|
---|
22 | }
|
---|
23 |
|
---|
24 | /** Standard constructor. */
|
---|
25 | public Point(int x, int y) {
|
---|
26 | set(x, y);
|
---|
27 | }
|
---|
28 |
|
---|
29 | /** Convenience constructor automatically casts floats to ints. */
|
---|
30 | public Point(float x, float y) {
|
---|
31 | this((int) x, (int) y);
|
---|
32 | }
|
---|
33 |
|
---|
34 | /** Copy constructor. */
|
---|
35 | public Point(Point other) {
|
---|
36 | this();
|
---|
37 | set(other);
|
---|
38 | }
|
---|
39 |
|
---|
40 | /** Sets the position of the point. */
|
---|
41 | public Point set(int x, int y) {
|
---|
42 | this.setX(x);
|
---|
43 | this.setY(y);
|
---|
44 | return this;
|
---|
45 | }
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Sets the position of this point to be the same as the given point. If p is
|
---|
49 | * null, no change is made.
|
---|
50 | */
|
---|
51 | public Point set(Point p) {
|
---|
52 | if (p != null)
|
---|
53 | set(p.getX(), p.getY());
|
---|
54 | return this;
|
---|
55 | }
|
---|
56 |
|
---|
57 | /** Moves the point by the given amount in each dimension. */
|
---|
58 | public Point add(int x, int y) {
|
---|
59 | this.setX(this.getX() + x);
|
---|
60 | this.setY(this.getY() + y);
|
---|
61 | return this;
|
---|
62 | }
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Moves the point by the given amount in each dimension. Doubles are
|
---|
66 | * automatically cast to ints.
|
---|
67 | */
|
---|
68 | public Point add(double x, double y) {
|
---|
69 | return add((int) x, (int) y);
|
---|
70 | }
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Adds the coordinates of the given point to this point's coordinates (treating
|
---|
74 | * points as vectors).
|
---|
75 | */
|
---|
76 | public Point add(Point p) {
|
---|
77 | if (p != null)
|
---|
78 | add(p.getX(), p.getY());
|
---|
79 | return this;
|
---|
80 | }
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Rotates this point around another point.
|
---|
84 | *
|
---|
85 | * @param angle
|
---|
86 | * The angle to rotate through (clockwise, in radians).
|
---|
87 | *
|
---|
88 | * @param x
|
---|
89 | * The x-coordinate of the centre of rotation.
|
---|
90 | *
|
---|
91 | * @param y
|
---|
92 | * The y-coordinate of the centre of rotation.
|
---|
93 | */
|
---|
94 | public Point rotate(double angle, int x, int y) {
|
---|
95 | // Find the distance from this point to the centre of rotation
|
---|
96 | // (should stay the same during rotation)
|
---|
97 | double length = Point.distanceBetween(x, y, this.getX(), this.getY());
|
---|
98 |
|
---|
99 | // Find the starting elevation angle of this point relative to
|
---|
100 | // the centre of rotation.
|
---|
101 | double startingAngle = Math.atan2(this.getY() - y, this.getX() - x);
|
---|
102 |
|
---|
103 | // Increase/decrease the elevation angle by the given amount
|
---|
104 | double finalAngle = startingAngle + angle;
|
---|
105 |
|
---|
106 | // Calculate the point at the same distance from the centre of rotation
|
---|
107 | // elevated at the new angle.
|
---|
108 | this.setX((int) (x + length * Math.cos(finalAngle)));
|
---|
109 | this.setY((int) (y + length * Math.sin(finalAngle)));
|
---|
110 |
|
---|
111 | return this;
|
---|
112 | }
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Rotates this point around another point.
|
---|
116 | *
|
---|
117 | * @param angle
|
---|
118 | * The angle to rotate through (clockwise, in radians).
|
---|
119 | *
|
---|
120 | * @param cor
|
---|
121 | * The centre of rotation.
|
---|
122 | */
|
---|
123 | public Point rotate(double angle, Point cor) {
|
---|
124 | if (cor != null)
|
---|
125 | rotate(angle, cor.getX(), cor.getY());
|
---|
126 | return this;
|
---|
127 | }
|
---|
128 |
|
---|
129 | /** Gets the distance from this point to another point. */
|
---|
130 | public double getDistanceTo(Point other) {
|
---|
131 | return distanceBetween(this, other);
|
---|
132 | }
|
---|
133 |
|
---|
134 | /** Gets the distance between two points. */
|
---|
135 | public static double distanceBetween(Point p1, Point p2) {
|
---|
136 | if (p1 == null || p2 == null)
|
---|
137 | return 0.0;
|
---|
138 |
|
---|
139 | return distanceBetween(p1.getX(), p1.getY(), p2.getX(), p2.getY());
|
---|
140 | }
|
---|
141 |
|
---|
142 | /** Gets the distance between two points specified by their coordinates. */
|
---|
143 | public static double distanceBetween(int x1, int y1, int x2, int y2) {
|
---|
144 | double x = x1 - x2;
|
---|
145 | double y = y1 - y2;
|
---|
146 | return Math.sqrt(x * x + y * y);
|
---|
147 | }
|
---|
148 |
|
---|
149 | @Override
|
---|
150 | public boolean equals(Object other) {
|
---|
151 | if (other instanceof Point) {
|
---|
152 | Point p = (Point) other;
|
---|
153 | return this.getX() == p.getX() && this.getY() == p.getY();
|
---|
154 | }
|
---|
155 | return false;
|
---|
156 | }
|
---|
157 |
|
---|
158 | /** Gets the x-coordinate of this point. */
|
---|
159 | public int getX() {
|
---|
160 | return x;
|
---|
161 | }
|
---|
162 |
|
---|
163 | /** Gets the y-coordinate of this point. */
|
---|
164 | public int getY() {
|
---|
165 | return y;
|
---|
166 | }
|
---|
167 |
|
---|
168 | @Override
|
---|
169 | public Point clone() {
|
---|
170 | return new Point(this);
|
---|
171 | }
|
---|
172 |
|
---|
173 | /**
|
---|
174 | * Gets a point representing the difference between the two given points
|
---|
175 | * (returned point should be treated as a vector).
|
---|
176 | */
|
---|
177 | public static Point difference(Point p1, Point p2) {
|
---|
178 | if (p1 == null || p2 == null)
|
---|
179 | return null;
|
---|
180 | return new Point(p1.getX() - p2.getX(), p1.getY() - p2.getY());
|
---|
181 | }
|
---|
182 |
|
---|
183 | /** Calculates the dot-product between two lines that share a common point. */
|
---|
184 | public static double dotProduct(Point commonPoint, Point lineEnd1, Point lineEnd2) {
|
---|
185 | if (commonPoint == null || lineEnd1 == null || lineEnd2 == null)
|
---|
186 | return 0.0;
|
---|
187 |
|
---|
188 | Point p1 = new Point(lineEnd1.getX() - commonPoint.getX(), lineEnd1.getY() - commonPoint.getY());
|
---|
189 | Point p2 = new Point(lineEnd2.getX() - commonPoint.getX(), lineEnd2.getY() - commonPoint.getY());
|
---|
190 | return p1.getX() * p2.getX() + p1.getY() * p2.getY();
|
---|
191 | }
|
---|
192 |
|
---|
193 | @Override
|
---|
194 | public String toString() {
|
---|
195 | return "(" + getX() + ", " + getY() + ")";
|
---|
196 | }
|
---|
197 |
|
---|
198 | public void setY(int y) {
|
---|
199 | this.y = y;
|
---|
200 | }
|
---|
201 |
|
---|
202 | public void setX(int x) {
|
---|
203 | this.x = x;
|
---|
204 | }
|
---|
205 | }
|
---|