edu.mit.sketch.geom
Class Polygon

java.lang.Object
  |
  +--java.awt.Polygon
        |
        +--edu.mit.sketch.geom.Polygon
All Implemented Interfaces:
GeometricObject, Paintable, Serializable, Shape, Terminal, Translatable

public class Polygon
extends Polygon
implements GeometricObject, Serializable

This class represents a polygon described by its vertices. The polygon should be closed explicitly if needed.

See Also:
Serialized Form

Field Summary
 Graphics graphics
          Graphics context for this Geometric object.
 long time_stamp
          Time stamp of this object.
 
Fields inherited from class java.awt.Polygon
bounds, npoints, xpoints, ypoints
 
Fields inherited from interface edu.mit.sketch.geom.GeometricObject
CIRCLE, ELLIPSE, LINE, POINT, POLYGON, RECTANGLE, SQUARE
 
Constructor Summary
Polygon()
          The constructor.
Polygon(int[] xpoints, int[] ypoints, int npoints)
          The constructor.
Polygon(Point[] points)
          The constructor.
Polygon(Polygon p)
          The constructor.
Polygon(Polygon p)
          The constructor.
 
Method Summary
 void addPointDouble(double x, double y)
          add point for doubles
 int closestNeighborIndex(Point p)
          Returns the index of the vertices that is closest to point p.
 void combineEndPoints(double error)
          Combines the ends of the polygon if they are sufficiently close.
 boolean containsGeometricObject(GeometricObject object)
          Returns false if the argument is not completely inside this object.
 boolean containsGeometricObjects(GeometricObject[] objects)
          Returns false if the objects in the input array are completely inside this object.
 void copyVerticesFrom(Polygon p)
          Copy the vertices.
 void drawAngleAt(int i, Point reference_point)
          Draws an arc (angle) on the ith vertex (start from 0) using the current graphics context.
 double[] getAbsoluteAngles()
          Returns an array containing the angles that each edge makes with the x axis.
 double[] getAbsolutePositiveAngles()
          Returns an array containing the angles that each edge makes with the x axis.
 double getAngle(int n)
          get the nth angle as a positive value between 0 and 2*PI
 Polygon getDataPoints()
          This method is used to get the original data points that forms this GeometricObject
 Line getEdge(int n)
          This method returns the nth edge of this Polygon.
 Rectangle getHorizontalBounds()
          Supplied for completeness.
 double[] getMajorAngles()
          Return the major angles in this polygon
 Polygon getPolygonalBounds()
          This method should return a polygon that fully contains the current object.
 Rectangle getRectangularBounds()
          Supplied for completeness.
 Rectangle getRectangularBounds(int steps)
          Supplied for completeness.
 long getTimeStamp()
          Returns the time stamp of the current Terminal
 String getType()
          Implement GeometricObject
 void normalize(double[] angle_set)
          Normalize the polygon edges so that the angles they make with the x axis are chosen from the given angle set.
 void paint()
          Draw the object
 void paint(Graphics g)
          Draw the object
 void paintOriginal(Graphics g)
          This method is used to paint the original data points that forms this GeometricObject
 Point pointAt(int i)
          Returns the ith point in this Polygon
 boolean pointIsOn(Point p, int radius)
          Returns true if the point is within +-radius distance from the curve defining the object.
 boolean pointIsOnOriginal(Point p, int radius)
          Returns true if the point is within +-radius distance from the original curve defining the object.
 void rotate(double theta)
          This method rotates the polygon by theta radians CCW.
 void rotateAboutCOM(double theta)
          This method rotates the polygon by theta radians CCW.
 void rotateAboutOrigin(double theta)
          This method rotates the polygon by theta radians CCW.
 void setDataPoints(Polygon points)
          This method is used to set the original data points that forms this GeometricObject
 void setGraphicsContext(Graphics g)
          Set graphics context for this Geometric object.
 void setTimeStamp(long time_stamp)
          Sets the time stamp of the current Terminal
 int spatialRelation(GeometricObject object)
          This method should return the spatial relation of the input parameter with respect to this object.
 String toString()
          Override toString
 boolean touches(GeometricObject object)
          This method should return true if the input objects touch.
 void translate(double x, double y)
          Add the arguments to the (x, y) position of the object.
 boolean tryCombining(Object o, int tolerance)
          Try combining the input object with this Polygon.
 
Methods inherited from class java.awt.Polygon
addPoint, contains, contains, contains, contains, contains, contains, getBoundingBox, getBounds, getBounds2D, getPathIterator, getPathIterator, inside, intersects, intersects, translate
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

time_stamp

public long time_stamp
Time stamp of this object.

graphics

public transient Graphics graphics
Graphics context for this Geometric object.
Constructor Detail

Polygon

public Polygon()
The constructor.

Polygon

public Polygon(Polygon p)
The constructor.

Polygon

public Polygon(Point[] points)
The constructor.

Polygon

public Polygon(int[] xpoints,
               int[] ypoints,
               int npoints)
The constructor.

Polygon

public Polygon(Polygon p)
The constructor.
Method Detail

copyVerticesFrom

public void copyVerticesFrom(Polygon p)
Copy the vertices. The input should be the same length as this polygon.

getType

public String getType()
Implement GeometricObject
Specified by:
getType in interface Paintable

addPointDouble

public void addPointDouble(double x,
                           double y)
add point for doubles

toString

