Package ucar.nc2.dt.ugrid.geom
Class Polygon2D
- java.lang.Object
-
- ucar.nc2.dt.ugrid.geom.Polygon2D
-
- All Implemented Interfaces:
Shape,Serializable
- Direct Known Subclasses:
Polygon2D.Double,Polygon2D.Float
public abstract class Polygon2D extends Object implements Shape, Serializable
NOTE: This class should NOT be used in situations involving geographic (lat, lon) data. In such cases, use LatLonPolygon2D instead.- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classPolygon2D.DoubleThe concrete Polygon class that stores coordinates internally as doubles.static classPolygon2D.FloatThe concrete Polygon class that stores coordinates internally as floats.
-
Field Summary
Fields Modifier and Type Field Description protected boolean_closedThe flag that says that the polygon has been closedprotected int_coordCountThe current number of coordinates
-
Constructor Summary
Constructors Constructor Description Polygon2D()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description static doublearea(Polygon2D poly)Calculates thedoublearea of the given polygon.voidclosePath()Close the polygon.booleancontains(double x, double y)Return true if the given point is inside the polygon.booleancontains(double x1, double y1, double w, double h)Return true if the given rectangle is entirely inside the polygon.booleancontains(Point2D p)Return true if the given point is inside the polygon.booleancontains(Rectangle2D r)Return true if the given rectangle is entirely inside the polygon.booleancontains(GeoRectangle r)Return true if the given rectangle is entirely inside the polygon.booleancontains(Polygon2D p)Determines if the given polygon is fully contained within this polygon.
This method can be fooled by suppling a polygon whose points are completly contained in this polygon but has some intersecting lines.abstract Polygon2Dcopy()Creates a copy of this polygon with the same coordinates.doubledistance(Point2D p)Calculates the square of the distance from the pointpto the nearest line segment specified by connecting the vertices of thisPolygon2DdoubledistanceSq(Point2D p)Calculates the square of the distance from the pointpto the nearest line segment specified by connecting the vertices of thisPolygon2DdoublegetArea()Calculates the area of thisPolygon2DRectanglegetBounds()Get the integerRectanglebounds of the polygon.abstract Rectangle2DgetBounds2D()Get the floating-point bounds of the polygon.abstract GeoRectanglegetBoundsGeo()Get the bound box of this poly as aGeoRectangle2Dabstract Point2DgetCentroid()Calculates the center point of thisPolygon2DdoublegetMaxX()Determines the highest x value within the points of this polygondoublegetMaxY()Determines the highest x value within the points of this polygondoublegetMinX()Determines the lowest x value within the points of this polygondoublegetMinY()Determines the lowest y value within the points of this polygonPathIteratorgetPathIterator(AffineTransform at)Get a path iterator over the object.PathIteratorgetPathIterator(AffineTransform at, double flatness)Get a path iterator over the object.intgetVertexCount()Get the number of verticesabstract List<Point2D>getVertices()Gets a list of Point2D vertices of this polygon.abstract doublegetX(int index)Get the given X-coordinateabstract double[]getXCoords()Gets a double array containing the X coordinates of the polygon.abstract doublegetY(int index)Get the given Y-coordinateabstract double[]getYCoords()Gets a double array containing the Y coordinates of the polygon.booleanintersects(double x1, double y1, double w, double h)Test if the polygon is intersected by the given rectangle.booleanintersects(Point2D p)Inclusively checks if the given point is contained within this polygon or intersected by any of its line-segmentsbooleanintersects(Point2D e0, Point2D e1)Checks if the line segment given by the extremese1ande2intersect this polygon, by checking segment to segment intersection for each vector of this polygon.booleanintersects(Rectangle2D r)Test if the polygon is intersected by the given rectangle.booleanintersects(GeoRectangle r)Test if the polygon is intersected by the given GeoRectangle.booleanintersects(Polygon2D poly)Checks if this polygon intersects the given polygon.static booleanlineIntersectsPoint(double e0x, double e0y, double e1x, double e1y, double p1x, double p1y)Checks if the point given by p1x, p1y is intersected by the line segment given by e0, e1static booleanlinesIntersect(double p0x, double p0y, double p1x, double p1y, double p2x, double p2y, double p3x, double p3y)Checks if the line segment drawn from p0 to p1 intersects the line drawn from p2 to p3.abstract voidlineTo(double x, double y)Add a new vertex to the end of the polygon.voidlineTo(Point2D p)abstract voidmoveTo(double x, double y)Move the start point of the vertex to the given position.voidmoveTo(Point2D p)voidreset()Reset the polygon back to empty.abstract voidsetX(int index, double x)Set the given X-coordinate.abstract voidsetY(int index, double y)Set the given Y-coordinateStringtoString()Return a string representation of the polygon.abstract voidtransform(AffineTransform at)Transform the polygon with the given transform.abstract voidtranslate(double x, double y)Translate the polygon the given distance.
-
-
-
Method Detail
-
area
public static double area(Polygon2D poly)
Calculates thedoublearea of the given polygon.- Parameters:
poly- aPolygon2Dobject- Returns:
- the
doublearea
-
linesIntersect
public static boolean linesIntersect(double p0x, double p0y, double p1x, double p1y, double p2x, double p2y, double p3x, double p3y)Checks if the line segment drawn from p0 to p1 intersects the line drawn from p2 to p3. This is an optimized variation ofLine2D.linesIntersect(double, double, double, double, double, double, double, double)- Parameters:
p0x- the first points x valuep0y- the first points y valuep1x- the second points x valuep1y- the second point's y valuep2x- the third point's x valuep2y- the third point's y valuep3x- the fourth point's x valuep3y- the fourth point's y value- Returns:
trueif the lines created from the given points intersect, otherwisefalse
-
lineIntersectsPoint
public static boolean lineIntersectsPoint(double e0x, double e0y, double e1x, double e1y, double p1x, double p1y)Checks if the point given by p1x, p1y is intersected by the line segment given by e0, e1- Parameters:
e0x- the line segment's first endpoint's x valuee0y- the line segment's first endpoint's y valuee1x- the line segment's second endpoint's x valuee1y- the line segment's second endpoint's y valuep1x- the point's x valuep1y- the point's y value- Returns:
trueif the point is intersected by the given line segment, otherwisefalse
-
closePath
public void closePath()
Close the polygon. No further segments can be added. If this method not called, then the path iterators will treat the polygon as thought it were closed, and implicitly join the most recently added vertex to the first one. However, this method should generally be called, as if the last vertex is the same as the first vertex, then it merges them.
-
contains
public boolean contains(double x, double y)Return true if the given point is inside the polygon. This method uses a straight-forward algorithm, where a point is taken to be inside the polygon if a horizontal line extended from the point to infinity intersects an odd number of segments.
-
contains
public boolean contains(Point2D p)
Return true if the given point is inside the polygon.
-
contains
public boolean contains(Rectangle2D r)
Return true if the given rectangle is entirely inside the polygon. (Currently, this algorithm can be fooled by supplying a rectangle that has all four corners inside the polygon, but which intersects some edges.)
-
contains
public boolean contains(GeoRectangle r)
Return true if the given rectangle is entirely inside the polygon. (Currently, this algorithm can be fooled by supplying a rectangle that has all four corners inside the polygon, but which intersects some edges.)
-
contains
public boolean contains(Polygon2D p)
Determines if the given polygon is fully contained within this polygon.
This method can be fooled by suppling a polygon whose points are completly contained in this polygon but has some intersecting lines. In this instance, although the polygons intersect the given polygon is not "wholely" contained within this polygon. This method would still returntrue.- Parameters:
p- aPolygon2Dobject- Returns:
trueif all the points of the given polygon are contained within this Polygon2D, otherwisefalse
-
copy
public abstract Polygon2D copy()
Creates a copy of this polygon with the same coordinates. The original polygon remains unchanged.- Returns:
- a copy of the this polygon
-
contains
public boolean contains(double x1, double y1, double w, double h)Return true if the given rectangle is entirely inside the polygon. (Currently, this algorithm can be fooled by supplying a rectangle that has all four corners inside the polygon, but which intersects some edges.)
-
getArea
public double getArea()
Calculates the area of thisPolygon2D- Returns:
- the
doublearea of this polygon
-
getBounds2D
public abstract Rectangle2D getBounds2D()
Get the floating-point bounds of the polygon.- Specified by:
getBounds2Din interfaceShape
-
getBoundsGeo
public abstract GeoRectangle getBoundsGeo()
Get the bound box of this poly as aGeoRectangle2D
-
getCentroid
public abstract Point2D getCentroid()
Calculates the center point of thisPolygon2D- Returns:
- the center point of this
Polygon2D
-
distanceSq
public double distanceSq(Point2D p)
Calculates the square of the distance from the pointpto the nearest line segment specified by connecting the vertices of thisPolygon2D- Parameters:
p- aPoint2D- Returns:
- the square of the distance from the given point to this
Polygon2D
-
distance
public double distance(Point2D p)
Calculates the square of the distance from the pointpto the nearest line segment specified by connecting the vertices of thisPolygon2D- Parameters:
p- aPoint2D- Returns:
- the square of the distance from the given point to this
Polygon2D
-
getXCoords
public abstract double[] getXCoords()
Gets a double array containing the X coordinates of the polygon.- Returns:
- the X coordinates
-
getYCoords
public abstract double[] getYCoords()
Gets a double array containing the Y coordinates of the polygon.- Returns:
- the Y coordinates
-
getVertices
public abstract List<Point2D> getVertices()
Gets a list of Point2D vertices of this polygon.- Returns:
- a List{Point2D} of vertices
-
getMinX
public double getMinX()
Determines the lowest x value within the points of this polygon- Returns:
- the lowest x value
-
getMinY
public double getMinY()
Determines the lowest y value within the points of this polygon- Returns:
- the lowest y value
-
getMaxX
public double getMaxX()
Determines the highest x value within the points of this polygon- Returns:
- the highest x value
-
getMaxY
public double getMaxY()
Determines the highest x value within the points of this polygon- Returns:
- the highest x value
-
getPathIterator
public PathIterator getPathIterator(AffineTransform at, double flatness)
Get a path iterator over the object.- Specified by:
getPathIteratorin interfaceShape
-
getPathIterator
public PathIterator getPathIterator(AffineTransform at)
Get a path iterator over the object.- Specified by:
getPathIteratorin interfaceShape
-
getVertexCount
public int getVertexCount()
Get the number of vertices
-
getX
public abstract double getX(int index)
Get the given X-coordinate- Throws:
IndexOutOfBoundsException- The index is out of bounds.
-
getY
public abstract double getY(int index)
Get the given Y-coordinate- Throws:
IndexOutOfBoundsException- The index is out of bounds.
-
intersects
public boolean intersects(Point2D e0, Point2D e1)
Checks if the line segment given by the extremese1ande2intersect this polygon, by checking segment to segment intersection for each vector of this polygon. [TPL]
-
intersects
public boolean intersects(Polygon2D poly)
Checks if this polygon intersects the given polygon. This algorithm uses the 2D Polygon-Polygon Intersection test as proposed in the webaddress below- Returns:
trueif there is an intersection, otherwisefalse- See Also:
- Efficient Collision Detection between 2D Polygons
-
intersects
public boolean intersects(Rectangle2D r)
Test if the polygon is intersected by the given rectangle. (Currently, this algorithm can be fooled by supplying a rectangle that has no corners inside the polygon, and does not contain any vertex of the polygon, but which intersects some edges.)- Specified by:
intersectsin interfaceShape
-
intersects
public boolean intersects(GeoRectangle r)
Test if the polygon is intersected by the given GeoRectangle. (Currently, this algorithm can be fooled by supplying a rectangle that has no corners inside the polygon, and does not contain any vertex of the polygon, but which intersects some edges.)
-
intersects
public boolean intersects(double x1, double y1, double w, double h)Test if the polygon is intersected by the given rectangle.- Specified by:
intersectsin interfaceShape
-
intersects
public boolean intersects(Point2D p)
Inclusively checks if the given point is contained within this polygon or intersected by any of its line-segments- Parameters:
p- aPoint2D- Returns:
trueif the given point is intersected by thisPolygon2D, otherwisefalse
-
lineTo
public void lineTo(Point2D p)
-
lineTo
public abstract void lineTo(double x, double y)Add a new vertex to the end of the polygon. Throw an exception of the polygon has already been closed.
-
moveTo
public void moveTo(Point2D p)
-
moveTo
public abstract void moveTo(double x, double y)Move the start point of the vertex to the given position. Throw an exception if the line already contains any vertices.
-
reset
public void reset()
Reset the polygon back to empty.
-
setX
public abstract void setX(int index, double x)Set the given X-coordinate.- Throws:
IndexOutOfBoundsException- The index is out of bounds.
-
setY
public abstract void setY(int index, double y)Set the given Y-coordinate- Throws:
IndexOutOfBoundsException- The index is out of bounds.
-
transform
public abstract void transform(AffineTransform at)
Transform the polygon with the given transform.
-
translate
public abstract void translate(double x, double y)Translate the polygon the given distance.
-
-