com.hedgehog.geo.twod.shapes
Class Polygon2D

java.lang.Object
  extended by com.hedgehog.HObject
      extended by com.hedgehog.geo.GeometricObject
          extended by com.hedgehog.geo.twod.GeometricObject2D
              extended by com.hedgehog.geo.twod.shapes.Shape2D
                  extended by com.hedgehog.geo.twod.shapes.Polygon2D
All Implemented Interfaces:
ModelObject, java.lang.Comparable, java.util.Comparator
Direct Known Subclasses:
StraightEdgePolygon2D

public abstract class Polygon2D
extends Shape2D
implements ModelObject

Title: Polygon2D - abstract base class of 2D polygons.

Description: Abstract base class of 2D polygon hierarchy.

Copyright: Copyright (c) Hedgehog Software 2007-2009.

Company: Hedgehog Software.

Since:
1.0
Version:
1.1

Field Summary
protected  java.util.Vector<Point2D> mVertices
          Polygon vertices.
 
Fields inherited from class com.hedgehog.HObject
mID, mName
 
Constructor Summary
Polygon2D()
          Default constructor.
Polygon2D(Point2D v0, Point2D v1, Point2D v2)
          Constructor.
Polygon2D(Point2D v0, Point2D v1, Point2D v2, Point2D v3)
          Constructor.
Polygon2D(Polygon2D object)
          Copy constructor.
Polygon2D(java.util.Vector<Point2D> vertices)
          Constructor.
 
Method Summary
 boolean addVertex(Point2D v)
          Appends the specified vertex to this polygon's vertex vector.
 boolean addVertex(Point2D v, int index)
          Adds/inserts the specified vertex at the specified index.
 boolean addVerticesBetween(int prevVertexIndex, int nextVertexIndex, java.util.Vector<Point2D> newVertices)
          Adds/inserts the specified vector of vertices between prevVertexIndex and nextVertexIndex indices.
 Point2D centroid()
          Returns the vertex centroid of this polygon.
 void clearVertices()
          Clears the vertices vector, thus removing all vertices from this polygon.
 int edgeIndex(Curve2D e, boolean bothSenses, double tolerance)
          Returns the Polygon edge index corresponding to an edge else returns -1 if the edge does not belong to the polygon
 java.util.Vector<Curve2D> edgesInCommon(Polygon2D p, boolean bothSenses, double tolerance)
          Returns the edges common to both Polygon objects.
 java.util.Vector<Curve2D> edgesNotInCommon(Polygon2D p, boolean bothSenses, double tolerance)
          Returns the edges that are not common to both Polygon objects.
 Vector2D edgeVector(int vertexIndex)
          Returns the vector from vertex.index to vertex.index+1.
