/// <summary> /// Gets the centroid of the polyline 2d. /// </summary> /// <param name="pl">The instance to which the method applies.</param> /// <returns>The centroid of the polyline 2d (WCS coordinates).</returns> public static Point3d Centroid(this Polyline2d pl) { var vertices = pl.GetVertices().ToArray(); var last = vertices.Length - 1; var vertex = vertices[0]; var p0 = vertex.Position.Convert2D(); _ = pl.Elevation; var cen = new Point2d(0.0, 0.0); var area = 0.0; var bulge = vertex.Bulge; double tmpArea; Point2d tmpPt; var tri = new Triangle2D(); var arc = new CircularArc2d(); if (bulge != 0.0) { arc = pl.GetArcSegment2DAt(0); tmpArea = arc.AlgebricArea(); tmpPt = arc.Centroid(); area += tmpArea; cen += (new Point2d(tmpPt.X, tmpPt.Y) * tmpArea).GetAsVector(); } for (var i = 1; i < last; i++) { var p1 = vertices[i].Position.Convert2D(); var p2 = vertices[i + 1].Position.Convert2D(); tri.Set(p0, p1, p2); tmpArea = tri.AlgebricArea; area += tmpArea; cen += (tri.Centroid * tmpArea).GetAsVector(); bulge = vertices[i].Bulge; if (bulge != 0.0) { arc = pl.GetArcSegment2DAt(i); tmpArea = arc.AlgebricArea(); tmpPt = arc.Centroid(); area += tmpArea; cen += (new Point2d(tmpPt.X, tmpPt.Y) * tmpArea).GetAsVector(); } } bulge = vertices[last].Bulge; if (bulge != 0.0 && pl.Closed) { arc = pl.GetArcSegment2DAt(last); tmpArea = arc.AlgebricArea(); tmpPt = arc.Centroid(); area += tmpArea; cen += (new Point2d(tmpPt.X, tmpPt.Y) * tmpArea).GetAsVector(); } cen = cen.DivideBy(area); return(new Point3d(cen.X, cen.Y, pl.Elevation).TransformBy(Matrix3d.PlaneToWorld(pl.Normal))); }