| Method from java.awt.Polygon Detail: |
public void addPoint(int x,
int y) {
if (npoints >= xpoints.length || npoints >= ypoints.length) {
int newLength = npoints * 2;
// Make sure that newLength will be greater than MIN_LENGTH and
// aligned to the power of 2
if (newLength < MIN_LENGTH) {
newLength = MIN_LENGTH;
} else if ((newLength & (newLength - 1)) != 0) {
newLength = Integer.highestOneBit(newLength);
}
xpoints = Arrays.copyOf(xpoints, newLength);
ypoints = Arrays.copyOf(ypoints, newLength);
}
xpoints[npoints] = x;
ypoints[npoints] = y;
npoints++;
if (bounds != null) {
updateBounds(x, 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. |
void calculateBounds(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 boolean contains(Point p) {
return contains(p.x, p.y);
}
Determines whether the specified Point is inside this
Polygon. |
public boolean contains(Point2D p) {
return contains(p.getX(), p.getY());
}
|
public boolean contains(Rectangle2D r) {
return contains(r.getX(), r.getY(), r.getWidth(), r.getHeight());
}
|
public boolean contains(int x,
int y) {
return contains((double) x, (double) y);
}
Determines whether the specified coordinates are inside this
Polygon.
|
public boolean contains(double x,
double y) {
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 boolean contains(double x,
double y,
double w,
double h) {
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 Rectangle getBoundingBox() {
if (npoints == 0) {
return new Rectangle();
}
if (bounds == null) {
calculateBounds(xpoints, ypoints, npoints);
}
return bounds.getBounds();
} Deprecated! As - of JDK version 1.1,
replaced by getBounds().
Returns the bounds of this Polygon. |
public Rectangle getBounds() {
return getBoundingBox();
}
Gets the bounding box of this Polygon.
The bounding box is the smallest Rectangle whose
sides are parallel to the x and y axes of the
coordinate space, and can completely contain the Polygon. |
public Rectangle2D getBounds2D() {
return getBounds();
}
|
public PathIterator getPathIterator(AffineTransform at) {
return new PolygonPathIterator(this, 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
AffineTransform can be specified so that the coordinates
returned in the iteration are transformed accordingly. |
public PathIterator getPathIterator(AffineTransform at,
double flatness) {
return getPathIterator(at);
}
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. |
public boolean inside(int x,
int y) {
return contains((double) x, (double) y);
} Deprecated! As - of JDK version 1.1,
replaced by contains(int, int).
Determines whether the specified coordinates are contained in this
Polygon. |
public boolean intersects(Rectangle2D r) {
return intersects(r.getX(), r.getY(), r.getWidth(), r.getHeight());
}
|
public boolean intersects(double x,
double y,
double w,
double h) {
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 void invalidate() {
bounds = null;
}
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. |
public void reset() {
npoints = 0;
bounds = null;
}
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. |
public void translate(int deltaX,
int deltaY) {
for (int i = 0; i < npoints; i++) {
xpoints[i] += deltaX;
ypoints[i] += deltaY;
}
if (bounds != null) {
bounds.translate(deltaX, deltaY);
}
}
Translates the vertices of the Polygon by
deltaX along the x axis and by
deltaY along the y axis. |
void updateBounds(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;
}
}
|