Class ep.cg.Polygon
`All Packages  Class Hierarchy  This Package  Previous  Next  Index`

# Class ep.cg.Polygon

```java.lang.Object
|
+----ep.cg.Polygon
```

public class Polygon
extends Object
A basic polygon class which uses our homemade Point class, and therefore is based on floating point math rather than integer math like the polygon class in awt. Points are forced to be in clockwise order.
pts_
Polygon()
This method will throw a NonConvexPolygonException if the point added would make the polygon intersect itself.
This method can be used to add a point without any convexity check.
checkConvex(Point)
public boolean checkConvex(Point p) { int idx = pts_.indexOf(p); int siz = pts_.size(); int previdx, nextidx; if (idx < 0) { return false; } if (siz < 2) return true; if (idx == 0) previdx = siz - 1; else previdx = idx - 1; if (idx == siz -1) nextidx = 0; else nextidx = idx + 1; Point prev = (Point)pts_.elementAt(previdx); Point next = (Point)pts_.elementAt(nextidx); if ( siz > 2) { int prevprevidx = 0, nextnextidx = 0; if (previdx == 0) prevprevidx = siz - 1; else prevprevidx = previdx - 1; if (nextidx == siz - 1) nextnextidx = 0; else nextnextidx = nextidx + 1; Point prevprev = (Point)pts_.elementAt(prevprevidx); Point nextnext = (Point)pts_.elementAt(nextnextidx); Point pold = new Point(prev.x(),prev.y()); pold.minus(prevprev); pold.normalize(); Point pnew = new Point(p.x(), p.y()); pnew.minus(prev); pnew.normalize(); double a = pold.x()*pnew.x() + pold.y()*pnew.y(); System.out.println("Angle is "+ ((Math.acos(a)/(2*Math.PI))*360) ); } return true; }
checkNewConvex(double, double)
This method checks if adding p to the polygon would keep it convex.
checkNewConvex(Point)
delPoint(Point)
Deletes a point.
getIdent()
getPoint(int)
Returns the point i of the polygon, where i e [0,n-1]
setIdent(int)
size()
Returns number of points in poly.
toString()
pts_
```  protected Vector pts_
```
Polygon
```  public Polygon()
```
setIdent
```  public void setIdent(int i)
```
getIdent
```  public int getIdent()
```
```  public void addPoint(Point p) throws NonConvexPolygonException
```
This method will throw a NonConvexPolygonException if the point added would make the polygon intersect itself. The point will be silently ignored if it is colinear with the two last points added, or with the first and last points added.
```  public void addPointNoCheck(Point p)
```
This method can be used to add a point without any convexity check. Use this only when you're _sure_ it will be convex, such as when intersecting two convex polygons...
delPoint
```  public void delPoint(Point p)
```
Deletes a point. Fails silently if the point does not exist. The parameter passed should be a reference to the point to delete.
checkNewConvex
```  public boolean checkNewConvex(double x,
double y)
```
This method checks if adding p to the polygon would keep it convex.
checkNewConvex
```  public boolean checkNewConvex(Point p)
```
checkConvex
```  public boolean checkConvex(Point p)
```
public boolean checkConvex(Point p) { int idx = pts_.indexOf(p); int siz = pts_.size(); int previdx, nextidx; if (idx < 0) { return false; } if (siz < 2) return true; if (idx == 0) previdx = siz - 1; else previdx = idx - 1; if (idx == siz -1) nextidx = 0; else nextidx = idx + 1; Point prev = (Point)pts_.elementAt(previdx); Point next = (Point)pts_.elementAt(nextidx); if ( siz > 2) { int prevprevidx = 0, nextnextidx = 0; if (previdx == 0) prevprevidx = siz - 1; else prevprevidx = previdx - 1; if (nextidx == siz - 1) nextnextidx = 0; else nextnextidx = nextidx + 1; Point prevprev = (Point)pts_.elementAt(prevprevidx); Point nextnext = (Point)pts_.elementAt(nextnextidx); Point pold = new Point(prev.x(),prev.y()); pold.minus(prevprev); pold.normalize(); Point pnew = new Point(p.x(), p.y()); pnew.minus(prev); pnew.normalize(); double a = pold.x()*pnew.x() + pold.y()*pnew.y(); System.out.println("Angle is "+ ((Math.acos(a)/(2*Math.PI))*360) ); } return true; }
getPoint
```  public Point getPoint(int i)
```
Returns the point i of the polygon, where i e [0,n-1]
size
```  public int size()
```
Returns number of points in poly.
toString
```  public String toString()
```
Overrides:
toString in class Object

`All Packages  Class Hierarchy  This Package  Previous  Next  Index`