using GeoAPI.Geometries; using GisSharpBlog.NetTopologySuite.Geometries; namespace GisSharpBlog.NetTopologySuite.Algorithm { /// /// Computes the centroid of a point point. /// Algorithm: /// Compute the average of all points. /// public class CentroidPoint { private int ptCount = 0; private readonly ICoordinate centSum = new Coordinate(); /// /// /// public CentroidPoint() {} /// /// /// public ICoordinate Centroid { get { ICoordinate cent = new Coordinate(); cent.X = centSum.X/ptCount; cent.Y = centSum.Y/ptCount; return cent; } } /// /// Adds the point(s) defined by a Geometry to the centroid total. /// If the point is not of dimension 0 it does not contribute to the centroid. /// /// The point to add. public void Add(IGeometry geom) { if (geom is IPoint) { Add(geom.Coordinate); } else if (geom is IGeometryCollection) { IGeometryCollection gc = (IGeometryCollection) geom; foreach (IGeometry geometry in gc.Geometries) { Add(geometry); } } } /// /// Adds the length defined by a coordinate. /// /// A coordinate. public void Add(ICoordinate pt) { ptCount += 1; centSum.X += pt.X; centSum.Y += pt.Y; } } }