auc.geom
Class Polygon2D

java.lang.Object
  |
  +--java.awt.Polygon
        |
        +--auc.geom.Polygon2D
All Implemented Interfaces:
java.io.Serializable, java.awt.Shape

public class Polygon2D
extends java.awt.Polygon

A class for the polygon representation with the real coordinates and basic polygon geometry functions.

See Also:
Serialized Form

Field Summary
protected  Vector2D max
          The maximum coordinates of the minimal rectangle that covers the polygon.
protected  Vector2D min
          The minimum coordinates of the minimal rectangle that covers the polygon.
protected  java.util.Vector vertices
          The list of vertices.
 
Fields inherited from class java.awt.Polygon
bounds, npoints, xpoints, ypoints
 
Constructor Summary
Polygon2D()
          Constructs empty polygon instance.
Polygon2D(int[] xpoints, int[] ypoint, int npoints)
          Constructs a polygon from the list of coordinates.
Polygon2D(Polygon2D poly)
          Constructs a copy of the given polygon instance.
 
Method Summary
 void addPoint(int x, int y)
          Adds a vertex to this polygon.
 boolean addVertex(Vector2D p)
          Adds a new vertex to the polygon at the end of the vertices list.
 java.util.Vector concavities()
          Calculates the concavities in this polygon.
 boolean contains(double x, double y)
          Evaluates whether the given point is inside this polygon.
 boolean contains(int x, int y)
          Evaluates whether the given point is inside this polygon.
 boolean contains(Polygon2D p)
          Checks whether this polygon contains polygon p.
 boolean contains(Vector2D p)
          The alternative way of checking the point containment.
 Polygon2D convex()
          Calculates the minimal convex polygon which contains this polygon.
 boolean equals(java.lang.Object obj)
          Calculates whether this polygon is equal to the given Polygon2D.
 int findVertex(double x, double y)
          Searches for the vertex in the list of polygon vertices.
 Vector2D getMax()
           
 double getMaxX()
           
 double getMaxY()
           
 Vector2D getMin()
           
 double getMinX()
           
 double getMinY()
           
 java.util.Iterator getVertexIterator()
          Returns the vertex list iterator to access each vertex.
 int getVerticesCount()
          Returns the number of vertices in this polygon.
static Vector2D intersection(Vector2D l1p1, Vector2D l1p2, Vector2D l2p1, Vector2D l2p2)
          Finds the intersection point of two segments.
 boolean intersects(Vector2D p1, Vector2D p2)
          Tells whether the segment crosses the polygon.
static void main(java.lang.String[] arg)
          Test for Polygon2D#convex() and Polygon2D#concavities() functions.
 double orientation()
          Calculates the orientation of this polygon.
 boolean removeVertex(double x, double y)
          Removes a vertex of given coordinates from polygon structure.
 boolean removeVertex(Vector2D v)
          Alternative way to remove the vertex.
 java.lang.String toString()
          The string representation is enumeration of vertices string representations.
 
Methods inherited from class java.awt.Polygon
contains, contains, contains, contains, getBoundingBox, getBounds, getBounds2D, getPathIterator, getPathIterator, inside, intersects, intersects, translate
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

vertices

protected java.util.Vector vertices
The list of vertices. Each vertex is represented by the vector object of Vector2D class.
See Also:
Vector2D

min

protected Vector2D min
The minimum coordinates of the minimal rectangle that covers the polygon. This field is calculated during polygon creation (vertex addition). The default value for min is Vector2D(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY) if no vertices were added.
See Also:
getMin()

max

protected Vector2D max
The maximum coordinates of the minimal rectangle that covers the polygon. This field is calculated during polygon creation (vertex addition). The default value for min is Vector2D(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY) if no vertices were added.
See Also:
getMax()
Constructor Detail

Polygon2D

public Polygon2D()
Constructs empty polygon instance.

Polygon2D

public Polygon2D(Polygon2D poly)
Constructs a copy of the given polygon instance.
Parameters:
poly - a reference to a given polygon.

Polygon2D

public Polygon2D(int[] xpoints,
                 int[] ypoint,
                 int npoints)
Constructs a polygon from the list of coordinates. It is made to be compatible with java.awt.Polygon.
Parameters:
xpoints - x-coordinate of the point
ypoints - y-coordinate of the point
npoints - number of oints to include into polygon.
See Also:
Polygon.Polygon(int[],int[],int)
Method Detail

getVerticesCount

public int getVerticesCount()
Returns the number of vertices in this polygon.
Returns:
the count of vertices in this polygon.

addPoint

public void addPoint(int x,
                     int y)
Adds a vertex to this polygon. This method is made for compatibility with java.awt.Polygon.
Overrides:
addPoint in class java.awt.Polygon
Parameters:
x - the x-coordinate of the vertex.
y - the y-coordinate of the vertex.
See Also:
Polygon.addPoint(int, int)

addVertex

public boolean addVertex(Vector2D p)
Adds a new vertex to the polygon at the end of the vertices list.
Parameters:
p - The vertex point to be added.
Returns:
true if successful (as java.util.Vector.add(Object)).

