getConvexHull method

Geometry getConvexHull()

Returns a {@link Geometry} that represents the convex hull of the input geometry. The returned geometry contains the minimal number of points needed to represent the convex hull. In particular, no more than two consecutive points will be collinear.

@return if the convex hull contains 3 or more points, a {@link Polygon}; 2 points, a {@link LineString}; 1 point, a {@link Point}; 0 points, an empty {@link GeometryCollection}.

Implementation

Geometry getConvexHull() {
  if (inputPts.length == 0) {
    return geomFactory.createGeometryCollectionEmpty();
  }
  if (inputPts.length == 1) {
    return geomFactory.createPoint(inputPts[0]);
  }
  if (inputPts.length == 2) {
    return geomFactory.createLineString(inputPts);
  }

  List<Coordinate> reducedPts = inputPts;
  // use heuristic to reduce points, if large
  if (inputPts.length > 50) {
    reducedPts = reduce(inputPts);
  }
  // sort points for Graham scan.
  List<Coordinate> sortedPts = preSort(reducedPts);

  // Use Graham scan to find convex hull.
  List<Coordinate> cHS = grahamScan(sortedPts);

  // Convert stack to an array.
  List<Coordinate> cH = toCoordinateArray(cHS);

  // Convert array to appropriate output geometry.
  return lineOrPolygon(cH);
}