public final class CBCAPolygon
extends java.awt.Polygon
Ref: Zalik & Kolingerova, "A cell-based point-in-polygon algorithm suitable for large sets of points", Computers & Geosciences 27 (2001) 1135-1145.
Constructor and Description |
---|
CBCAPolygon()
Creates an empty polygon.
|
CBCAPolygon(int[] xPoints,
int[] yPoints,
int nPoints)
Constructs and initializes a
Polygon from the specified
parameters. |
Modifier and Type | Method and Description |
---|---|
void |
addPoint(int x,
int y)
Appends the specified coordinates to this
Polygon . |
boolean |
contains(double x,
double y)
Determines if the specified coordinates are inside this
Polygon . |
static CBCAPolygon |
createConvexBoundaryPolygon(java.awt.Point[] points)
Find the convex hull polygon around an arbitrary unordered array of points.
|
static boolean |
doLineSegmentsIntersect(int ax1,
int ay1,
int ax2,
int ay2,
int bx1,
int by1,
int bx2,
int by2)
Check if two line segments intersect, according to the algorithm from
"Algorithms", Cormen, Leiserson & Rivest, 1992, pp.
|
void |
invalidate()
Invalidates or flushes any internally-cached data that depends
on the vertex coordinates of this
Polygon . |
void |
reset()
Resets this
Polygon object to an empty polygon. |
java.lang.String |
toString()
Returns a string representation of the object.
|
void |
translate(int deltaX,
int deltaY)
Translates the vertices of the
Polygon by
deltaX along the x axis and by
deltaY along the y axis. |
public CBCAPolygon()
public CBCAPolygon(int[] xPoints, int[] yPoints, int nPoints)
Polygon
from the specified
parameters.xPoints
- an array of x coordinatesyPoints
- an array of y coordinatesnPoints
- the total number of points in the
Polygon
java.lang.NegativeArraySizeException
- if the value of
nPoints
is negative.java.lang.IndexOutOfBoundsException
- if nPoints
is
greater than the length of xPoints
or the length of yPoints
.java.lang.NullPointerException
- if xPoints
or
yPoints
is null
.public void reset()
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.reset
in class java.awt.Polygon
public void invalidate()
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.invalidate
in class java.awt.Polygon
public void translate(int deltaX, int deltaY)
Polygon
by
deltaX
along the x axis and by
deltaY
along the y axis.translate
in class java.awt.Polygon
deltaX
- the amount to translate along the x axisdeltaY
- the amount to translate along the y axispublic void addPoint(int x, int y)
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.
addPoint
in class java.awt.Polygon
x
- the specified coordinatesy
- the specified coordinatesPolygon.getBounds()
,
Polygon.contains(java.awt.Point)
public java.lang.String toString()
toString
in class java.lang.Object
public boolean contains(double x, double y)
Polygon
. For the definition of
insideness, see the class comments of Shape
.
This method is not thread-safe, as it uses pre-allocated buffer objects. Do not use one instance of CBCAPolygon in more than one thread.
contains
in interface java.awt.Shape
contains
in class java.awt.Polygon
x
- the specified coordinatesy
- the specified coordinatestrue
if the Polygon
contains the
specified coordinates; false
otherwise.public static boolean doLineSegmentsIntersect(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2)
ax1
- X coordinate of start of first segmentay1
- Y coordinate of start of first segmentax2
- X coordinate of end of first segmentay2
- Y coordinate of end of first segmentbx1
- X coordinate of start of second segmentby1
- Y coordinate of start of second segmentbx2
- X coordinate of end of second segmentby2
- Y coordinate of end of second segmentpublic static CBCAPolygon createConvexBoundaryPolygon(java.awt.Point[] points)
Note that the points array will be modified by this method; if the points are required for some other usage, ensure that a copy is made before passing the array to this method.
points
- array of Points to bind inside the convex hull; this array will be altered by this methodjava.lang.IllegalArgumentException
- if the point array does not contain enough unique vertices to create a valid polygon