getVertexIterator

public java.util.Iterator getVertexIterator()
Returns the vertex list iterator to access each vertex.
Returns:
the iterator from vertex list.

findVertex

public int findVertex(double x,
                      double y)
Searches for the vertex in the list of polygon vertices.
Parameters:
x - The x-axis coordinate of vertex.
y - The y-axis coordinate of vertex.
Returns:
The index in the vertices list. -1 if not found.

removeVertex

public boolean removeVertex(Vector2D v)
Alternative way to remove the vertex.
Parameters:
v - vector that points to one of the polygon vertices.
Returns:
the result of operation.
See Also:
removeVertex(double, double)

removeVertex

public boolean removeVertex(double x,
                            double y)
Removes a vertex of given coordinates from polygon structure.
Parameters:
x - The x-axis coordinate of vertex.
y - The y-axis coordinate of vertex.
precision - The precision parameter to test the vector equality.
Returns:
true if found and removed, false if not found.
See Also:
removeVertex(Vector2D)

intersection

public static Vector2D intersection(Vector2D l1p1,
                                    Vector2D l1p2,
                                    Vector2D l2p1,
                                    Vector2D l2p2)
Finds the intersection point of two segments.
Parameters:
s1p1 - the beginning point of the first segment
s1p2 - the ending point of the first segment
s2p1 - the beginning point of the second segment
s2p2 - the ending point of the second segment
Returns:
the intersection point if segments intersect with each other, Vector2D(Double.INFINITY, Double.INFINITY) if segments have more than one common point or null if segments do not have common points at all.

intersects

public boolean intersects(Vector2D p1,
                          Vector2D p2)
Tells whether the segment crosses the polygon.
Parameters:
p1 - the beginning point of the segment
p2 - the ending point of the segment
Returns:
true if segment intersects at least one of the polygon edges, or false if segment does not intersect any of the polygon edges.

contains

public boolean contains(double x,
                        double y)
Evaluates whether the given point is inside this polygon.
Overrides:
contains in class java.awt.Polygon
Parameters:
x - The x-axis coordinate of the point.
y - The y-axis coordinate of the point.
Returns:
true if point(x,y) belongs to polygon, false otherwise.

contains

public boolean contains(int x,
                        int y)
Evaluates whether the given point is inside this polygon. Overrides the java.awt.Polygon method and addresses the operation to Polygon2D.contains(double, double).
Overrides:
contains in class java.awt.Polygon
Parameters:
x - the x-coordinate of the point.
y - the y-coordinate of the point.
Returns:
true if the point is inside of this polygon, false otherwise.
See Also:
contains(double, double)

orientation

public double orientation()
Calculates the orientation of this polygon. The orientation can be positive or negative depending on the sequence of the vertices. The orientation is negative if the vertices are in clockwise order and negative if the vertices are ordered in oposite direction.
Returns:
positive double if the orientation is positive, negative if orientation is negative, and 0 if the polygon has zero area of some of the edges crosses.

concavities

public java.util.Vector concavities()
Calculates the concavities in this polygon. Concavities are also of type Polygon2D.
Returns:
the list of concavities.

convex

public Polygon2D convex()
Calculates the minimal convex polygon which contains this polygon.
Returns:
the convex polygon without concavities.

contains

public boolean contains(Vector2D p)
The alternative way of checking the point containment.
Parameters:
p - the vector which points to the desired point.
Returns:
true if the point is inside this polygon.
See Also:
contains(double, double)

contains

public boolean contains(Polygon2D p)
Checks whether this polygon contains polygon p. Actually it just checks if all vertices of p are inside of this polygon, so don't relly on this method much.
Parameters:
p - The polygon for checking.
Returns:
true if p is inside this polygon, false otherwise.

getMinX

public double getMinX()
Returns:
The minimum x-axis coordinate of the polygon vertices.
See Also:
min

getMaxX

public double getMaxX()
Returns:
The maximum x-axis coordinate of the polygon vertices.
See Also:
max

getMinY

public double getMinY()
Returns:
The minimum y-axis coordinate of the polygon vertices.
See Also:
min

getMaxY

public double getMaxY()
Returns:
The maximum y-axis coordinate of the polygon vertices.
See Also:
max

getMin

public Vector2D getMin()
Returns:
The minimum point of the rectangle that covers the polygon.
See Also:
min

getMax

public Vector2D getMax()
Returns:
The maximum point of the rectangle that covers the polygon.
See Also:
max

equals

public boolean equals(java.lang.Object obj)
Calculates whether this polygon is equal to the given Polygon2D.
Overrides:
equals in class java.lang.Object
Parameters:
obj - the polygon object.
Returns:
true if obj is of type Polygon2D, has the same number of vertices and contains all of this polygon vertices.

toString

public java.lang.String toString()
The string representation is enumeration of vertices string representations.
Overrides:
toString in class java.lang.Object
Returns:
the string representation of this polygon.

main

public static void main(java.lang.String[] arg)
Test for Polygon2D#convex() and Polygon2D#concavities() functions.
Parameters:
arg - the list of arguments from command line (not used).