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
NOTE: This class should NOT be used in situations involving geographic (lat, lon) data. In such cases, use
LatLonPolygon2D instead.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classThe concrete Polygon class that stores coordinates internally as doubles.static classThe concrete Polygon class that stores coordinates internally as floats. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected booleanThe flag that says that the polygon has been closedprotected intThe current number of coordinates -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic doubleCalculates thedoublearea of the given polygon.voidClose 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.booleanReturn true if the given point is inside the polygon.booleanReturn true if the given rectangle is entirely inside the polygon.booleanReturn true if the given rectangle is entirely inside the polygon.booleanDetermines 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.doubleCalculates the square of the distance from the pointpto the nearest line segment specified by connecting the vertices of thisPolygon2DdoubleCalculates the square of the distance from the pointpto the nearest line segment specified by connecting the vertices of thisPolygon2DdoublegetArea()Calculates the area of thisPolygon2DGet the integerRectanglebounds of the polygon.abstract Rectangle2DGet the floating-point bounds of the polygon.abstract GeoRectangleGet the bound box of this poly as aGeoRectangle2Dabstract Point2DCalculates 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 polygonGet a path iterator over the object.getPathIterator(AffineTransform at, double flatness) Get a path iterator over the object.intGet the number of verticesGets a list of Point2D vertices of this polygon.abstract doublegetX(int index) Get the given X-coordinateabstract double[]Gets a double array containing the X coordinates of the polygon.abstract doublegetY(int index) Get the given Y-coordinateabstract double[]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.booleanInclusively 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.booleanTest if the polygon is intersected by the given rectangle.booleanTest 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.voidabstract voidmoveTo(double x, double y) Move the start point of the vertex to the given position.voidvoidreset()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-coordinatetoString()Return a string representation of the polygon.abstract voidTransform the polygon with the given transform.abstract voidtranslate(double x, double y) Translate the polygon the given distance.
-
Field Details
-
_coordCount
protected int _coordCountThe current number of coordinates -
_closed
protected boolean _closedThe flag that says that the polygon has been closed
-
-
Constructor Details
-
Polygon2D
public Polygon2D()
-
-
Method Details
-
area
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
Return true if the given point is inside the polygon. -
contains
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
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
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
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
-
getBounds
Get the integerRectanglebounds of the polygon. -
getBounds2D
Get the floating-point bounds of the polygon.- Specified by:
getBounds2Din interfaceShape
-
getBoundsGeo
Get the bound box of this poly as aGeoRectangle2D -
getCentroid
Calculates the center point of thisPolygon2D- Returns:
- the center point of this
Polygon2D
-
distanceSq
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
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
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
Get a path iterator over the object.- Specified by:
getPathIteratorin interfaceShape
-
getPathIterator
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
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
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:
-
intersects
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
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
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
-
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
-
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
Transform the polygon with the given transform. -
translate
public abstract void translate(double x, double y) Translate the polygon the given distance. -
toString
Return a string representation of the polygon.
-