1 package nl.tudelft.simulation.language.d2;
2
3 import org.djutils.draw.bounds.Bounds2d;
4
5 /**
6 * Shape utilities for world coordinates.
7 * <p>
8 * Copyright (c) 2002-2025 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
9 * for project information <a href="https://simulation.tudelft.nl/dsol/manual/" target="_blank">DSOL Manual</a>. The DSOL
10 * project is distributed under a three-clause BSD-style license, which can be found at
11 * <a href="https://simulation.tudelft.nl/dsol/docs/latest/license.html" target="_blank">DSOL License</a>.
12 * </p>
13 * @author <a href="https://www.linkedin.com/in/peterhmjacobs">Peter Jacobs </a>
14 */
15 public final class Shape2d
16 {
17 /**
18 * constructs a new Shape.
19 */
20 private Shape2d()
21 {
22 // utility class.
23 }
24
25 /**
26 * overlaps extent and shape. Overlap = Intersect + Contain
27 * @param extent the extent
28 * @param shape the shape
29 * @return whether extent overlaps shape
30 */
31 public static boolean overlaps(final Bounds2d extent, final Bounds2d shape)
32 {
33 if (extent.getMaxX() < shape.getMinX())
34 {
35 return false;
36 }
37 if (extent.getMaxY() < shape.getMinY())
38 {
39 return false;
40 }
41 if (extent.getMinX() > shape.getMaxX())
42 {
43 return false;
44 }
45 if (extent.getMinY() > shape.getMaxY())
46 {
47 return false;
48 }
49 return true;
50 }
51
52 /**
53 * do the rectangles really intersect (so, not being contained)?
54 * @param r1 the first rectangle
55 * @param r2 the second rectangle
56 * @return whether r1 really intersects r2
57 */
58 public static boolean intersects(final Bounds2d r1, final Bounds2d r2)
59 {
60 return !Shape2d.contains(r1, r2) && !Shape2d.contains(r2, r1) && overlaps(r1, r2);
61 }
62
63 /**
64 * is r1 completely in r2.
65 * @param r1 the first rectangle
66 * @param r2 the second rectangle
67 * @return whether r1 in r2
68 */
69 public static boolean contains(final Bounds2d r1, final Bounds2d r2)
70 {
71 boolean contains = (r2.getMinX() <= r1.getMinX() && r2.getMinY() <= r1.getMinY() && r2.getMaxX() >= r1.getMaxX()
72 && r2.getMaxY() >= r1.getMaxY());
73 return contains;
74 }
75 }