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.

Variable Index

 o pts_

Constructor Index

 o Polygon()

Method Index

 o addPoint(Point)
This method will throw a NonConvexPolygonException if the point added would make the polygon intersect itself.
 o addPointNoCheck(Point)
This method can be used to add a point without any convexity check.
 o 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; }
 o checkNewConvex(double, double)
This method checks if adding p to the polygon would keep it convex.
 o checkNewConvex(Point)
 o delPoint(Point)
Deletes a point.
 o getIdent()
 o getPoint(int)
Returns the point i of the polygon, where i e [0,n-1]
 o setIdent(int)
 o size()
Returns number of points in poly.
 o toString()

Variables

 o pts_
  protected Vector pts_

Constructors

 o Polygon
  public Polygon()

Methods

 o setIdent
  public void setIdent(int i)
 o getIdent
  public int getIdent()
 o addPoint
  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.
 o addPointNoCheck
  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...
 o 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.
 o checkNewConvex
  public boolean checkNewConvex(double x,
                                double y)
This method checks if adding p to the polygon would keep it convex.
 o checkNewConvex
  public boolean checkNewConvex(Point p)
 o 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; }
 o getPoint
  public Point getPoint(int i)
Returns the point i of the polygon, where i e [0,n-1]
 o size
  public int size()
Returns number of points in poly.
 o toString
  public String toString()
Overrides:
toString in class Object

All Packages  Class Hierarchy  This Package  Previous  Next  Index