View Javadoc

1   /*
2    * @(#) DirectionalShape.java 23-jul-2004 Copyright (c) 2002-2005 Delft University of Technology Jaffalaan 5,
3    * 2628 BX Delft, the Netherlands. All rights reserved. This software is proprietary information of Delft
4    * University of Technology The code is published under the Lesser General Public License
5    */
6   package nl.tudelft.simulation.language.d2;
7   
8   import java.util.ArrayList;
9   import java.util.List;
10  
11  import javax.vecmath.Point2d;
12  
13  /***
14   * DirectionalShape is used to create a shape out of vertices and find out whether a certain point is inside
15   * or outside of the shape.
16   * <p>
17   * (c) copyright 2002-2005 <a href="http://www.simulation.tudelft.nl">Delft University of Technology </a>, the
18   * Netherlands.
19   * <p>
20   * See for project information <a
21   * href="http://www.simulation.tudelft.nl/dsol/language">www.simulation.tudelft.nl/language </a> <br>
22   * License of use: <a href="http://www.gnu.org/copyleft/lesser.html">Lesser General Public License (LGPL)
23   * </a>, no warranty
24   * 
25   * @version $Revision: 1.7 $ $Date: 2005/07/04 12:21:25 $
26   * @author <a href="mailto:royc@tbm.tudelft.nl">Roy Chin </a>
27   */
28  public final class DirectionalShape
29  {
30      /*** points that span up the shape. */
31      private List<Point2d> points = new ArrayList<Point2d>();
32  
33      /*** lines that connect the points. */
34      private List<DirectionalLine> lines = new ArrayList<DirectionalLine>();
35  
36      /*** the default last side. */
37      public static final int DEFAULT_LAST_SIDE = -10;
38  
39      /***
40       * constructs a new directional line.
41       */
42      public DirectionalShape()
43      {
44          super();
45      }
46  
47      /***
48       * add a point to the shape.
49       * 
50       * @param x
51       *            X coordinate
52       * @param y
53       *            Y coordinate
54       */
55      public void addPoint(final double x, final double y)
56      {
57          this.points.add(new Point2d(x, y));
58      }
59  
60      /***
61       * determine the line segments between the points.
62       */
63      public void determineSegments()
64      {
65          // First clear possible previous segments
66          this.lines.clear();
67          // All segments but the closing segment
68          for (int i = 0; i < this.points.size() - 1; i++)
69          {
70              double x1 = this.points.get(i).x;
71              double y1 = this.points.get(i).y;
72              double x2 = this.points.get(i + 1).x;
73              double y2 = this.points.get(i + 1).y;
74              DirectionalLine line = new DirectionalLine(x1, y1, x2, y2);
75              this.lines.add(line);
76          }
77          // The closing segment
78          double x1 = this.points.get(this.points.size() - 1).x;
79          double y1 = this.points.get(this.points.size() - 1).y;
80          double x2 = this.points.get(0).x;
81          double y2 = this.points.get(0).y;
82          DirectionalLine line = new DirectionalLine(x1, y1, x2, y2);
83          this.lines.add(line);
84      }
85  
86      /***
87       * determine whether a point (x,y) is inside this shape or not.
88       * 
89       * @param x
90       *            X coordinate
91       * @param y
92       *            Y coodinate
93       * @return True if (x,y) is inside this shape
94       */
95      public boolean getInside(final double x, final double y)
96      {
97          boolean result = true;
98          // Note -10 is just an arbritrary number that is not
99          // being used as one of the constants in DirectionalLine
100         int lastSide = DEFAULT_LAST_SIDE;
101         for (DirectionalLine line : this.lines)
102         {
103             int side = line.getSideThin(x, y);
104             if (lastSide != DEFAULT_LAST_SIDE)
105             {
106                 if (side != lastSide)
107                 {
108                     result = false;
109                 }
110             }
111             lastSide = side;
112         }
113         return result;
114     }
115 }