Package org.ka2ddo.yaac.util
Class CBCAPolygon
java.lang.Object
java.awt.Polygon
org.ka2ddo.yaac.util.CBCAPolygon
- All Implemented Interfaces:
Shape
,Serializable
This implements the Cell-Based Containment Algorithm for more efficiently
determining whether a point is inside a polygon with a large number of vertices.
Ref: Zalik & Kolingerova, "A cell-based point-in-polygon algorithm suitable for large sets of points", Computers & Geosciences 27 (2001) 1135-1145.
- Author:
- Andrew Pavlin, KA2DDO
- See Also:
-
Field Summary
-
Constructor Summary
ConstructorDescriptionCreates an empty polygon.CBCAPolygon
(int[] xPoints, int[] yPoints, int nPoints) Constructs and initializes aPolygon
from the specified parameters. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addPoint
(int x, int y) Appends the specified coordinates to thisPolygon
.boolean
contains
(double x, double y) Determines if the specified coordinates are inside thisPolygon
.static CBCAPolygon
createConvexBoundaryPolygon
(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
Invalidates or flushes any internally-cached data that depends on the vertex coordinates of thisPolygon
.void
reset()
Resets thisPolygon
object to an empty polygon.toString()
Returns a string representation of the object.void
translate
(int deltaX, int deltaY) Translates the vertices of thePolygon
bydeltaX
along the x axis and bydeltaY
along the y axis.Methods inherited from class java.awt.Polygon
contains, contains, contains, contains, contains, getBoundingBox, getBounds, getBounds2D, getPathIterator, getPathIterator, inside, intersects, intersects
-
Constructor Details
-
CBCAPolygon
public CBCAPolygon()Creates an empty polygon. -
CBCAPolygon
public CBCAPolygon(int[] xPoints, int[] yPoints, int nPoints) Constructs and initializes aPolygon
from the specified parameters.- Parameters:
xPoints
- an array of x coordinatesyPoints
- an array of y coordinatesnPoints
- the total number of points in thePolygon
- Throws:
NegativeArraySizeException
- if the value ofnPoints
is negative.IndexOutOfBoundsException
- ifnPoints
is greater than the length ofxPoints
or the length ofyPoints
.NullPointerException
- ifxPoints
oryPoints
isnull
.
-
-
Method Details
-
reset
public void reset()Resets thisPolygon
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 emptyPolygon
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. -
invalidate
public void invalidate()Invalidates or flushes any internally-cached data that depends on the vertex coordinates of thisPolygon
. This method should be called after any direct manipulation of the coordinates in thexPoints
oryPoints
arrays to avoid inconsistent results from methods such asgetBounds
orcontains
that might cache data from earlier computations relating to the vertex coordinates.- Overrides:
invalidate
in classPolygon
-
translate
public void translate(int deltaX, int deltaY) Translates the vertices of thePolygon
bydeltaX
along the x axis and bydeltaY
along the y axis. -
addPoint
public void addPoint(int x, int y) Appends the specified coordinates to thisPolygon
.If an operation that calculates the bounding box of this
Polygon
has already been performed, such asgetBounds
orcontains
, then this method updates the bounding box. -
toString
Returns a string representation of the object. -
contains
public boolean contains(double x, double y) Determines if the specified coordinates are inside thisPolygon
. For the definition of insideness, see the class comments ofShape
.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.
-
doLineSegmentsIntersect
public 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. 889-890.- Parameters:
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 segment- Returns:
- boolean true if line segments intersect between their endpoints
-
createConvexBoundaryPolygon
Find the convex hull polygon around an arbitrary unordered array of 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.
- Parameters:
points
- array of Points to bind inside the convex hull; this array will be altered by this method- Returns:
- CBCAPolygon of a convex hull around the point set, where every vertex is a Point from the original array of Points
- Throws:
IllegalArgumentException
- if the point array does not contain enough unique vertices to create a valid polygon
-