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