View Javadoc

1   /*
2    * @(#) DirectionalShape.java 23-jul-2004
3    * 
4    * Copyright (c) 2003 Delft University of Technology Jaffalaan 5, 2628 BX Delft,
5    * the Netherlands All rights reserved.
6    * 
7    * This software is proprietary information of Delft University of Technology
8    * The code is published under the General Public License
9    */
10  package nl.tudelft.simulation.language.d2;
11  
12  import java.util.ArrayList;
13  import java.util.Iterator;
14  import java.util.List;
15  
16  import javax.vecmath.Point2d;
17  
18  /***
19   * DirectionalShape is used to create a shape out of vertices and find out
20   * whether a certain point is inside or outside of the shape
21   * <p>
22   * (c) copyright 2003 <a href="http://www.simulation.tudelft.nl">Delft
23   * University of Technology </a>, the Netherlands. <br>
24   * See for project information <a
25   * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
26   * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
27   * License (GPL) </a>, no warranty <br>
28   * 
29   * @version 1.0 <br>
30   * @author <a href="http://www.tbm.tudelft.nl/webstaf/royc/index.htm">Roy Chin
31   *         </a>
32   */
33  public class DirectionalShape
34  {
35      /*** points that span up the shape */
36      protected List points = new ArrayList();
37  
38      /*** lines that connect the points */
39      protected List lines = new ArrayList();
40  
41      /***
42       * constructs a new directional line
43       */
44      public DirectionalShape()
45      {
46          super();
47      }
48  
49      /***
50       * add a point to the shape
51       * 
52       * @param x X coordinate
53       * @param y 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 = ((Point2d) this.points.get(i)).x;
71              double y1 = ((Point2d) this.points.get(i)).y;
72              double x2 = ((Point2d) this.points.get(i + 1)).x;
73              double y2 = ((Point2d) 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 = ((Point2d) this.points.get(this.points.size() - 1)).x;
79          double y1 = ((Point2d) this.points.get(this.points.size() - 1)).y;
80          double x2 = ((Point2d) this.points.get(0)).x;
81          double y2 = ((Point2d) 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 X coordinate
90       * @param y Y coodinate
91       * @return True if (x,y) is inside this shape
92       */
93      public boolean getInside(final double x, final double y)
94      {
95          boolean result = true;
96          // Note -10 is just an arbritrary number that is not
97          // being used as one of the constants in DirectionalLine
98          int lastSide = -10;
99          for (Iterator i = this.lines.iterator(); i.hasNext();)
100         {
101             DirectionalLine line = (DirectionalLine) i.next();
102             int side = line.getSideThin(x, y);
103             if (lastSide != -10)
104             {
105                 if (side != lastSide)
106                 {
107                     result = false;
108                 }
109             }
110             lastSide = side;
111         }
112         return result;
113     }
114 }