.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Get the area-midpoint of a closed polyline

2 REPLIES 2
Reply
Message 1 of 3
Juergen.Becker
910 Views, 2 Replies

Get the area-midpoint of a closed polyline

Hi,

is there any possibilty to get the area-midpoint of a closed polyline.

Regards Jürgen
2 REPLIES 2
Message 2 of 3
_gile
in reply to: Juergen.Becker

Hi,

Not sure this reply your demand, but here's a snippet to get a polyline centroid

{code}private Point3d GetCentroid(Polyline pl)
{
Point2d p0 = pl.GetPoint2dAt(0);
Point2d cen = new Point2d(0.0, 0.0);
double area = 0.0;
double bulge = pl.GetBulgeAt(0);
int last = pl.NumberOfVertices - 1;
double tmpArea;
Point2d tmpPoint;

if (bulge != 0.0)
{
double[] datas = getArcGeom(pl, bulge, 0, 1);
area = datas[0];
cen = new Point2d(datas[1], datas[2]) * datas[0];
}
for (int i = 1; i < last; i++)
{
tmpArea = triangleAlgebricArea(p0, pl.GetPoint2dAt(i), pl.GetPoint2dAt(i + 1));
tmpPoint = triangleCentroid(p0, pl.GetPoint2dAt(i), pl.GetPoint2dAt(i + 1));
cen += (tmpPoint * tmpArea).GetAsVector();
area += tmpArea;
bulge = pl.GetBulgeAt(i);
if (bulge != 0.0)
{
double[] datas = getArcGeom(pl, bulge, i, i + 1);
area += datas[0];
cen += new Vector2d(datas[1], datas[2]) * datas[0];
}
}
bulge = pl.GetBulgeAt(last);
if (bulge != 0.0)
{
double[] datas = getArcGeom(pl, bulge, last, 0);
area += datas[0];
cen += new Vector2d(datas[1], datas[2]) * datas[0];
}
cen = cen.DivideBy(area);
Point3d result = new Point3d(cen.X, cen.Y, pl.Elevation);
return result.TransformBy(Matrix3d.PlaneToWorld(pl.Normal));
}

private double[] GetArcGeom(Polyline pl, double bulge, int index1, int index2)
{
CircularArc2d arc = (pl.GetArcSegment2dAt(index1));
double arcRadius = arc.Radius;
Point2d arcCenter = arc.Center;
double arcAngle = 4.0 * Math.Atan(bulge);
double tmpArea = arcAlgebricArea(arcRadius, arcAngle);
Point2d tmpPoint = arcCentroid(pl.GetPoint2dAt(index1), pl.GetPoint2dAt(index2), arcCenter, tmpArea);
return new double[3] { tmpArea, tmpPoint.X, tmpPoint.Y };
}

private Point2d TriangleCentroid(Point2d p0, Point2d p1, Point2d p2)
{
return (p0 + p1.GetAsVector() + p2.GetAsVector()) / 3.0;
}

private double TriangleAlgebricArea(Point2d p0, Point2d p1, Point2d p2)
{
return (((p1.X - p0.X) * (p2.Y - p0.Y)) - ((p2.X - p0.X) * (p1.Y - p0.Y))) / 2.0;
}

private Point2d ArcCentroid(Point2d start, Point2d end, Point2d cen, double tmpArea)
{
double chord = start.GetDistanceTo(end);
double angle = angleFromTo(start, end);
return polar2d(cen, angle - (Math.PI / 2.0), (chord * chord * chord) / (12.0 * tmpArea));
}

private double ArcAlgebricArea(double rad, double ang)
{
return rad * rad * (ang - Math.Sin(ang)) / 2.0;
}

private double AngleFromTo(Point2d p1, Point2d p2)
{
return (p2 - p1).Angle;
}

private Point2d Polar2d(Point2d org, double angle, double distance)
{
return new Point2d(org.X + distance, org.Y).RotateBy(angle, org);
}{code}


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 3
deckeresq
in reply to: _gile

While this is 5 years later, I just wanted to say thanks for this code! It's a lifesaver.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost