FileDocCategorySizeDatePackage
Polygon.javaAPI DocJava SE 5 API23060Fri Aug 26 14:56:46 BST 2005java.awt

Polygon

public class Polygon extends Object implements Shape, Serializable
The Polygon class encapsulates a description of a closed, two-dimensional region within a coordinate space. This region is bounded by an arbitrary number of line segments, each of which is one side of the polygon. Internally, a polygon comprises of a list of (xy) coordinate pairs, where each pair defines a vertex of the polygon, and two successive pairs are the endpoints of a line that is a side of the polygon. The first and final pairs of (xy) points are joined by a line segment that closes the polygon. This Polygon is defined with an even-odd winding rule. See {@link java.awt.geom.PathIterator#WIND_EVEN_ODD WIND_EVEN_ODD} for a definition of the even-odd winding rule. This class's hit-testing methods, which include the contains, intersects and inside methods, use the insideness definition described in the {@link Shape} class comments.
version
1.26, 07/24/98
author
Sami Shaio
see
Shape
author
Herb Jellinek
since
JDK1.0

Fields Summary
public int
npoints
The total number of points. The value of npoints represents the number of valid points in this Polygon and might be less than the number of elements in {@link #xpoints xpoints} or {@link #ypoints ypoints}. This value can be NULL.
public int[]
xpoints
The array of x coordinates. The number of elements in this array might be more than the number of x coordinates in this Polygon. The extra elements allow new points to be added to this Polygon without re-creating this array. The value of {@link #npoints npoints} is equal to the number of valid points in this Polygon.
public int[]
ypoints
The array of y coordinates. The number of elements in this array might be more than the number of y coordinates in this Polygon. The extra elements allow new points to be added to this Polygon without re-creating this array. The value of npoints is equal to the number of valid points in this Polygon.
protected Rectangle
bounds
Bounds of the polygon. This value can be NULL. Please see the javadoc comments getBounds().
private static final long
serialVersionUID
Constructors Summary
public Polygon()
Creates an empty polygon.


             
      
	xpoints = new int[4];
	ypoints = new int[4];
    
public Polygon(int[] xpoints, int[] ypoints, int npoints)
Constructs and initializes a Polygon from the specified parameters.

param
xpoints an array of x coordinates
param
ypoints an array of y coordinates
param
npoints the total number of points in the Polygon
exception
NegativeArraySizeException if the value of npoints is negative.
exception
IndexOutOfBoundsException if npoints is greater than the length of xpoints or the length of ypoints.
exception
NullPointerException if xpoints or ypoints is null.

    	// Fix 4489009: should throw IndexOutofBoundsException instead
    	// of OutofMemoryException if npoints is huge and > {x,y}points.length
    	if (npoints > xpoints.length || npoints > ypoints.length) {
    		throw new IndexOutOfBoundsException("npoints > xpoints.length || npoints > ypoints.length");
    	}
	this.npoints = npoints;
	this.xpoints = new int[npoints];
	this.ypoints = new int[npoints];
	System.arraycopy(xpoints, 0, this.xpoints, 0, npoints);
	System.arraycopy(ypoints, 0, this.ypoints, 0, npoints);	
    
Methods Summary
public voidaddPoint(int x, int y)
Appends the specified coordinates to this Polygon.

If an operation that calculates the bounding box of this Polygon has already been performed, such as getBounds or contains, then this method updates the bounding box.

param
x the specified x coordinate
param
y the specified y coordinate
see
java.awt.Polygon#getBounds
see
java.awt.Polygon#contains

	if (npoints == xpoints.length) {
	    int tmp[];

	    tmp = new int[npoints * 2];
	    System.arraycopy(xpoints, 0, tmp, 0, npoints);
	    xpoints = tmp;

	    tmp = new int[npoints * 2];
	    System.arraycopy(ypoints, 0, tmp, 0, npoints);
	    ypoints = tmp;
	}
	xpoints[npoints] = x;
	ypoints[npoints] = y;
	npoints++;
	if (bounds != null) {
	    updateBounds(x, y);
	}
    
voidcalculateBounds(int[] xpoints, int[] ypoints, int npoints)

	int boundsMinX = Integer.MAX_VALUE;
	int boundsMinY = Integer.MAX_VALUE;
	int boundsMaxX = Integer.MIN_VALUE;
	int boundsMaxY = Integer.MIN_VALUE;
	
	for (int i = 0; i < npoints; i++) {
	    int x = xpoints[i];
	    boundsMinX = Math.min(boundsMinX, x);
	    boundsMaxX = Math.max(boundsMaxX, x);
	    int y = ypoints[i];
	    boundsMinY = Math.min(boundsMinY, y);
	    boundsMaxY = Math.max(boundsMaxY, y);
	}
	bounds = new Rectangle(boundsMinX, boundsMinY,
			       boundsMaxX - boundsMinX,
			       boundsMaxY - boundsMinY);
    
public booleancontains(java.awt.Point p)
Determines whether the specified {@link Point} is inside this Polygon.

param
p the specified Point to be tested
return
true if the Polygon contains the Point; false otherwise.
see
#contains(double, double)

	return contains(p.x, p.y);
    
public booleancontains(int x, int y)
Determines whether the specified coordinates are inside this Polygon.

param
x the specified x coordinate to be tested
param
y the specified y coordinate to be tested
return
true if this Polygon contains the specified coordinates, (xy); false otherwise.
see
#contains(double, double)
since
JDK1.1

	return contains((double) x, (double) y);
    
public booleancontains(double x, double y)
Determines if the specified coordinates are inside this Polygon. For the definition of insideness, see the class comments of {@link Shape}.

param
x the specified x coordinate
param
y the specified y coordinate
return
true if the Polygon contains the specified coordinates; false otherwise.

        if (npoints <= 2 || !getBoundingBox().contains(x, y)) {
	    return false;
	}
	int hits = 0;

	int lastx = xpoints[npoints - 1];
	int lasty = ypoints[npoints - 1];
	int curx, cury;

	// Walk the edges of the polygon
	for (int i = 0; i < npoints; lastx = curx, lasty = cury, i++) {
	    curx = xpoints[i];
	    cury = ypoints[i];

	    if (cury == lasty) {
		continue;
	    }

	    int leftx;
	    if (curx < lastx) {
		if (x >= lastx) {
		    continue;
		}
		leftx = curx;
	    } else {
		if (x >= curx) {
		    continue;
		}
		leftx = lastx;
	    }

	    double test1, test2;
	    if (cury < lasty) {
		if (y < cury || y >= lasty) {
		    continue;
		}
		if (x < leftx) {
		    hits++;
		    continue;
		}
		test1 = x - curx;
		test2 = y - cury;
	    } else {
		if (y < lasty || y >= cury) {
		    continue;
		}
		if (x < leftx) {
		    hits++;
		    continue;
		}
		test1 = x - lastx;
		test2 = y - lasty;
	    }

	    if (test1 < (test2 / (lasty - cury) * (lastx - curx))) {
		hits++;
	    }
	}

	return ((hits & 1) != 0);
    
public booleancontains(java.awt.geom.Point2D p)
Tests if a specified {@link Point2D} is inside the boundary of this Polygon.

param
p a specified Point2D
return
true if this Polygon contains the specified Point2D; false otherwise.
see
#contains(double, double)

	return contains(p.getX(), p.getY());
    
public booleancontains(double x, double y, double w, double h)
Tests if the interior of this Polygon entirely contains the specified set of rectangular coordinates.

param
x the x coordinate of the top-left corner of the specified set of rectangular coordinates
param
y the y coordinate of the top-left corner of the specified set of rectangular coordinates
param
w the width of the set of rectangular coordinates
param
h the height of the set of rectangular coordinates
return
true if this Polygon entirely contains the specified set of rectangular coordinates; false otherwise
since
1.2

	if (npoints <= 0 || !getBoundingBox().intersects(x, y, w, h)) {
	    return false;
	}

	Crossings cross = getCrossings(x, y, x+w, y+h);
	return (cross != null && cross.covers(y, y+h));
    
public booleancontains(java.awt.geom.Rectangle2D r)
Tests if the interior of this Polygon entirely contains the specified Rectangle2D.

param
r the specified Rectangle2D
return
true if this Polygon entirely contains the specified Rectangle2D; false otherwise.
see
#contains(double, double, double, double)

	return contains(r.getX(), r.getY(), r.getWidth(), r.getHeight());
    
public java.awt.RectanglegetBoundingBox()
Returns the bounds of this Polygon.

return
the bounds of this Polygon.
deprecated
As of JDK version 1.1, replaced by getBounds().

	if (npoints == 0) {
	    return new Rectangle();
	}
	if (bounds == null) {
	    calculateBounds(xpoints, ypoints, npoints);
	}
	return bounds.getBounds();
    
public java.awt.RectanglegetBounds()
Gets the bounding box of this Polygon. The bounding box is the smallest {@link Rectangle} whose sides are parallel to the x and y axes of the coordinate space, and can completely contain the Polygon.

return
a Rectangle that defines the bounds of this Polygon.
since
JDK1.1

	return getBoundingBox();
    
public java.awt.geom.Rectangle2DgetBounds2D()
Returns the high precision bounding box of the {@link Shape}.

return
a {@link Rectangle2D} that precisely bounds the Shape.

	return getBounds();
    
private sun.awt.geom.CrossingsgetCrossings(double xlo, double ylo, double xhi, double yhi)

	Crossings cross = new Crossings.EvenOdd(xlo, ylo, xhi, yhi);
	int lastx = xpoints[npoints - 1];
	int lasty = ypoints[npoints - 1];
	int curx, cury;

	// Walk the edges of the polygon
	for (int i = 0; i < npoints; i++) {
	    curx = xpoints[i];
	    cury = ypoints[i];
	    if (cross.accumulateLine(lastx, lasty, curx, cury)) {
		return null;
	    }
	    lastx = curx;
	    lasty = cury;
	}

	return cross;
    
public java.awt.geom.PathIteratorgetPathIterator(java.awt.geom.AffineTransform at)
Returns an iterator object that iterates along the boundary of this Polygon and provides access to the geometry of the outline of this Polygon. An optional {@link AffineTransform} can be specified so that the coordinates returned in the iteration are transformed accordingly.

param
at an optional AffineTransform to be applied to the coordinates as they are returned in the iteration, or null if untransformed coordinates are desired
return
a {@link PathIterator} object that provides access to the geometry of this Polygon.

	return new PolygonPathIterator(this, at);
    
public java.awt.geom.PathIteratorgetPathIterator(java.awt.geom.AffineTransform at, double flatness)
Returns an iterator object that iterates along the boundary of the Shape and provides access to the geometry of the outline of the Shape. Only SEG_MOVETO, SEG_LINETO, and SEG_CLOSE point types are returned by the iterator. Since polygons are already flat, the flatness parameter is ignored. An optional AffineTransform can be specified in which case the coordinates returned in the iteration are transformed accordingly.

param
at an optional AffineTransform to be applied to the coordinates as they are returned in the iteration, or null if untransformed coordinates are desired
param
flatness the maximum amount that the control points for a given curve can vary from colinear before a subdivided curve is replaced by a straight line connecting the endpoints. Since polygons are already flat the flatness parameter is ignored.
return
a PathIterator object that provides access to the Shape object's geometry.

	return getPathIterator(at);
    
public booleaninside(int x, int y)
Determines whether the specified coordinates are contained in this Polygon.

param
x the specified x coordinate to be tested
param
y the specified y coordinate to be tested
return
true if this Polygon contains the specified coordinates, (xy); false otherwise.
see
#contains(double, double)
deprecated
As of JDK version 1.1, replaced by contains(int, int).

	return contains((double) x, (double) y);
    
public booleanintersects(double x, double y, double w, double h)
Tests if the interior of this Polygon intersects the interior of a specified set of rectangular coordinates.

param
x the x coordinate of the specified rectangular shape's top-left corner
param
y the y coordinate of the specified rectangular shape's top-left corner
param
w the width of the specified rectangular shape
param
h the height of the specified rectangular shape
return
true if the interior of this Polygon and the interior of the specified set of rectangular coordinates intersect each other; false otherwise
since
1.2

	if (npoints <= 0 || !getBoundingBox().intersects(x, y, w, h)) {
	    return false;
	}

	Crossings cross = getCrossings(x, y, x+w, y+h);
	return (cross == null || !cross.isEmpty());
    
public booleanintersects(java.awt.geom.Rectangle2D r)
Tests if the interior of this Polygon intersects the interior of a specified Rectangle2D.

param
r a specified Rectangle2D
return
true if this Polygon and the interior of the specified Rectangle2D intersect each other; false otherwise.

	return intersects(r.getX(), r.getY(), r.getWidth(), r.getHeight());
    
public voidinvalidate()
Invalidates or flushes any internally-cached data that depends on the vertex coordinates of this Polygon. This method should be called after any direct manipulation of the coordinates in the xpoints or ypoints arrays to avoid inconsistent results from methods such as getBounds or contains that might cache data from earlier computations relating to the vertex coordinates.

see
java.awt.Polygon#getBounds
since
1.4

	bounds = null;
    
public voidreset()
Resets this Polygon object to an empty polygon. The coordinate arrays and the data in them are left untouched but the number of points is reset to zero to mark the old vertex data as invalid and to start accumulating new vertex data at the beginning. All internally-cached data relating to the old vertices are discarded. Note that since the coordinate arrays from before the reset are reused, creating a new empty Polygon might be more memory efficient than resetting the current one if the number of vertices in the new polygon data is significantly smaller than the number of vertices in the data from before the reset.

see
java.awt.Polygon#invalidate
since
1.4

	npoints = 0;
	bounds = null;
    
public voidtranslate(int deltaX, int deltaY)
Translates the vertices of the Polygon by deltaX along the x axis and by deltaY along the y axis.

param
deltaX the amount to translate along the x axis
param
deltaY the amount to translate along the y axis
since
JDK1.1

	for (int i = 0; i < npoints; i++) {
	    xpoints[i] += deltaX;
	    ypoints[i] += deltaY;
	}
	if (bounds != null) {
	    bounds.translate(deltaX, deltaY);
	}
    
voidupdateBounds(int x, int y)

	if (x < bounds.x) {
	    bounds.width = bounds.width + (bounds.x - x);
	    bounds.x = x;
	}
	else {
	    bounds.width = Math.max(bounds.width, x - bounds.x);
	    // bounds.x = bounds.x;
	}

	if (y < bounds.y) {
	    bounds.height = bounds.height + (bounds.y - y);
	    bounds.y = y;
	}
	else {
	    bounds.height = Math.max(bounds.height, y - bounds.y);
	    // bounds.y = bounds.y;
	}