View Javadoc

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