hi all, i'm trying to find the centroid of a closed polyline.
i've read a few lisp examples that can do this (including one on the swamp
http://www.theswamp.org/index.php?topic=18725.0) but haven't been able to transform this code to .net. From the lisp examples it seems that the polyline is converted to a region and then the centriod is found from the region, however from what i can see there is no centroid property (although there is a centroid property under solid3dmassproperties).
So i couldn't convert lisp to .net and went off and found the formula for the centroid of a polyline (
http://en.wikipedia.org/wiki/Centroid) as well as a vb.net sample (
http://www.vb-helper.com/howto_net_polygon_centroid.html) after slightly modifying the code i've come up with the following...
' For polylines
Dim pl As AADS.Polyline = TryCast(context.PickedObject, AADS.Polyline)
If pl Is Nothing OrElse pl.Closed = False Then Return
Dim RunningX As Long
Dim RunningY As Long
Dim second_factor As Long
For Counter As Integer = 0 To pl.NumberOfVertices - 2
second_factor = pl.GetPoint2dAt(Counter).X * pl.GetPoint2dAt(Counter + 1).Y - pl.GetPoint2dAt(Counter + 1).X * pl.GetPoint2dAt(Counter).Y
RunningX += (pl.GetPoint2dAt(Counter).X + pl.GetPoint2dAt(Counter + 1).X) * second_factor
RunningY += (pl.GetPoint2dAt(Counter).Y + pl.GetPoint2dAt(Counter + 1).Y) * second_factor
Next
Dim X, Y As Long
' Divide by 6 times the polygon's area.
Dim polygon_area As Single = pl.Area
X = RunningX / (6 * pl.Area)
Y = RunningY / (6 * pl.Area)
' If the values are negative, the polygon is
' oriented counterclockwise. Reverse the signs.
If X < 0 Then
X = -X
Y = -Y
End If
Dim CentroidPoint As AAG.Point3d = New AAG.Point3d(X, Y, pl.Elevation)
the code works for a rectangle that is drawn at 0,0... however, when the polyline moves away from 0,0 the code produces some crazy results and if the polyline is in a different quadrant about the usc it produces some more crazy results
does anyone know of a better / easier way to find the centroid, or a problem with my code???
any help would be great
cheers, Mark