1 package org.djutils.draw.bounds;
2
3 import java.awt.geom.Rectangle2D;
4 import java.util.Arrays;
5 import java.util.Collection;
6 import java.util.Iterator;
7 import java.util.Locale;
8
9 import org.djutils.draw.Drawable2d;
10 import org.djutils.draw.point.Point2d;
11 import org.djutils.exceptions.Throw;
12
13
14
15
16
17
18
19
20
21
22
23 public class Bounds2d implements Drawable2d, Bounds<Bounds2d, Point2d>
24 {
25
26 private static final long serialVersionUID = 20200829L;
27
28
29 private final double minX;
30
31
32 private final double minY;
33
34
35 private final double maxX;
36
37
38 private final double maxY;
39
40
41
42
43
44
45
46
47
48
49 public Bounds2d(final double minX, final double maxX, final double minY, final double maxY) throws IllegalArgumentException
50 {
51 Throw.when(Double.isNaN(minX) || Double.isNaN(maxX) || Double.isNaN(minY) || Double.isNaN(maxY),
52 IllegalArgumentException.class, "bounds must be numbers (not NaN)");
53 Throw.when(minX > maxX || minY > maxY, IllegalArgumentException.class,
54 "lower bound for each dimension should be less than or equal to its upper bound");
55 this.minX = minX;
56 this.minY = minY;
57 this.maxX = maxX;
58 this.maxY = maxY;
59 }
60
61
62
63
64
65
66
67 public Bounds2d(final double deltaX, final double deltaY)
68 {
69 this(-0.5 * deltaX, 0.5 * deltaX, -0.5 * deltaY, 0.5 * deltaY);
70 }
71
72
73
74
75
76
77
78
79 public Bounds2d(final Iterator<? extends Point2d> points)
80 {
81 Throw.whenNull(points, "points");
82 Throw.when(!points.hasNext(), IllegalArgumentException.class, "need at least one point");
83 Point2d point = points.next();
84 double tempMinX = point.x;
85 double tempMaxX = point.x;
86 double tempMinY = point.y;
87 double tempMaxY = point.y;
88 while (points.hasNext())
89 {
90 point = points.next();
91 tempMinX = Math.min(tempMinX, point.x);
92 tempMaxX = Math.max(tempMaxX, point.x);
93 tempMinY = Math.min(tempMinY, point.y);
94 tempMaxY = Math.max(tempMaxY, point.y);
95 }
96 this.minX = tempMinX;
97 this.maxX = tempMaxX;
98 this.minY = tempMinY;
99 this.maxY = tempMaxY;
100 }
101
102
103
104
105
106
107
108 public Bounds2d(final Point2d[] points) throws NullPointerException, IllegalArgumentException
109 {
110 this(Arrays.stream(Throw.whenNull(points, "points")).iterator());
111 }
112
113
114
115
116
117
118 public Bounds2d(final Drawable2d drawable2d) throws NullPointerException
119 {
120 this(Throw.whenNull(drawable2d, "drawable2d").getPoints());
121 }
122
123
124
125
126
127
128
129 public Bounds2d(final Drawable2d... drawable2d) throws NullPointerException, IllegalArgumentException
130 {
131 this(pointsOf(drawable2d));
132 }
133
134
135
136
137
138
139
140
141 static Drawable2d[] ensureHasOne(final Drawable2d[] drawable2dArray) throws NullPointerException, IllegalArgumentException
142 {
143 Throw.whenNull(drawable2dArray, "drawable2dArray");
144 Throw.when(drawable2dArray.length == 0, IllegalArgumentException.class, "Array must contain at least one value");
145 return drawable2dArray;
146 }
147
148
149
150
151
152
153
154
155 public static Iterator<Point2d> pointsOf(final Drawable2d... drawable2d)
156 {
157 return new Iterator<Point2d>()
158 {
159
160 private int nextArgument = 0;
161
162
163 private Iterator<? extends Point2d> currentIterator = ensureHasOne(drawable2d)[0].getPoints();
164
165 @Override
166 public boolean hasNext()
167 {
168 return this.nextArgument < drawable2d.length - 1 || this.currentIterator.hasNext();
169 }
170
171 @Override
172 public Point2d next()
173 {
174 if (this.currentIterator.hasNext())
175 {
176 return this.currentIterator.next();
177 }
178
179 this.nextArgument++;
180 this.currentIterator = drawable2d[this.nextArgument].getPoints();
181 return this.currentIterator.next();
182 }
183 };
184 }
185
186
187
188
189
190
191
192 public Bounds2d(final Collection<Drawable2d> drawableCollection) throws NullPointerException, IllegalArgumentException
193 {
194 this(pointsOf(drawableCollection));
195 }
196
197
198
199
200
201
202
203
204 public static Iterator<Point2d> pointsOf(final Collection<Drawable2d> drawableCollection)
205 throws NullPointerException, IllegalArgumentException
206 {
207 return new Iterator<Point2d>()
208 {
209
210 private Iterator<Drawable2d> collectionIterator = ensureHasOne(drawableCollection.iterator());
211
212
213 private Iterator<? extends Point2d> currentIterator = this.collectionIterator.next().getPoints();
214
215 @Override
216 public boolean hasNext()
217 {
218 if (this.currentIterator == null)
219 {
220 return false;
221 }
222 return this.currentIterator.hasNext();
223 }
224
225 @Override
226 public Point2d next()
227 {
228 Point2d result = this.currentIterator.next();
229 if (!this.currentIterator.hasNext())
230 {
231 if (this.collectionIterator.hasNext())
232 {
233 this.currentIterator = this.collectionIterator.next().getPoints();
234 }
235 else
236 {
237 this.currentIterator = null;
238 }
239 }
240 return result;
241 }
242 };
243 }
244
245
246
247
248
249
250
251
252 public static Iterator<Drawable2d> ensureHasOne(final Iterator<Drawable2d> iterator)
253 throws NullPointerException, IllegalArgumentException
254 {
255 Throw.when(!iterator.hasNext(), IllegalArgumentException.class, "Collection may not be empty");
256 return iterator;
257 }
258
259 @Override
260 public Iterator<Point2d> getPoints()
261 {
262 Point2d[] array = new Point2d[] {new Point2d(this.minX, this.minY), new Point2d(this.minX, this.maxY),
263 new Point2d(this.maxX, this.minY), new Point2d(this.maxX, this.maxY)};
264 return Arrays.stream(array).iterator();
265 }
266
267 @Override
268 public int size()
269 {
270 return 4;
271 }
272
273
274
275
276
277
278
279
280 public boolean contains(final double x, final double y) throws IllegalArgumentException
281 {
282 Throw.when(Double.isNaN(x) || Double.isNaN(y), IllegalArgumentException.class, "coordinates must be numbers (not NaN)");
283 return x > this.minX && x < this.maxX && y > this.minY && y < this.maxY;
284 }
285
286 @Override
287 public boolean contains(final Point2d point)
288 {
289 Throw.whenNull(point, "point");
290 return contains(point.x, point.y);
291 }
292
293 @Override
294 public boolean contains(final Bounds2d otherBounds) throws NullPointerException
295 {
296 Throw.whenNull(otherBounds, "otherBounds");
297 return contains(otherBounds.minX, otherBounds.minY) && contains(otherBounds.maxX, otherBounds.maxY);
298 }
299
300
301
302
303
304
305
306 public boolean covers(final double x, final double y)
307 {
308 Throw.when(Double.isNaN(x) || Double.isNaN(y), IllegalArgumentException.class, "coordinates must be numbers (not NaN)");
309 return x >= this.minX && x <= this.maxX && y >= this.minY && y <= this.maxY;
310 }
311
312 @Override
313 public boolean covers(final Point2d point)
314 {
315 Throw.whenNull(point, "point");
316 return covers(point.x, point.y);
317 }
318
319 @Override
320 public boolean covers(final Bounds2d otherBounds) throws NullPointerException
321 {
322 Throw.whenNull(otherBounds, "otherBounds");
323 return covers(otherBounds.minX, otherBounds.minY) && covers(otherBounds.maxX, otherBounds.maxY);
324 }
325
326 @Override
327 public boolean disjoint(final Bounds2d otherBounds) throws NullPointerException
328 {
329 Throw.whenNull(otherBounds, "otherBounds");
330 return otherBounds.minX > this.maxX || otherBounds.maxX < this.minX || otherBounds.minY > this.maxY
331 || otherBounds.maxY < this.minY;
332 }
333
334 @Override
335 public boolean intersects(final Bounds2d otherBounds2d) throws NullPointerException
336 {
337 return !disjoint(otherBounds2d);
338 }
339
340 @Override
341 public Bounds2d intersection(final Bounds2d otherBounds2d)
342 {
343 Throw.whenNull(otherBounds2d, "otherBounds2d");
344 if (disjoint(otherBounds2d))
345 {
346 return null;
347 }
348 return new Bounds2d(Math.max(this.minX, otherBounds2d.minX), Math.min(this.maxX, otherBounds2d.maxX),
349 Math.max(this.minY, otherBounds2d.minY), Math.min(this.maxY, otherBounds2d.maxY));
350 }
351
352
353
354
355
356 public Rectangle2D toRectangle2D()
357 {
358 return new Rectangle2D.Double(this.minX, this.minY, this.maxX - this.minX, this.maxY - this.minY);
359 }
360
361 @Override
362 public double getMinX()
363 {
364 return this.minX;
365 }
366
367 @Override
368 public double getMaxX()
369 {
370 return this.maxX;
371 }
372
373 @Override
374 public double getMinY()
375 {
376 return this.minY;
377 }
378
379 @Override
380 public double getMaxY()
381 {
382 return this.maxY;
383 }
384
385 @Override
386 public Point2d midPoint()
387 {
388 return new Point2d((this.minX + this.maxX) / 2, (this.minY + this.maxY) / 2);
389 }
390
391
392
393
394
395 public double getArea()
396 {
397 return getDeltaX() * getDeltaY();
398 }
399
400 @Override
401 public Bounds2d getBounds()
402 {
403 return this;
404 }
405
406 @Override
407 public String toString()
408 {
409 return toString("%f");
410 }
411
412 @Override
413 public String toString(final String doubleFormat, final boolean doNotIncludeClassName)
414 {
415 String format =
416 String.format("%1$s[x[%2$s : %2$s], y[%2$s : %2$s]]", doNotIncludeClassName ? "" : "Bounds2d ", doubleFormat);
417 return String.format(Locale.US, format, this.minX, this.maxX, this.minY, this.maxY);
418 }
419
420 @Override
421 public int hashCode()
422 {
423 final int prime = 31;
424 int result = 1;
425 long temp;
426 temp = Double.doubleToLongBits(this.maxX);
427 result = prime * result + (int) (temp ^ (temp >>> 32));
428 temp = Double.doubleToLongBits(this.maxY);
429 result = prime * result + (int) (temp ^ (temp >>> 32));
430 temp = Double.doubleToLongBits(this.minX);
431 result = prime * result + (int) (temp ^ (temp >>> 32));
432 temp = Double.doubleToLongBits(this.minY);
433 result = prime * result + (int) (temp ^ (temp >>> 32));
434 return result;
435 }
436
437 @SuppressWarnings("checkstyle:needbraces")
438 @Override
439 public boolean equals(final Object obj)
440 {
441 if (this == obj)
442 return true;
443 if (obj == null)
444 return false;
445 if (getClass() != obj.getClass())
446 return false;
447 Bounds2d other = (Bounds2d) obj;
448 if (Double.doubleToLongBits(this.maxX) != Double.doubleToLongBits(other.maxX))
449 return false;
450 if (Double.doubleToLongBits(this.maxY) != Double.doubleToLongBits(other.maxY))
451 return false;
452 if (Double.doubleToLongBits(this.minX) != Double.doubleToLongBits(other.minX))
453 return false;
454 if (Double.doubleToLongBits(this.minY) != Double.doubleToLongBits(other.minY))
455 return false;
456 return true;
457 }
458
459 }