Package org.locationtech.jts.algorithm
Class Centroid
- java.lang.Object
-
- org.locationtech.jts.algorithm.Centroid
-
public class Centroid extends java.lang.ObjectComputes the centroid of aGeometryof any dimension. If the geometry is nominally of higher dimension, but has lower effective dimension (i.e. contains only components having zero length or area), the centroid will be computed as for the equivalent lower-dimension geometry. If the input geometry is empty, anullCoordinate is returned.Algorithm
- Dimension 2 - the centroid is computed
as the weighted sum of the centroids
of a decomposition of the area into (possibly overlapping) triangles.
Holes and multipolygons are handled correctly.
See
http://www.faqs.org/faqs/graphics/algorithms-faq/for further details of the basic approach. - Dimension 1 - Computes the average of the midpoints of all line segments weighted by the segment length. Zero-length lines are treated as points.
- Dimension 0 - Compute the average coordinate for all points. Repeated points are all included in the average.
- Version:
- 1.7
- Dimension 2 - the centroid is computed
as the weighted sum of the centroids
of a decomposition of the area into (possibly overlapping) triangles.
Holes and multipolygons are handled correctly.
See
-
-
Field Summary
Fields Modifier and Type Field Description private CoordinateareaBasePtprivate doubleareasum2private Coordinatecg3private CoordinatelineCentSumprivate CoordinateptCentSumprivate intptCountprivate doubletotalLengthprivate CoordinatetriangleCent3
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidadd(Geometry geom)Adds a Geometry to the centroid total.private voidadd(Polygon poly)private voidaddHole(Coordinate[] pts)private voidaddLineSegments(Coordinate[] pts)Adds the line segments defined by an array of coordinates to the linear centroid accumulators.private voidaddPoint(Coordinate pt)Adds a point to the point centroid accumulator.private voidaddShell(Coordinate[] pts)private voidaddTriangle(Coordinate p0, Coordinate p1, Coordinate p2, boolean isPositiveArea)private static doublearea2(Coordinate p1, Coordinate p2, Coordinate p3)Returns twice the signed area of the triangle p1-p2-p3.private static voidcentroid3(Coordinate p1, Coordinate p2, Coordinate p3, Coordinate c)Computes three times the centroid of the triangle p1-p2-p3.CoordinategetCentroid()Gets the computed centroid.static CoordinategetCentroid(Geometry geom)Computes the centroid point of a geometry.private voidsetAreaBasePoint(Coordinate basePt)
-
-
-
Field Detail
-
areaBasePt
private Coordinate areaBasePt
-
triangleCent3
private Coordinate triangleCent3
-
areasum2
private double areasum2
-
cg3
private Coordinate cg3
-
lineCentSum
private Coordinate lineCentSum
-
totalLength
private double totalLength
-
ptCount
private int ptCount
-
ptCentSum
private Coordinate ptCentSum
-
-
Constructor Detail
-
Centroid
public Centroid(Geometry geom)
Creates a new instance for computing the centroid of a geometry
-
-
Method Detail
-
getCentroid
public static Coordinate getCentroid(Geometry geom)
Computes the centroid point of a geometry.- Parameters:
geom- the geometry to use- Returns:
- the centroid point, or null if the geometry is empty
-
add
private void add(Geometry geom)
Adds a Geometry to the centroid total.- Parameters:
geom- the geometry to add
-
getCentroid
public Coordinate getCentroid()
Gets the computed centroid.- Returns:
- the computed centroid, or null if the input is empty
-
setAreaBasePoint
private void setAreaBasePoint(Coordinate basePt)
-
add
private void add(Polygon poly)
-
addShell
private void addShell(Coordinate[] pts)
-
addHole
private void addHole(Coordinate[] pts)
-
addTriangle
private void addTriangle(Coordinate p0, Coordinate p1, Coordinate p2, boolean isPositiveArea)
-
centroid3
private static void centroid3(Coordinate p1, Coordinate p2, Coordinate p3, Coordinate c)
Computes three times the centroid of the triangle p1-p2-p3. The factor of 3 is left in to permit division to be avoided until later.
-
area2
private static double area2(Coordinate p1, Coordinate p2, Coordinate p3)
Returns twice the signed area of the triangle p1-p2-p3. The area is positive if the triangle is oriented CCW, and negative if CW.
-
addLineSegments
private void addLineSegments(Coordinate[] pts)
Adds the line segments defined by an array of coordinates to the linear centroid accumulators.- Parameters:
pts- an array ofCoordinates
-
addPoint
private void addPoint(Coordinate pt)
Adds a point to the point centroid accumulator.- Parameters:
pt- aCoordinate
-
-