public String toString()
Override toString
Overrides:
toString in class Object

paint

public void paint()
Draw the object
Specified by:
paint in interface Paintable

paintOriginal

public void paintOriginal(Graphics g)
This method is used to paint the original data points that forms this GeometricObject
Specified by:
paintOriginal in interface Paintable

paint

public void paint(Graphics g)
Draw the object
Specified by:
paint in interface Paintable

pointIsOn

public boolean pointIsOn(Point p,
                         int radius)
Returns true if the point is within +-radius distance from the curve defining the object. Returns false o/w.
Specified by:
pointIsOn in interface Translatable

pointIsOnOriginal

public boolean pointIsOnOriginal(Point p,
                                 int radius)
Returns true if the point is within +-radius distance from the original curve defining the object. Returns false o/w.
Specified by:
pointIsOnOriginal in interface Translatable

translate

public void translate(double x,
                      double y)
Add the arguments to the (x, y) position of the object.
Specified by:
translate in interface Translatable

closestNeighborIndex

public int closestNeighborIndex(Point p)
Returns the index of the vertices that is closest to point p.

pointAt

public Point pointAt(int i)
Returns the ith point in this Polygon

drawAngleAt

public void drawAngleAt(int i,
                        Point reference_point)
Draws an arc (angle) on the ith vertex (start from 0) using the current graphics context. Point p is used to determine which angle to paint ( internal or external ). Cosine theorem is applied here.

setGraphicsContext

public void setGraphicsContext(Graphics g)
Set graphics context for this Geometric object. Must be set at least once before doing any drawing.
Specified by:
setGraphicsContext in interface Paintable

touches

public boolean touches(GeometricObject object)
This method should return true if the input objects touch. It should be optimized making use of the object type information.
Specified by:
touches in interface GeometricObject

getHorizontalBounds

public Rectangle getHorizontalBounds()
Supplied for completeness. Returns the bounding box with the smallest perimeter for steps = 9

getRectangularBounds

public Rectangle getRectangularBounds()
Supplied for completeness. Returns the bounding box with the smallest perimeter for steps = 9
Specified by:
getRectangularBounds in interface GeometricObject

containsGeometricObject

public boolean containsGeometricObject(GeometricObject object)
Returns false if the argument is not completely inside this object. Return true O/W.
Specified by:
containsGeometricObject in interface GeometricObject

getRectangularBounds

public Rectangle getRectangularBounds(int steps)
Supplied for completeness. Returns the bounding box with the smallest perimeter for step angles of Math.PI/(2*steps) Should be optimized further. Should always return a Rectangle with an angle between 0.0 and Math.PI/2

getEdge

public Line getEdge(int n)
This method returns the nth edge of this Polygon.

getAngle

public double getAngle(int n)
get the nth angle as a positive value between 0 and 2*PI

getPolygonalBounds

public Polygon getPolygonalBounds()
This method should return a polygon that fully contains the current object. The polygon is open. In order to close it, the last point should be the same as the first one.
Specified by:
getPolygonalBounds in interface GeometricObject

rotateAboutOrigin

public void rotateAboutOrigin(double theta)
This method rotates the polygon by theta radians CCW.

rotateAboutCOM

public void rotateAboutCOM(double theta)
This method rotates the polygon by theta radians CCW.

rotate

public void rotate(double theta)
This method rotates the polygon by theta radians CCW.

spatialRelation

public int spatialRelation(GeometricObject object)
This method should return the spatial relation of the input parameter with respect to this object. see the SpatialRelation class for a detailed list of possible spatial relations. Another version of this method should be implemented for handling spatial relations where a rotated coordinate system is to be used.
Specified by:
spatialRelation in interface GeometricObject

setTimeStamp

public void setTimeStamp(long time_stamp)
Sets the time stamp of the current Terminal
Specified by:
setTimeStamp in interface Terminal

getTimeStamp

public long getTimeStamp()
Returns the time stamp of the current Terminal
Specified by:
getTimeStamp in interface Terminal

setDataPoints

public void setDataPoints(Polygon points)
This method is used to set the original data points that forms this GeometricObject
Specified by:
setDataPoints in interface GeometricObject

getDataPoints

public Polygon getDataPoints()
This method is used to get the original data points that forms this GeometricObject
Specified by:
getDataPoints in interface GeometricObject

containsGeometricObjects

public boolean containsGeometricObjects(GeometricObject[] objects)
Returns false if the objects in the input array are completely inside this object. Return true O/W.
Specified by:
containsGeometricObjects in interface GeometricObject

getAbsoluteAngles

public double[] getAbsoluteAngles()
Returns an array containing the angles that each edge makes with the x axis.

getAbsolutePositiveAngles

public double[] getAbsolutePositiveAngles()
Returns an array containing the angles that each edge makes with the x axis.

tryCombining

public boolean tryCombining(Object o,
                            int tolerance)
Try combining the input object with this Polygon. Return true is successful, false O/W. The input object may be a Line or a Polygon Two Polygons are combinable if their end points are sufficiently close to each other (determined by the tolerance).

combineEndPoints

public void combineEndPoints(double error)
Combines the ends of the polygon if they are sufficiently close.

getMajorAngles

public double[] getMajorAngles()
Return the major angles in this polygon

normalize

public void normalize(double[] angle_set)
Normalize the polygon edges so that the angles they make with the x axis are chosen from the given angle set.