source: trunk/src/org/expeditee/core/Point.java@ 1097

Last change on this file since 1097 was 1097, checked in by davidb, 6 years ago

Newly structured files from Corey's work on logic/graphics separation

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