gizmoball.shape
Class Polygon

java.lang.Object
  extended by gizmoball.shape.Polygon
All Implemented Interfaces:
Shape

public class Polygon
extends java.lang.Object
implements Shape

This class represents a polygon object, implemented as an array of vertices and an array of edges. Polygon implements the Shape interface, and has factory methods used to generate rectangles, squares, and right triangles. Representation Invariant: vertices and edges have same size: vertices.size() = edges.size(), there are at least three vertices: vertices.size() > 2, edges represents line segments of consecutive vertices: for i = 0 to vertices.size()-2 edges.get(i).getv1().equals(vertices.get(i) && edges.get(i).getv2().equals(vertices.get(i+1) edges.get(edges.size()-1).getv1().equals(vertices.get(edges.size()-1) && edges.get(edges.size()-1).getv2().equals(vertices.get(0) line segments do not intersect: for i = 1 to edges.size()-1 for j = i to edges.size() - 1 !ShapeGeometry.lineSegmentsIntersect(edges.get(i), edges.get(j))

Author:
Eitan Reich

Constructor Summary
Polygon(java.util.List<Vect> vertexList)
          Constructor for polygon taking a list of vertices as input.
 
Method Summary
static boolean canCreatePoly(java.util.List<Vect> vertexList)
           
 boolean containsPoint(Vect pt)
          Returns true iff the specified point is in the interior of the polygon.
 java.util.List<Circle> getCircles()
          Returns a list of Circle objects present in polygon, consisting of circles of radius zero for each of the vertices
 java.util.List<LineSegment> getLineSegments()
          Returns a list of LineSegment objects present in polygon, consisting of edges in polygon
 java.util.List<Vect> getVertices()
           
static Polygon makeRectangle(Vect upperLeft, double width, double height)
          makes a rectangle at the specified location with the specified length and width
static Polygon makeRightTriangle45(Vect upperLeft, double length)
          Constructs a 45-45-90 triangle with the right angle in the bottom left
static Polygon makeSquare(Vect upperLeft, double length)
          Returns a polygon that is a square whose upperleft corner is specified by upperLeft and whose side lengths are specified by length
 Vect maxXY()
          Returns the maximum coordinates for a bounding rectangle of the polygon.
 Vect minXY()
          Returns the minimum coordinates for a bounding rectangle of the polygon.
 Polygon rotate(Angle theta, Vect centerOfRotation)
          Rotates the polygon by an Angle theta around an arbitrary point centerOfRotation and returns the new Polygon object
 Polygon translate(Vect v)
          Translates the polygon by a Vect v and returns the new polygon object
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Polygon

public Polygon(java.util.List<Vect> vertexList)
Constructor for polygon taking a list of vertices as input. The condition is that the line segments constructed by connecting consecutive vertices can not intersect, so that the resulting polygon is a valid shape. The list of vertices must have size greater than 2 so that the polygon is valid.

Parameters:
vertexList - list of Vect objects representing vertices of polygon
Method Detail

canCreatePoly

public static boolean canCreatePoly(java.util.List<Vect> vertexList)
Parameters:
vertexList - list of vertices
Returns:
true iff a polygon can correctly be created from this list of vertices

minXY

public Vect minXY()
Returns the minimum coordinates for a bounding rectangle of the polygon.

Specified by:
minXY in interface Shape
Returns:
the minimum coordinates for a bounding rectangle of the shape

maxXY

public Vect maxXY()
Returns the maximum coordinates for a bounding rectangle of the polygon.

Specified by:
maxXY in interface Shape
Returns:
the maximum coordinates for a bounding rectangle of the shape

containsPoint

public boolean containsPoint(Vect pt)
Returns true iff the specified point is in the interior of the polygon.

Specified by:
containsPoint in interface Shape

getCircles

public java.util.List<Circle> getCircles()
Returns a list of Circle objects present in polygon, consisting of circles of radius zero for each of the vertices

Specified by:
getCircles in interface Shape
Returns:
List list of Circle objects

getLineSegments

public java.util.List<LineSegment> getLineSegments()
Returns a list of LineSegment objects present in polygon, consisting of edges in polygon

Specified by:
getLineSegments in interface Shape
Returns:
List list of LineSegment objects

getVertices

public java.util.List<Vect> getVertices()
Returns:
list of Vect vertices in polygon

rotate

public Polygon rotate(Angle theta,
                      Vect centerOfRotation)
Rotates the polygon by an Angle theta around an arbitrary point centerOfRotation and returns the new Polygon object

Specified by:
rotate in interface Shape
Parameters:
theta - angle to rotate
centerOfRotation - point to rotate around
Returns:
shape rotated around centerOfRotation by angle theta

translate

public Polygon translate(Vect v)
Translates the polygon by a Vect v and returns the new polygon object

Specified by:
translate in interface Shape
Parameters:
v - vector to translate shape by
Returns:
shape translated by v

makeSquare

public static Polygon makeSquare(Vect upperLeft,
                                 double length)
Returns a polygon that is a square whose upperleft corner is specified by upperLeft and whose side lengths are specified by length

Parameters:
upperLeft - Vector representing upper left corner of square
length - double length of sides
Returns:
Polygon object created

makeRightTriangle45

public static Polygon makeRightTriangle45(Vect upperLeft,
                                          double length)
Constructs a 45-45-90 triangle with the right angle in the bottom left

Parameters:
upperLeft - vector representing upper left corner of triangle
length - length of sides (not the hypotenuse)
Returns:
Polygon object created

makeRectangle

public static Polygon makeRectangle(Vect upperLeft,
                                    double width,
                                    double height)
makes a rectangle at the specified location with the specified length and width

Parameters:
upperLeft - vector representing upper left corner of rectangle
width - x dimension of rectangle
height - y dimension of rectangle
Returns:
Polygon object created