1 package org.djutils.draw.point;
2
3 import java.util.Arrays;
4 import java.util.Iterator;
5 import java.util.Locale;
6 import java.util.Objects;
7
8 import org.djutils.draw.Directed;
9 import org.djutils.exceptions.Throw;
10 import org.djutils.math.AngleUtil;
11
12 /**
13 * A DirectedPoint2d is a Point2d that additionally carries a direction in 2d-space (dirZ). This is <b>not</b> the direction
14 * that the point is when viewed from the origin (0,0).
15 * <p>
16 * Copyright (c) 2023-2025 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
17 * for project information <a href="https://djutils.org" target="_blank"> https://djutils.org</a>. The DJUTILS project is
18 * distributed under a three-clause BSD-style license, which can be found at
19 * <a href="https://djutils.org/docs/license.html" target="_blank"> https://djutils.org/docs/license.html</a>.
20 * </p>
21 * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
22 * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
23 * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
24 */
25 public class DirectedPoint2d extends Point2d implements Directed
26 {
27 /** The counter-clockwise rotation around the point in radians. */
28 @SuppressWarnings("checkstyle:visibilitymodifier")
29 public final double dirZ;
30
31 /**
32 * Construct a new DirectedPoint2d with an x and y coordinate and a direction.
33 * @param x the x coordinate
34 * @param y the y coordinate
35 * @param dirZ the counter-clockwise rotation around the point in radians
36 * @throws IllegalArgumentException when any coordinate or <code>dirZ</code> is <code>NaN</code>
37 */
38 public DirectedPoint2d(final double x, final double y, final double dirZ)
39 {
40 super(x, y);
41 Throw.whenNaN(dirZ, "dirZ");
42 this.dirZ = dirZ;
43 }
44
45 /**
46 * Construct a new DirectedPoint2d from an x and y coordinates in a double[] and a direction.
47 * @param xy the <code>x</code> and <code>y</code> coordinates in that order
48 * @param dirZ the counter-clockwise rotation around the point in radians
49 * @throws NullPointerException when <code>xy</code> is <code>null</code>
50 * @throws ArithmeticException when any value in <code>xy</code> is <code>NaN</code> or <code>rotZ</code> is
51 * <code>NaN</code>
52 * @throws IllegalArgumentException when the length of <code>xy</code> is not 2
53 */
54 public DirectedPoint2d(final double[] xy, final double dirZ)
55 {
56 super(xy);
57 Throw.whenNaN(dirZ, "dirZ");
58 this.dirZ = dirZ;
59 }
60
61 /**
62 * Construct a new DirectedPoint2d from an AWT Point2D and a direction.
63 * @param point java.awt.geom.Point2D
64 * @param dirZ the counter-clockwise rotation around the point in radians
65 * @throws NullPointerException when <code>point</code> is <code>null</code>
66 * @throws ArithmeticException when <code>rotZ</code> is <code>NaN</code>
67 */
68 public DirectedPoint2d(final java.awt.geom.Point2D point, final double dirZ)
69 {
70 super(point);
71 Throw.whenNaN(dirZ, "dirZ");
72 this.dirZ = dirZ;
73 }
74
75 /**
76 * Construct a new DirectedPoint2d from a Point2d and a direction.
77 * @param point a point (with or without orientation)
78 * @param dirZ the counter-clockwise rotation around the point in radians
79 * @throws NullPointerException when <code>point</code> is <code>null</code>
80 * @throws ArithmeticException when <code>rotZ</code> is <code>NaN</code>
81 */
82 public DirectedPoint2d(final Point2d point, final double dirZ)
83 {
84 this(point.x, point.y, dirZ);
85 }
86
87 /**
88 * Construct a new DirectedPoint2d from two coordinates and the coordinates of a point that the direction goes through.
89 * @param x the x coordinate of the of the new DirectedPoint
90 * @param y the y coordinate of the of the new DirectedPoint
91 * @param throughX the x-coordinate of a point that the direction goes through
92 * @param throughY the y-coordinate of a point that the direction goes through
93 * @throws ArithmeticException when <code>throughX</code>, or <code>throughY</code> is <code>null</code>
94 * @throws IllegalArgumentException when <code>throughX == x</code> and <code>throughY == y</code>
95 */
96 public DirectedPoint2d(final double x, final double y, final double throughX, final double throughY)
97 {
98 this(x, y, buildDirection(throughX - x, throughY - y));
99 }
100
101 /**
102 * Build the direction.
103 * @param dX x difference
104 * @param dY y difference
105 * @return the computed value of dirZ
106 * @throws IllegalArgumentException when <code>dX == 0.0</code> and <code>dY == 0.0</code>
107 */
108 private static double buildDirection(final double dX, final double dY)
109 {
110 Throw.when(0 == dX && 0 == dY, IllegalArgumentException.class, "Through point may not be equal to point");
111 return Math.atan2(dY, dX);
112 }
113
114 /**
115 * Construct a new DirectedPoint2d from a Point2d and a point that the direction goes through.
116 * @param point the point
117 * @param throughPoint the point that the direction goes through
118 * @throws NullPointerException when <code>point</code> is <code>null</code>, or <code>throughPoint</code> ==
119 * <code>null</code>
120 * @throws IllegalArgumentException when <code>throughX == point.x</code> and <code>throughY ==
121 * point.y</code>
122 */
123 public DirectedPoint2d(final Point2d point, final Point2d throughPoint)
124 {
125 this(Throw.whenNull(point, "point").x, point.y, Throw.whenNull(throughPoint, "throughPoint").x, throughPoint.y);
126 }
127
128 /**
129 * Construct a new DirectedPoint2d from a Point2d and the coordinates of a point that the direction goes through.
130 * @param point the point
131 * @param throughX the x coordinate of a point that the direction goes through
132 * @param throughY the y coordinate of a point that the direction goes through
133 * @throws NullPointerException when <code>point</code> is <code>null</code>
134 * @throws ArithmeticException when <code>throughX</code>, or <code>throughY</code> is <code>NaN</code>
135 * @throws IllegalArgumentException when <code>throughX == point.x</code> and <code>throughY ==
136 * point.y</code>
137 */
138 public DirectedPoint2d(final Point2d point, final double throughX, final double throughY)
139 {
140 this(Throw.whenNull(point, "point").x, point.y, throughX, throughY);
141 }
142
143 @Override
144 public DirectedPoint2d translate(final double dX, final double dY)
145 {
146 return new DirectedPoint2d(this.x + dX, this.y + dY, this.dirZ);
147 }
148
149 @Override
150 public DirectedPoint3d translate(final double dX, final double dY, final double z)
151 throws ArithmeticException, IllegalArgumentException
152 {
153 return new DirectedPoint3d(this.x + dX, this.y + dY, z, 0, this.dirZ);
154 }
155
156 @Override
157 public DirectedPoint2d scale(final double factor) throws IllegalArgumentException
158 {
159 Throw.whenNaN(factor, "factor");
160 return new DirectedPoint2d(this.x * factor, this.y * factor, this.dirZ);
161 }
162
163 @Override
164 public DirectedPoint2d neg()
165 {
166 return new DirectedPoint2d(-this.x, -this.y, AngleUtil.normalizeAroundZero(this.dirZ + Math.PI));
167 }
168
169 @Override
170 public DirectedPoint2d abs()
171 {
172 return new DirectedPoint2d(Math.abs(this.x), Math.abs(this.y), this.dirZ);
173 }
174
175 @Override
176 public DirectedPoint2d normalize() throws IllegalArgumentException
177 {
178 double length = Math.sqrt(this.x * this.x + this.y * this.y);
179 Throw.when(length == 0.0, IllegalArgumentException.class, "cannot normalize (0.0, 0.0)");
180 return this.scale(1.0 / length);
181 }
182
183 /**
184 * Interpolate towards another DirectedPoint2d with a fraction. It is allowed for fraction to be less than zero or larger
185 * than 1. In that case the interpolation turns into an extrapolation. DirZ is interpolated using the
186 * AngleUtil.interpolateShortest method.
187 * @param otherPoint the other point
188 * @param fraction the factor for interpolation towards the other point. When <code>fraction</code> is between 0
189 * and 1, it is an interpolation, otherwise an extrapolation. If <code>fraction</code> is 0; <code>this</code>
190 * Point is returned; if <code>fraction</code> is 1, the <code>otherPoint</code> is returned
191 * @return a new OrientedPoint2d at the requested fraction
192 * @throws NullPointerException when <code>otherPoint</code> is <code>null</code>
193 * @throws ArithmeticException when <code>fraction</code> is <code>NaN</code>
194 */
195 public DirectedPoint2d interpolate(final DirectedPoint2d otherPoint, final double fraction)
196 {
197 Throw.whenNull(otherPoint, "otherPoint");
198 Throw.whenNaN(fraction, "fraction");
199 if (0.0 == fraction)
200 {
201 return this;
202 }
203 if (1.0 == fraction)
204 {
205 return otherPoint;
206 }
207 return new DirectedPoint2d((1.0 - fraction) * this.x + fraction * otherPoint.x,
208 (1.0 - fraction) * this.y + fraction * otherPoint.y,
209 AngleUtil.interpolateShortest(this.dirZ, otherPoint.dirZ, fraction));
210 }
211
212 /**
213 * Return a new DirectedPoint2d with an in-place rotation around the z-axis by the provided rotateZ. The resulting rotation
214 * is normalized between -π and π.
215 * @param rotateZ the rotation around the z-axis
216 * @return a new point with the same coordinates and applied rotation
217 * @throws ArithmeticException when <code>rotateZ</code> is <code>NaN</code>
218 */
219 public DirectedPoint2d rotate(final double rotateZ)
220 {
221 Throw.whenNaN(rotateZ, "rotateZ");
222 return new DirectedPoint2d(this.x, this.y, AngleUtil.normalizeAroundZero(this.dirZ + rotateZ));
223 }
224
225 @Override
226 public double getDirZ()
227 {
228 return this.dirZ;
229 }
230
231 @Override
232 public Iterator<Point2d> iterator()
233 {
234 return Arrays.stream(new Point2d[] {this}).iterator();
235 }
236
237 @Override
238 public String toString()
239 {
240 return toString("%f", false);
241 }
242
243 @Override
244 public String toString(final String doubleFormat, final boolean doNotIncludeClassName)
245 {
246 String format =
247 String.format("%1$s[x=%2$s, y=%2$s, dirZ=%2$s]", doNotIncludeClassName ? "" : "DirectedPoint2d ", doubleFormat);
248 return String.format(Locale.US, format, this.x, this.y, this.dirZ);
249 }
250
251 /**
252 * Compare this Directed with another Directed with specified tolerances in the coordinates and the angles.
253 * @param other the Directed to compare to
254 * @param epsilonCoordinate the upper bound of difference for one of the coordinates; use Double.POSITIVE_INFINITY if you do
255 * not want to check the coordinates
256 * @param epsilonDirection the upper bound of difference for the direction(s); use Double.POSITIVE_INFINITY if you do not
257 * want to check the angles
258 * @return boolean;<code>true</code> if <code>x</code> and <code>y</code> are less than <code>epsilonCoordinate</code>
259 * apart, and <code>rotZ</code> is less than <code>epsilonDirection</code> apart, otherwise <code>false</code>
260 * @throws NullPointerException when <code>other</code> is <code>null</code>
261 * @throws ArithmeticException when <code>epsilonCoordinate</code> or <code>epsilonDirection</code> is <code>NaN</code>
262 * @throws IllegalArgumentException <code>epsilonCoordinate</code> or <code>epsilonDirection</code> is <code>negative</code>
263 */
264 public boolean epsilonEquals(final DirectedPoint2d other, final double epsilonCoordinate, final double epsilonDirection)
265 throws NullPointerException, IllegalArgumentException
266 {
267 Throw.whenNull(other, "other");
268 Throw.whenNaN(epsilonCoordinate, "epsilonCoordinate");
269 Throw.whenNaN(epsilonDirection, "epsilonDirection");
270 Throw.when(epsilonCoordinate < 0 || epsilonDirection < 0, IllegalArgumentException.class,
271 "epsilonCoordinate and epsilonRotation may not be negative");
272 if (Math.abs(this.x - other.x) > epsilonCoordinate)
273 {
274 return false;
275 }
276 if (Math.abs(this.y - other.y) > epsilonCoordinate)
277 {
278 return false;
279 }
280 if (Math.abs(AngleUtil.normalizeAroundZero(this.dirZ - other.dirZ)) > epsilonDirection)
281 {
282 return false;
283 }
284 return true;
285 }
286
287 @Override
288 public int hashCode()
289 {
290 final int prime = 31;
291 int result = super.hashCode();
292 result = prime * result + Objects.hash(this.dirZ);
293 return result;
294 }
295
296 @Override
297 @SuppressWarnings("checkstyle:needbraces")
298 public boolean equals(final Object obj)
299 {
300 if (this == obj)
301 return true;
302 if (!super.equals(obj))
303 return false;
304 if (getClass() != obj.getClass())
305 return false;
306 DirectedPoint2d other = (DirectedPoint2d) obj;
307 return Double.doubleToLongBits(this.dirZ) == Double.doubleToLongBits(other.dirZ);
308 }
309
310 }