abstract  Curve2D getEdge(int index)
          Returns the edge at the specified index.
 java.util.Vector<Curve2D> getEdges()
          Returns this polygon's edges.
 Point2D getVertex(int index)
          Returns the vertex at the specified index.
 java.util.Vector<Point2D> getVertices()
          Returns the verties vector.
 boolean isAllEdgesSameLength(double tolerance)
          Tests whether or not this polygon's edges are all the same length, within the specified tolerance.
 boolean isEdge(Curve2D e, boolean bothSenses, double tolerance)
          Tests whether or not the specified edge is an edge of the polygon.
 boolean isEdgeIndex(int edgeIndex)
          Tests whether or not the specified edge index is valid; ie [0:N-1].
 boolean isPentagon()
          Tests whether or not this polygon is a pentagon ; ie it has 5 vertices.
 boolean isQuadrilateral()
          Tests whether or not this polygon is a quadrilateral ; ie it has 4 vertices.
 boolean isTriangle()
          Tests whether or not this polygon is a triangle ; ie it has 3 vertices.
 boolean isValid(double tolerance)
          Tests the validity of this polygon.
 boolean isVertex(Point2D vertex, double tolerance)
          Tests if the specified point is a vertex of this polygon.
 boolean isVertexIndex(int vertexIndex)
          Tests whether or not the specified index is a local index of this polygon; ie vertexIndex < 0 || vertexIndex > mVertices.size()-1.
 int leftEdgeIndex(Curve2D e)
          Returns the 'left' (i.e.
 int leftVertexIndex(int vertexIndex)
          Returns the 'left' (ie the one before) vertex index before the specified vertex index.
 int leftVertexIndex(Point2D v, double tolerance)
          Returns the 'left' (ie the one before) vertex index before a specified vertex.
 Curve2D longestEdge()
          Returns the longest edge.
 double maxVertexXCoordinate()
          Returns the maximum x coordinate of this polygon's vertices.
 double maxVertexYCoordinate()
          Returns the maximum y coordinate of this polygon's vertices.
 Point2D maxXVertex()
          Returns the vertex having the maximum x coordinate.
 int maxXVertexIndex()
          Returns the mVertices vector index of the vertex having the maximum x coordinate.
 Point2D maxYVertex()
          Returns the vertex having the maximum y coordinate.
 int maxYVertexIndex()
          Returns the mVertices vector index of the vertex having the maximum y coordinate.
 void minmax(HDouble xmin, HDouble xmax, HDouble ymin, HDouble ymax)
          Returns the min and max (x,y,z) values of this polygon.
 double minVertexXCoordinate()
          Returns the minimum x coordinate of this polygon's vertices.
 double minVertexYCoordinate()
          Returns the minimum y coordinate of this polygon's vertices.
 Point2D minXVertex()
          Returns the vertex having the minimum x coordinate.
 int minXVertexIndex()
          Returns the mVertices vector index of the vertex having the minimum x coordinate.
 Point2D minYVertex()
          Returns the vertex having the minimum y coordinate.
 int minYVertexIndex()
          Returns the mVertices vector index of the vertex having the minimum y coordinate.
 Point2D nextVertex(int vIndex)
          Returns the vertex after the specified vertex index the ordering of the polygon vertices.
 Point2D nextVertex(Point2D v, double tolerance)
          Returns the vertex after the specified vertex wrt the ordering of the polygon vertices.
 int nextVertexIndex(int vIndex)
          Returns the next vertex index in the inherent order of the vertices in mVertices.
 int numberEdges()
          Returns the number of edges, which is assumed to be the same as the number of vertices.
 int numberVertices()
          Returns the number of vertices.
 java.util.Vector<Point2D> otherVertices(Point2D v, double tolerance)
          Returns the vector of polygon vertices not containing the specified vertex.
 FloatingPointVector parametricValuesOfVertices(Point2D vertex, boolean sameSense, double tolerance)
          Returns the positions of the vertices from a specified vertex, normalized w.r.t the total polygon perimeter.
 double perimeterFromVertexGivenPoint(Point2D vertex, Point2D p, boolean sameSense, double tolerance)
          Returns the perimeter of a polygon from a specified vertex upto the point p.
 Point2D pointFromVertexGivenPerimeter(Point2D vertex, double s, boolean sameSense, double tolerance)
          Returns a point on the boundary of a polygon which is a specified distance from a vertex measured along the polygon boundary.
 boolean pointOnBoundary(Point2D p, double tolerance)
          Tests whether or not the specified point lies on the boundary edges of this polygon.
 int pointOnEdge(Point2D p, double tolerance)
          Tests if the specified point lies on an edge of the polygon to the specified tolerance.
 Point2D previousVertex(int vIndex)
          Returns the vertex before the specified vertex index the ordering of the polygon vertices.
 Point2D previousVertex(Point2D v, double tolerance)
          Returns the vertex before the specified vertex wrt the ordering of the polygon vertices.
 int previousVertexIndex(int vIndex)
          Returns the previous vertex index in the inherent order of the vertices in mVertices.
 boolean removeVertex(int vertexIndex)
          Removes the veretx at the specified index.
 boolean removeVertex(int vIndex, int lIndex, int rIndex)
          Removes a vertex from a polygon with specified left and right vertex indices.
 boolean removeVertex(Point2D v, double tolerance)
          Removes a vertex from this Polygon object.
 boolean removeVertex(Point2D v, Point2D left, Point2D right)
          Removes a vertex from a polygon with specified left and right vertices.
 boolean replaceVertex(int vertexIndex, Point2D newVertex)
          Replaces the vertex at the specified index with the new vertex.
 boolean replaceVertex(Point2D vertex, Point2D newVertex, double tolerance)
          Replaces the specified vertex with the new vertex.
 boolean replaceVerticesBetween(int vi, int vj, ForwardBackwardDirection sense, Point2D newVertex)
          Replaces all vertices between vi and vj with the specified vertex.
 boolean replaceVerticesBetween(int vi, int vj, ForwardBackwardDirection sense, java.util.Vector<Point2D> newVertices)
          Replaces all vertices between vi and vj with the specified vector of vertices.
 int rightEdgeIndex(Curve2D e)
          Returns the 'right' (i.e.
 int rightVertexIndex(int vertexIndex)
          Returns the 'right' (ie the one after) vertex index before the specified vertex index.
 int rightVertexIndex(Point2D v, double tolerance)
          Returns the 'right' (i.e.
 boolean samePolygon(Polygon2D polygon, boolean sameSequence, double tolerance)
          Tests whether or not the specified polygon is the same as this point set.
 boolean setVertex(Point2D vertex, int index)
          Sets the vertex at the specified index to the specified point.
 void setVertices(java.util.Vector<Point2D> vertices)
          Sets the polygon vertices vector.
 boolean shareVertex(Polygon2D p, double tolerance)
          Tests whether or not the specified polygon shares a vertex with this polygon.
 Curve2D shortestEdge()
          Returns the shortest edge.
 Point2DSet toPoint2DSet()
          Returns a Point2DSet object of this polygon's vertices.
 java.lang.String toString()
          Returns a String representation of a Polygon2D object.
 int[] vertexIDs()
          Returns an array of this polygon's vertex IDs.
 int vertexIndex(Point2D vertex, double tolerance)
          Returns the index of the specified vertex.
 ForwardBackwardDirection vertexSense(TripleInteger vertexIndices)
          Returns the sense (wrt this polygon's inherent vertex ordering) of the specified vertex indices.
 Vector2D vertexVector(int vertexIndex)
          Returns the Vector2D equivalent of the specified vertex, with the global origin assumed to be (0,0).
 double[] verticesArray()
          Returns a pointer to an array of this polygon's vertices; required by some rendering software.
 java.util.Vector<Point2D> verticesInCommon(Polygon2D p, double tolerance)
          Returns the vertices that are common to both Polygon objects.
 java.util.Vector<Point2D> verticesNotInCommon(Polygon2D p, double tolerance)
          Returns the vertices that are not common to both Polygon objects.
 
Methods inherited from class com.hedgehog.geo.twod.shapes.Shape2D
area, areaDifference, boundingBox, is2D, is3D, isCurve, isGrid, isMesh, isPoint, isPointSet, isShape, isSurface, perimeter, pointInside, pointOutside, rotate, scale, translate, translate
 
Methods inherited from class com.hedgehog.geo.GeometricObject
objectDimension
 
Methods inherited from class com.hedgehog.HObject
clone, compare, compareTo, copy, equals, getID, getName, hashCode, hasID, hasName, setID, setName
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface com.hedgehog.model.ModelObject
childModelObjects, databaseInsertStatement, databaseUpdateStatement, fromDatabase, fromXMLNode, toX3DNode, toXMLNode
 

Field Detail

mVertices

protected java.util.Vector<Point2D> mVertices
Polygon vertices.

Constructor Detail

Polygon2D

public Polygon2D()
Default constructor. Since the minimum valid polygon is a triangle the constructor allocates a vector of 3 elements with all three vertices equal to (0,0).


Polygon2D

public Polygon2D(java.util.Vector<Point2D> vertices)
          throws java.lang.IllegalArgumentException
Constructor. Assigns the specified vector of vertices to this polygon.

Parameters:
vertices - Vector polygon vertices.
Throws:
java.lang.IllegalArgumentException - Thrown if argument vertices does not have at least 3 elements.

Polygon2D

public Polygon2D(Point2D v0,
                 Point2D v1,
                 Point2D v2)
Constructor. Creates a new triangle from the specified 3 vertices.

Parameters:
v0 - Vertex 0.
v1 - Vertex 1.
v2 - Vertex 2.

Polygon2D

public Polygon2D(Point2D v0,
                 Point2D v1,
                 Point2D v2,
                 Point2D v3)
Constructor. Creates a quadrilateral from the specified 4 vertices.

Parameters:
v0 - Vertex 0.
v1 - Vertex 1.
v2 - Vertex 2.
v3 - Vertex 3.

Polygon2D

public Polygon2D(Polygon2D object)
Copy constructor.

Parameters:
object - Object to copy.
Method Detail

getVertices

public java.util.Vector<Point2D> getVertices()
Returns the verties vector.

Returns:
Polygon vertices vector.

setVertices

public void setVertices(java.util.Vector<Point2D> vertices)
Sets the polygon vertices vector.

Parameters:
vertices - New polygon verties vector.

getVertex

public Point2D getVertex(int index)
                  throws java.lang.IllegalArgumentException
Returns the vertex at the specified index.

Parameters:
index - Vertex index.
Returns:
Vertex at index.
Throws:
java.lang.IllegalArgumentException - Thrown if index is invalid.

setVertex

public boolean setVertex(Point2D vertex,
                         int index)
                  throws java.lang.IllegalArgumentException
Sets the vertex at the specified index to the specified point.

Parameters:
vertex - Point.
index - Index.
Returns:
Logical-true if vertex is set, else logical-false.
Throws:
java.lang.IllegalArgumentException - Thrown if index is invalid.

samePolygon

public boolean samePolygon(Polygon2D polygon,
                           boolean sameSequence,
                           double tolerance)
Tests whether or not the specified polygon is the same as this point set.

Parameters:
polygon - Polygon to compare.
sameSequence - Specifies whether or not an exact match is performed on the vertex sets of both polygons.
tolerance - Required tolerance.
Returns:
Logical-true if the specified polygon is equal to this polygon, else logical-false.

shareVertex

public boolean shareVertex(Polygon2D p,
                           double tolerance)
Tests whether or not the specified polygon shares a vertex with this polygon.

Parameters:
p - Polygon to compare against this polygon.
tolerance - Required tolerance.
Returns:
Logial-true if p shares a vertex with this polygon.

numberEdges

public int numberEdges()
Returns the number of edges, which is assumed to be the same as the number of vertices.

Returns:
The number of edges.

numberVertices

public int numberVertices()
Returns the number of vertices.

Returns:
The number of vertices.

isValid

public boolean isValid(double tolerance)
Tests the validity of this polygon. Tests that the polygon has at least 3 vertices, all vertices are non-null and no points are coincident.

Parameters:
tolerance - Required tolerance.
Returns:
Logical-true if a valid polygon, else logical-false.

isVertex

public boolean isVertex(Point2D vertex,
                        double tolerance)
Tests if the specified point is a vertex of this polygon.

Parameters:
vertex - Point to test.
tolerance - Required tolerance.
Returns:
Logical-true if the specified point is a vertex of this polygon, else logical-false.

isVertexIndex

public boolean isVertexIndex(int vertexIndex)
Tests whether or not the specified index is a local index of this polygon; ie vertexIndex < 0 || vertexIndex > mVertices.size()-1.

Parameters:
vertexIndex - Vertex index to test.
Returns:
Logical-true if the specified index is in range, else logical-false.

isEdge

public boolean isEdge(Curve2D e,
                      boolean bothSenses,
                      double tolerance)
Tests whether or not the specified edge is an edge of the polygon.

Parameters:
e - Edge to test.
bothSenses - If true then both sense of (p0-p1) and (p1-p0) are matched, else just the (p0-p1) sense.
tolerance - Required tolerance.
Returns:
Logical-true if e is an edge of this polygon, else logical-false.

isAllEdgesSameLength

public boolean isAllEdgesSameLength(double tolerance)
Tests whether or not this polygon's edges are all the same length, within the specified tolerance.

Parameters:
tolerance - Required tolerance.
Returns:
Logical-true if this polygon's edges are all the same length, else logical-false.

isTriangle

public boolean isTriangle()
Tests whether or not this polygon is a triangle ; ie it has 3 vertices.

Returns:
Logical-true if this polygon is a triangle, else logical-false.

isQuadrilateral

public boolean isQuadrilateral()
Tests whether or not this polygon is a quadrilateral ; ie it has 4 vertices.

Returns:
Logical-true if this polygon is a quadrilateral, else logical-false.

isPentagon

public boolean isPentagon()
Tests whether or not this polygon is a pentagon ; ie it has 5 vertices.

Returns:
Logical-true if this polygon is a pentagon, else logical-false.

isEdgeIndex

public boolean isEdgeIndex(int edgeIndex)
Tests whether or not the specified edge index is valid; ie [0:N-1].

Parameters:
edgeIndex - Edge index to test.
Returns:
Logical-true if edgeIndex is an edge index of this polygon, else logical-false.

vertexIndex

public int vertexIndex(Point2D vertex,
                       double tolerance)
Returns the index of the specified vertex.

Parameters:
vertex - Polygon vertex.
tolerance - Required tolerance.
Returns:
Index of specified vertex.

vertexVector

public Vector2D vertexVector(int vertexIndex)
                      throws java.lang.IllegalArgumentException
Returns the Vector2D equivalent of the specified vertex, with the global origin assumed to be (0,0). having direction verts[N-1] to v0.

Parameters:
vertexIndex - Vertex index of vertices.
Returns:
The vector from v(0,0) to getVertex(index).
Throws:
java.lang.IllegalArgumentException - Thrown if vertexIndex is out of range [0:N-1].

vertexIDs

public int[] vertexIDs()
Returns an array of this polygon's vertex IDs.

Returns:
An array of this polygon's vertex IDs.

verticesArray

public double[] verticesArray()
Returns a pointer to an array of this polygon's vertices; required by some rendering software. The returned array of vertices is of size 2*numberVertices(), with the (x,y) coordinate values stored contiguously.

Returns:
Array of polygon vertices. Note that the returned is equal to 2 times the number of vertices.

vertexSense

public ForwardBackwardDirection vertexSense(TripleInteger vertexIndices)
                                     throws java.lang.IllegalArgumentException
Returns the sense (wrt this polygon's inherent vertex ordering) of the specified vertex indices. Note1: Three vertex indices are required to fully determine the sense of a polygon's vertices because the vertices form a cyclic chain. Consider the quadrilateral with indices [0,1,2,3]. Indices (1,2) define a forward sense. Indices (1,2) define a backward sense. However, indices (0,3) can be equally interpreted as either a forward or backward sense. Note2: If all three values of the specfied triple are equivalent then a sense of STATIONARY is returned.

Parameters:
vertexIndices - Vertex indices.
Returns:
Sense of the specified vertex indices.
Throws:
java.lang.IllegalArgumentException - Thrown if any of the values of vertexIndices are invalid vertex indices.

addVertex

public boolean addVertex(Point2D v)
Appends the specified vertex to this polygon's vertex vector.

Parameters:
v - Vertex to add.
Returns:
Logical-true if vertex added, else logical-false (if v is null).

addVertex

public boolean addVertex(Point2D v,
                         int index)
Adds/inserts the specified vertex at the specified index.

Parameters:
v - Vertex to add.
index - Index in this polygon's points vector.
Returns:
Logical-true if vertex added, else logical-false (if v is null or index is out of range).

addVerticesBetween

public boolean addVerticesBetween(int prevVertexIndex,
                                  int nextVertexIndex,
                                  java.util.Vector<Point2D> newVertices)
                           throws java.lang.IllegalArgumentException
Adds/inserts the specified vector of vertices between prevVertexIndex and nextVertexIndex indices. Note that this method caters for prevVertexIndex >= nextVertexIndex.

Parameters:
prevVertexIndex - Previous vertex index.
nextVertexIndex - Next vertex index.
newVertices - Vector of new vertices to insert between prevVertexIndex and nextVertexIndex.
Returns:
Logical-true if the vertices are added/inserted, else logical-false.
Throws:
java.lang.IllegalArgumentException - Thrown if prevVertexIndex or nextVertexIndex are invalid indices or newVertices is null or empty.

clearVertices

public void clearVertices()
Clears the vertices vector, thus removing all vertices from this polygon.


removeVertex

public boolean removeVertex(Point2D v,
                            double tolerance)
                     throws java.lang.IllegalArgumentException
Removes a vertex from this Polygon object. Confirms that the polygon is at least a quadrilateral. If this polygon is a triangle then the vertex is not removed since the minimal shape must be a triangle.

Parameters:
v - Vertex to delete.
tolerance - Required tolerance.
Returns:
Logical-true if the vertex was removed, else logical-false.
Throws:
java.lang.IllegalArgumentException - Thrown if v is not a vertex of this polygon.

removeVertex

public boolean removeVertex(int vertexIndex)
Removes the veretx at the specified index. Confirms that the polygon is at least a quadrilateral. If this polygon is a triangle then the vertex is not removed since the minimal shape must be a triangle.

Parameters:
vertexIndex - Index of vertex to remove.
Returns:
Logical-true if the vertex at the specified index is removed, else logical-false.

removeVertex

public boolean removeVertex(Point2D v,
                            Point2D left,
                            Point2D right)
Removes a vertex from a polygon with specified left and right vertices.

Parameters:
v - Vertex to remove.
left - Left vertex.
right - Right vertex.
Returns:
Logical-true if deletion is ok, else logical-false if v, left or right are not polygon vertices or if deleting the vertex would create a polygon of less than 3 edges.

removeVertex

public boolean removeVertex(int vIndex,
                            int lIndex,
                            int rIndex)
Removes a vertex from a polygon with specified left and right vertex indices.

Parameters:
vIndex - Vertex index to remove.
lIndex - Left vertex index to remove.
rIndex - Right vertex index to remove.
Returns:
Logical-true if deletion is ok, else logical-false if v, left or right are not polygon vertices or if deleting the vertex would create a polygon of less than 3 edges.

nextVertex

public Point2D nextVertex(Point2D v,
                          double tolerance)
Returns the vertex after the specified vertex wrt the ordering of the polygon vertices.

Parameters:
v - Vertex of polygon.
tolerance - Required tolerance.
Returns:
Vertex after v, or null if v is null or not a vertex of this polygon.

nextVertex

public Point2D nextVertex(int vIndex)
                   throws java.lang.IllegalArgumentException
Returns the vertex after the specified vertex index the ordering of the polygon vertices.

Parameters:
vIndex - Vertex index.
Returns:
Vertex after vertex associated with vIndex.
Throws:
java.lang.IllegalArgumentException - Thrown if vIndex is out of range.

nextVertexIndex

public int nextVertexIndex(int vIndex)
                    throws java.lang.IllegalArgumentException
Returns the next vertex index in the inherent order of the vertices in mVertices. Caters for circular indexing, in that if vIndex==mVertices.size()-1 then 0 is returned as the next vertex index.

Parameters:
vIndex - Vertex index.
Returns:
Next vertex index.
Throws:
java.lang.IllegalArgumentException - Thrown if vIndex is out of range.

replaceVertex

public boolean replaceVertex(int vertexIndex,
                             Point2D newVertex)
Replaces the vertex at the specified index with the new vertex.

Parameters:
vertexIndex - Veretx index to replace.
newVertex - New vertex.
Returns:
Logical-false if either vertexIndex is invalid or newVertex is null and does not perform the change; otherwise logical-true.

replaceVertex

public boolean replaceVertex(Point2D vertex,
                             Point2D newVertex,
                             double tolerance)
Replaces the specified vertex with the new vertex.

Parameters:
vertex - Vertex to replace.
newVertex - New vertex.
tolerance - Required tolerance.
Returns:
Logical-false if either vertex is not a vertex of this polugon or newVertex is null and does not perform the change; otherwise logical-true.

replaceVerticesBetween

public boolean replaceVerticesBetween(int vi,
                                      int vj,
                                      ForwardBackwardDirection sense,
                                      java.util.Vector<Point2D> newVertices)
                               throws java.lang.IllegalArgumentException
Replaces all vertices between vi and vj with the specified vector of vertices. If vi and vj are adjacent vertex indices (eg v(i) and v(i+1)) then newVertices are inserted between them, otherwise (v(i) and v(j)) all vertices between are replaced by the vector of vertices. Note that vertex replacement follows the inherent ordering of the vertices. For example for a polygon with 6 vertices (indices [0,1,2,3,4,5]) then replaceVerticesBetween(0,2,v) will replace vertex 1 giving a resultant polygon [0,v,2,3,4,5], where as replaceVerticesBetween(0,5,v) will replace vertices 1, 2, 3, and 4 giving a resultant polygon [0,v,5].

Parameters:
vi - Vertex index.
vj - Vertex index.
sense - Sense (forward/backward) between vi and vj.
newVertices - Vector of new vertices to insert between vi nad vj.
Returns:
Logical-true if the vertices are replaced, else logical-false.
Throws:
java.lang.IllegalArgumentException - Thrown if vi or vj are invalid indices, newVertices is null or empty or sense is null or equal to STATIONARY.

replaceVerticesBetween

public boolean replaceVerticesBetween(int vi,
                                      int vj,
                                      ForwardBackwardDirection sense,
                                      Point2D newVertex)
                               throws java.lang.IllegalArgumentException
Replaces all vertices between vi and vj with the specified vertex. If vi and vj are adjacent vertex indices (eg v(i) and v(i+1)) then newVertex is inserted between them, otherwise (v(i) and v(j)) all vertices between are replaced by the single vertex.

Parameters:
vi - Vertex index.
vj - Vertex index.
sense - Sense (forward/backward) between vi and vj
newVertex - New vertex.
Returns:
Logical-true if the vertices are replaced, else logical-false.
Throws:
java.lang.IllegalArgumentException - Thrown if vi or vj are invalid indices, newVertex is null or sense is null or equal to STATIONARY..

previousVertex

public Point2D previousVertex(Point2D v,
                              double tolerance)
Returns the vertex before the specified vertex wrt the ordering of the polygon vertices.

Parameters:
v - Vertex of polygon.
tolerance - Required tolerance.
Returns:
Vertex before v, or null if v is null or not a vertex of this polygon.

previousVertex

public Point2D previousVertex(int vIndex)
                       throws java.lang.IllegalArgumentException
Returns the vertex before the specified vertex index the ordering of the polygon vertices.

Parameters:
vIndex - Vertex index.
Returns:
Vertex before vertex associated with vIndex.
Throws:
java.lang.IllegalArgumentException - Thrown if vIndex is out of range.

previousVertexIndex

public int previousVertexIndex(int vIndex)
                        throws java.lang.IllegalArgumentException
Returns the previous vertex index in the inherent order of the vertices in mVertices. Caters for circular indexing, in that if vIndex==0 then mVertices.size()-1 is returned as the previous vertex index.

Parameters:
vIndex - Vertex index.
Returns:
Previous vertex index.
Throws:
java.lang.IllegalArgumentException - Thrown if vIndex is out of range.

pointOnEdge

public int pointOnEdge(Point2D p,
                       double tolerance)
Tests if the specified point lies on an edge of the polygon to the specified tolerance.

Parameters:
p - Point to test.
tolerance - Required tolerance.
Returns:
Returns the index of the edge that p is on, or -1 if p does not fall on an edge of this polygon.

pointOnBoundary

public boolean pointOnBoundary(Point2D p,
                               double tolerance)
Tests whether or not the specified point lies on the boundary edges of this polygon.

Specified by:
pointOnBoundary in class Shape2D
Parameters:
p - Point to test.
tolerance - Required tolerance.
Returns:
Logical-true if p is on the boundary of this polygon, else logical-false.

otherVertices

public java.util.Vector<Point2D> otherVertices(Point2D v,
                                               double tolerance)
                                        throws java.lang.IllegalArgumentException
Returns the vector of polygon vertices not containing the specified vertex. Tests the (x,y,z) coordinate values and not a pointer comparison.

Parameters:
v - Polygon vertex.
tolerance - Required tolerance.
Returns:
Vector of vertices other than v.
Throws:
java.lang.IllegalArgumentException - Thrown if v is null is not a vertex of this polygon.

edgesInCommon

public java.util.Vector<Curve2D> edgesInCommon(Polygon2D p,
                                               boolean bothSenses,
                                               double tolerance)
                                        throws java.lang.IllegalArgumentException
Returns the edges common to both Polygon objects.

Parameters:
p - Polygon to compare.
bothSenses - If true then both edge sense are compared, else just the inherent sense of the polygon edges.
tolerance - Required tolerance.
Returns:
Vector of edges in common.
Throws:
java.lang.IllegalArgumentException - Thrown if p is null.

edgesNotInCommon

public java.util.Vector<Curve2D> edgesNotInCommon(Polygon2D p,
                                                  boolean bothSenses,
                                                  double tolerance)
                                           throws java.lang.IllegalArgumentException
Returns the edges that are not common to both Polygon objects.

Parameters:
p - Polygon to compare.
bothSenses - If true then both edge sense are compared, else just the inherent sense of the polygon edges.
tolerance - Required tolerance.
Returns:
Vector of edges not in common.
Throws:
java.lang.IllegalArgumentException - Thrown if p is null.

verticesInCommon

public java.util.Vector<Point2D> verticesInCommon(Polygon2D p,
                                                  double tolerance)
                                           throws java.lang.IllegalArgumentException
Returns the vertices that are common to both Polygon objects. The returned vector of vertices is not necessarily contiguous and is this is required use verticesInCommonChain() instead.

Parameters:
p - Polygon to compare against this polygon for common vertices.
tolerance - Required tolerance used by samePoint().
Returns:
Vector of common vertices and empty if no common vertices found.
Throws:
java.lang.IllegalArgumentException - Thrown if p is null.

verticesNotInCommon

public java.util.Vector<Point2D> verticesNotInCommon(Polygon2D p,
                                                     double tolerance)
                                              throws java.lang.IllegalArgumentException
Returns the vertices that are not common to both Polygon objects.

Parameters:
p - Polygon to compare against this polygon for non-common vertices.
tolerance - Require tolerance.
Returns:
Vector of non-common vertices.
Throws:
java.lang.IllegalArgumentException - Thrown if p is null.

edgeVector

public Vector2D edgeVector(int vertexIndex)
                    throws java.lang.IllegalArgumentException
Returns the vector from vertex.index to vertex.index+1. Note the sense of the vector, with the last edge vector having direction verts[N-1] to verts[0].

Parameters:
vertexIndex - Start index of vertices.
Returns:
The vector from vertex(index) to vertex(index+1).
Throws:
java.lang.IllegalArgumentException - Thrown if vertexIndex is out of range [0:N-1].

edgeIndex

public int edgeIndex(Curve2D e,
                     boolean bothSenses,
                     double tolerance)
Returns the Polygon edge index corresponding to an edge else returns -1 if the edge does not belong to the polygon

Parameters:
e - Edge.
bothSenses - Specifies whether or not both senses of the curve are examined to find a match.
tolerance - Required tolerance.
Returns:
Index of specified edge, or -1 if no match is found.

maxXVertex

public Point2D maxXVertex()
Returns the vertex having the maximum x coordinate.

Returns:
The vertex having the maximum x coordinate.

maxXVertexIndex

public int maxXVertexIndex()
Returns the mVertices vector index of the vertex having the maximum x coordinate.

Returns:
The mVertices vector index of the vertex having the maximum x coordinate.

maxYVertex

public Point2D maxYVertex()
Returns the vertex having the maximum y coordinate.

Returns:
The vertex having the maximum y coordinate.

maxYVertexIndex

public int maxYVertexIndex()
Returns the mVertices vector index of the vertex having the maximum y coordinate.

Returns:
The mVertices vector index of the vertex having the maximum y coordinate.

minXVertex

public Point2D minXVertex()
Returns the vertex having the minimum x coordinate.

Returns:
The vertex having the minimum x coordinate.

minXVertexIndex

public int minXVertexIndex()
Returns the mVertices vector index of the vertex having the minimum x coordinate.

Returns:
The mVertices vector index of the vertex having the minimum x coordinate.

minYVertex

public Point2D minYVertex()
Returns the vertex having the minimum y coordinate.

Returns:
The vertex having the minimum y coordinate.

minYVertexIndex

public int minYVertexIndex()
Returns the mVertices vector index of the vertex having the minimum y coordinate.

Returns:
The mVertices vector index of the vertex having the minimum y coordinate.

maxVertexXCoordinate

public double maxVertexXCoordinate()
Returns the maximum x coordinate of this polygon's vertices.

Returns:
Maximum x coordinate of this polygon's vertices.

maxVertexYCoordinate

public double maxVertexYCoordinate()
Returns the maximum y coordinate of this polygon's vertices.

Returns:
Maximum y coordinate of this polygon's vertices.

minVertexXCoordinate

public double minVertexXCoordinate()
Returns the minimum x coordinate of this polygon's vertices.

Returns:
Minimum x coordinate of this polygon's vertices.

minVertexYCoordinate

public double minVertexYCoordinate()
Returns the minimum y coordinate of this polygon's vertices.

Returns:
Minimum y coordinate of this polygon's vertices.

minmax

public void minmax(HDouble xmin,
                   HDouble xmax,
                   HDouble ymin,
                   HDouble ymax)
Returns the min and max (x,y,z) values of this polygon.

Parameters:
xmin - Set upon return to the minimum x coordinate of polygon.
ymin - Set upon return to the minimum y coordinate of polygon.
xmax - Set upon return to the maximum x coordinate of polygon.
ymax - Set upon return to the maximum y coordinate of polygon.

leftVertexIndex

public int leftVertexIndex(int vertexIndex)
Returns the 'left' (ie the one before) vertex index before the specified vertex index.

Parameters:
vertexIndex - Vertex index.
Returns:
The left vertex index. A value of -1 is returned if vertexIndex is not a polygon vertex index.

rightVertexIndex

public int rightVertexIndex(int vertexIndex)
Returns the 'right' (ie the one after) vertex index before the specified vertex index.

Parameters:
vertexIndex - Vertex index.
Returns:
The right vertex index. A value of -1 is returned if vertexIndex is not a polygon vertex index.

leftVertexIndex

public int leftVertexIndex(Point2D v,
                           double tolerance)
Returns the 'left' (ie the one before) vertex index before a specified vertex.

Parameters:
v - Vertex to find left vertex of.
tolerance - Required tolerance.
Returns:
The left vertex index. A value of -1 is returned if v is not a polygon vertex.

rightVertexIndex

public int rightVertexIndex(Point2D v,
                            double tolerance)
Returns the 'right' (i.e. the one after) vertex index after a specified vertex.

Parameters:
v - Vertex to find right vertex of.
tolerance - Required tolerance.
Returns:
The right vertex index. A value of -1 is returned if v is not a polygon vertex.

leftEdgeIndex

public int leftEdgeIndex(Curve2D e)
Returns the 'left' (i.e. the one before) edge index before a specified edge.

Parameters:
e - Curve to find the left index of.
Returns:
Left edge index. A value of -1 is returned if the left index is not found.

rightEdgeIndex

public int rightEdgeIndex(Curve2D e)
Returns the 'right' (i.e. the one after) edge index after the specified edge.

Parameters:
e - Curve to find the right index of.
Returns:
Right edge index. A value of -1 is returned if the right index is not found.

longestEdge

public Curve2D longestEdge()
                    throws InvalidObjectException
Returns the longest edge.

Returns:
The longest edge.
Throws:
InvalidObjectException - Thrown if this polygon has no edges.

shortestEdge

public Curve2D shortestEdge()
                     throws InvalidObjectException
Returns the shortest edge.

Returns:
The shortest edge.
Throws:
InvalidObjectException - Thrown if this polygon has no edges.

getEdges

public java.util.Vector<Curve2D> getEdges()
Returns this polygon's edges. Uses the overridden getEdge() method.

Returns:
Vector of this polygon's edges.

parametricValuesOfVertices

public FloatingPointVector parametricValuesOfVertices(Point2D vertex,
                                                      boolean sameSense,
                                                      double tolerance)
                                               throws java.lang.IllegalArgumentException
Returns the positions of the vertices from a specified vertex, normalized w.r.t the total polygon perimeter.

Parameters:
vertex - Source vertex from which to measure positions of other vertices.
sameSense - If true then vertices traversed in same order as internal vector, else in opposite direction.
tolerance - Required tolerance.
Returns:
Vector of parametric positions of this polygon's vertices from the specified vertex.
Throws:
java.lang.IllegalArgumentException - Thrown if vertex is not a vertex of this polygon.

perimeterFromVertexGivenPoint

public double perimeterFromVertexGivenPoint(Point2D vertex,
                                            Point2D p,
                                            boolean sameSense,
                                            double tolerance)
                                     throws java.lang.IllegalArgumentException
Returns the perimeter of a polygon from a specified vertex upto the point p.

Parameters:
vertex - Vertex of polygon from which to measure perimeter from.
p - Point on polygon boundary up to which perimeter is measured.
sameSense - If true then vertices traversed in same order as internal vector, else in opposite direction.
tolerance - Required tolerance.
Returns:
Perimeter from vertex to point.
Throws:
java.lang.IllegalArgumentException - Thrown if vertex is not a vertex of this polygon.

pointFromVertexGivenPerimeter

public Point2D pointFromVertexGivenPerimeter(Point2D vertex,
                                             double s,
                                             boolean sameSense,
                                             double tolerance)
                                      throws java.lang.IllegalArgumentException
Returns a point on the boundary of a polygon which is a specified distance from a vertex measured along the polygon boundary.

Parameters:
vertex - Source vertex from which to measure.
s - Distance from vertex.
sameSense - If true then vertices traversed in same order as internal vector, else in opposite direction.
tolerance - Required tolerance.
Returns:
Returns the point on this polygon's boundary distance s from the specified vertex in the specified sense.
Throws:
java.lang.IllegalArgumentException - Thrown if vertex is not a vertex of this polygon or s is either less than zero or greater than the polygon perimeter.

toString

public java.lang.String toString()
Returns a String representation of a Polygon2D object.

Overrides:
toString in class java.lang.Object
Returns:
A String representation of a Polygon2D object.

toPoint2DSet

public Point2DSet toPoint2DSet()
Returns a Point2DSet object of this polygon's vertices.

Returns:
A Point2DSet object of this polygon's vertices.

centroid

public Point2D centroid()
Returns the vertex centroid of this polygon.

Specified by:
centroid in class Shape2D
Returns:
Centroid of vertices.

getEdge

public abstract Curve2D getEdge(int index)
                         throws java.lang.IllegalArgumentException
Returns the edge at the specified index.

Parameters:
index - Edge index.
Returns:
Edge at specified index.
Throws:
java.lang.IllegalArgumentException - Thrown if index is invalid.