1
2
3
4
5
6
7
8
9
10 package nl.tudelft.simulation.language.d2;
11
12 import java.awt.geom.Rectangle2D;
13
14 /***
15 * Shape utilities.
16 * <p>
17 * (c) copyright 2003 <a href="http://www.simulation.tudelft.nl">Delft
18 * University of Technology </a>, the Netherlands. <br>
19 * See for project information <a
20 * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
21 * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
22 * License (GPL) </a>, no warranty <br>
23 *
24 * @author <a href="http://www.tbm.tudelft.nl/webstaf/peterja/index.htm">Peter
25 * Jacobs </a>
26 * @version 1.2 Jun 17, 2004
27 * @since 1.4
28 */
29 public final class Shape
30 {
31 /***
32 * constructs a new Shape
33 */
34 private Shape()
35 {
36 super();
37 }
38
39 /***
40 * overlaps extent and shape. Overlap = Intersect + Contain
41 *
42 * @param extent the extent
43 * @param shape the shape
44 * @return whether extent overlaps shape
45 */
46 public static boolean overlaps(final Rectangle2D extent,
47 final Rectangle2D shape)
48 {
49 if (extent.getMaxX() < shape.getMinX())
50 {
51 return false;
52 }
53 if (extent.getMaxY() < shape.getMinY())
54 {
55 return false;
56 }
57 if (extent.getMinX() > shape.getMaxX())
58 {
59 return false;
60 }
61 if (extent.getMinY() > shape.getMaxY())
62 {
63 return false;
64 }
65 return true;
66 }
67
68 /***
69 * @param r1 the first rectangle
70 * @param r2 the second rectangle
71 * @return whether r1 intersects r2
72 */
73 public static boolean intersects(final Rectangle2D r1, final Rectangle2D r2)
74 {
75 return !Shape.contains(r1, r2) && Shape.intersects(r1, r2);
76 }
77
78 /***
79 * is r1 completely in r2
80 *
81 * @param r1 the first rectangle
82 * @param r2 the second rectangle
83 * @return whether r1 in r2
84 */
85 public static boolean contains(final Rectangle2D r1, final Rectangle2D r2)
86 {
87 boolean contains = (r1.getMinX() <= r1.getMinX()
88 && r1.getMinY() <= r2.getMinY() && r1.getMaxX() >= r2.getMaxX() && r1
89 .getMaxY() >= r2.getMaxY());
90 return contains;
91 }
92 }