Class Polygon
- java.lang.Object
-
- elki.data.spatial.Polygon
-
- All Implemented Interfaces:
SpatialComparable,java.lang.Iterable<double[]>
public class Polygon extends java.lang.Object implements SpatialComparable, java.lang.Iterable<double[]>
Class representing a simple polygon. While you can obviously store non-simple polygons in this, note that many of the supplied methods will assume that the polygons are simple.- Since:
- 0.4.0
- Author:
- Erich Schubert
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.StringBuilderappendToBuffer(java.lang.StringBuilder buf)Append the polygon to the buffer.doubleareaShoelace()Compute the polygon area (geometric, not geographic) using the Shoelace formula.booleancontainsPoint2D(double[] v)Point in polygon test, based ondouble[]get(int idx)Get a double[] by index.intgetDimensionality()Returns the dimensionality of the object.doublegetMax(int dimension)Returns the maximum coordinate at the specified dimension.doublegetMin(int dimension)Returns the minimum coordinate at the specified dimension.booleanintersects2DIncomplete(Polygon other)Simple polygon intersection test.ArrayListIter<double[]>iter()Get an iterator to the double[] contents.java.util.Iterator<double[]>iterator()intsize()Get the polygon length.inttestClockwise()Test polygon orientation.java.lang.StringtoString()
-
-
-
Constructor Detail
-
Polygon
public Polygon(java.util.List<double[]> points)
Constructor.- Parameters:
points- Polygon points
-
Polygon
public Polygon(java.util.List<double[]> points, double minx, double maxx, double miny, double maxy)Constructor.- Parameters:
points- Polygon pointsminx- Minimum x valuemaxx- Maximum x valueminy- Minimum y valuemaxy- Maximum y value
-
-
Method Detail
-
iter
public ArrayListIter<double[]> iter()
Get an iterator to the double[] contents.- Returns:
- Iterator
-
appendToBuffer
public java.lang.StringBuilder appendToBuffer(java.lang.StringBuilder buf)
Append the polygon to the buffer.- Parameters:
buf- Buffer to append to
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
size
public int size()
Get the polygon length.- Returns:
- Polygon length
-
get
public double[] get(int idx)
Get a double[] by index.- Parameters:
idx- Index to get- Returns:
- double[]
-
getDimensionality
public int getDimensionality()
Description copied from interface:SpatialComparableReturns the dimensionality of the object.- Specified by:
getDimensionalityin interfaceSpatialComparable- Returns:
- the dimensionality
-
getMin
public double getMin(int dimension)
Description copied from interface:SpatialComparableReturns the minimum coordinate at the specified dimension.- Specified by:
getMinin interfaceSpatialComparable- Parameters:
dimension- the dimension for which the coordinate should be returned, where 0 ≤ dimension <getDimensionality()- Returns:
- the minimum coordinate at the specified dimension
-
getMax
public double getMax(int dimension)
Description copied from interface:SpatialComparableReturns the maximum coordinate at the specified dimension.- Specified by:
getMaxin interfaceSpatialComparable- Parameters:
dimension- the dimension for which the coordinate should be returned, where 0 ≤ dimension <getDimensionality()- Returns:
- the maximum coordinate at the specified dimension
-
testClockwise
public int testClockwise()
Test polygon orientation.- Returns:
- -1, 0, 1 for counterclockwise, undefined and clockwise.
-
intersects2DIncomplete
public boolean intersects2DIncomplete(Polygon other)
Simple polygon intersection test.FIXME: while this is found on some web pages as "solution" and satisfies or needs, it clearly is not correct; not even for convex polygons: Consider a cross where the two bars are made out of four vertices each. No vertex is inside the other polygon, yet they intersect.
I knew this before writing this code, but this O(n) code was the simplest thing to come up with, and it would work for our current data sets. A way to fix this is to augment it with the obvious O(n*n) segment intersection test. (Note that you will still need to test for point containment, since the whole polygon could be contained in the other!)
- Parameters:
other- Other polygon- Returns:
- True when the polygons intersect
-
containsPoint2D
public boolean containsPoint2D(double[] v)
Point in polygon test, based onhttp://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
by W. Randolph Franklin
- Parameters:
v- Point to test- Returns:
- True when contained.
-
areaShoelace
public double areaShoelace()
Compute the polygon area (geometric, not geographic) using the Shoelace formula.This is appropriate for simple polygons in the cartesian plane only.
- Returns:
- Area
-
iterator
public java.util.Iterator<double[]> iterator()
- Specified by:
iteratorin interfacejava.lang.Iterable<double[]>
-
-