Index: src/Common/NetTopologySuite/Algorithm/CentroidArea.cs =================================================================== diff -u -r8f6ae890fed8e8eae3a32f9c0498a10f82e0ddf9 -r5fc71a385897af92ccb092f2f969b5709afab85a --- src/Common/NetTopologySuite/Algorithm/CentroidArea.cs (.../CentroidArea.cs) (revision 8f6ae890fed8e8eae3a32f9c0498a10f82e0ddf9) +++ src/Common/NetTopologySuite/Algorithm/CentroidArea.cs (.../CentroidArea.cs) (revision 5fc71a385897af92ccb092f2f969b5709afab85a) @@ -15,34 +15,50 @@ /// public class CentroidArea { - private ICoordinate basePt = null; // the point all triangles are based at - private ICoordinate triangleCent3 = new Coordinate(); // temporary variable to hold centroid of triangle - private double areasum2 = 0; // Partial area sum - private ICoordinate cg3 = new Coordinate(); // partial centroid sum + private ICoordinate basePt = null; // the point all triangles are based at + private ICoordinate triangleCent3 = new Coordinate(); // temporary variable to hold centroid of triangle + private double areasum2 = 0; // Partial area sum + private readonly ICoordinate cg3 = new Coordinate(); // partial centroid sum /// /// /// - public CentroidArea() { } + public CentroidArea() {} + /// + /// + /// + public ICoordinate Centroid + { + get + { + ICoordinate cent = new Coordinate(); + cent.X = cg3.X/3/areasum2; + cent.Y = cg3.Y/3/areasum2; + return cent; + } + } + /// /// Adds the area defined by a Geometry to the centroid total. /// If the point has no area it does not contribute to the centroid. /// /// The point to add. public void Add(IGeometry geom) { - if (geom is IPolygon) + if (geom is IPolygon) { IPolygon poly = (IPolygon) geom; BasePoint = poly.ExteriorRing.GetCoordinateN(0); Add(poly); } - else if (geom is IGeometryCollection) + else if (geom is IGeometryCollection) { IGeometryCollection gc = (IGeometryCollection) geom; foreach (IGeometry geometry in gc.Geometries) + { Add(geometry); + } } } @@ -61,30 +77,18 @@ /// /// /// - public ICoordinate Centroid - { - get - { - ICoordinate cent = new Coordinate(); - cent.X = cg3.X / 3 / areasum2; - cent.Y = cg3.Y / 3 / areasum2; - return cent; - } - } - - /// - /// - /// private ICoordinate BasePoint { get { - return this.basePt; + return basePt; } set { - if (this.basePt == null) - this.basePt = value; + if (basePt == null) + { + basePt = value; + } } } @@ -96,7 +100,9 @@ { AddShell(poly.ExteriorRing.Coordinates); foreach (ILineString ls in poly.InteriorRings) + { AddHole(ls.Coordinates); + } } /// @@ -107,7 +113,9 @@ { bool isPositiveArea = !CGAlgorithms.IsCCW(pts); for (int i = 0; i < pts.Length - 1; i++) + { AddTriangle(basePt, pts[i], pts[i + 1], isPositiveArea); + } } /// @@ -118,7 +126,9 @@ { bool isPositiveArea = CGAlgorithms.IsCCW(pts); for (int i = 0; i < pts.Length - 1; i++) + { AddTriangle(basePt, pts[i], pts[i + 1], isPositiveArea); + } } /// @@ -133,9 +143,9 @@ double sign = (isPositiveArea) ? 1.0 : -1.0; Centroid3(p0, p1, p2, ref triangleCent3); double area2 = Area2(p0, p1, p2); - cg3.X += sign * area2 * triangleCent3.X; - cg3.Y += sign * area2 * triangleCent3.Y; - areasum2 += sign * area2; + cg3.X += sign*area2*triangleCent3.X; + cg3.Y += sign*area2*triangleCent3.Y; + areasum2 += sign*area2; } /// @@ -156,7 +166,7 @@ /// private static double Area2(ICoordinate p1, ICoordinate p2, ICoordinate p3) { - return (p2.X - p1.X) * (p3.Y - p1.Y) - (p3.X - p1.X) * (p2.Y - p1.Y); + return (p2.X - p1.X)*(p3.Y - p1.Y) - (p3.X - p1.X)*(p2.Y - p1.Y); } } -} +} \ No newline at end of file