Page 1 of 1

How to Find Area and Perimeter of a Closed LWPolygon

Posted: 07 Aug 2022, 00:20
by Masoud
hello
How to Find Area and Perimeter of a Closed LWPolygon in cadpicturebox?

Re: How to Find Area and Perimeter of a Closed LWPolygon

Posted: 08 Aug 2022, 10:42
by support
Masoud wrote:
07 Aug 2022, 00:20
hello
How to Find Area and Perimeter of a Closed LWPolygon in cadpicturebox?
Hello,
CAD .NET doesn't have ready-to-use Area and Perimeter functions. However, you can implement them yourself. For example, you could use ready-to-use api DPoint.Distance

Code: Select all

double distance_between_points = DPoint.Distance(point1, point2);
to get the distance between points and then calculate the sum of all the distances to get the perimeter of the closed LW Polyline.
As for the Area. You can calculate the area of a closed LW Polyline by using polygon area formulas (e.g. https://en.wikipedia.org/wiki/Shoelace_formula). To get information about vertexes, please, use

Code: Select all

CADLWPolyLine vLWPolyline = new CADLWPolyLine();
   CADEntityCollection vertices = vLWPolyline.Vertexes;
Best regards,
Catherine.

Re: How to Find Area and Perimeter of a Closed LWPolygon

Posted: 08 Aug 2022, 11:16
by Masoud
Thank you Catherine
I wrote this function and put it in the form so that others can use it

public class Point3D
{
public double X { get; set; }
public double Y { get; set; }
public double Z { get; set; }
public Point3D()
{
this.X = 0;
this.Y = 0;
this.Z = 0;
}
}

private double CalculateAreaPolygon(List<Point3D> Points)
{
double Sum1 = 0;
double Sum2 = 0;
for (int i = 0; i < Points.Count; i++)
{
if (i < Points.Count - 1)
{
Sum1 = Sum1 + (Points.X * Points[i + 1].Y);
}
else
{
Sum1 = Sum1 + (Points.X * Points[0].Y);
}
}
for (int i = 0; i < Points.Count; i++)
{
if (i < Points.Count - 1)
{
Sum2 = Sum2 + (Points.Y * Points[i + 1].X);
}
else
{
Sum2 = Sum2 + (Points.Y * Points[0].X);
}
}
return (Sum1 - Sum2) / 2;
}

Re: How to Find Area and Perimeter of a Closed LWPolygon

Posted: 08 Aug 2022, 14:38
by support
Masoud wrote:
08 Aug 2022, 11:16
Thank you Catherine
I wrote this function and put it in the form so that others can use it

public class Point3D
{
public double X { get; set; }
public double Y { get; set; }
public double Z { get; set; }
public Point3D()
{
this.X = 0;
this.Y = 0;
this.Z = 0;
}
}

private double CalculateAreaPolygon(List<Point3D> Points)
{
double Sum1 = 0;
double Sum2 = 0;
for (int i = 0; i < Points.Count; i++)
{
if (i < Points.Count - 1)
{
Sum1 = Sum1 + (Points.X * Points[i + 1].Y);
}
else
{
Sum1 = Sum1 + (Points.X * Points[0].Y);
}
}
for (int i = 0; i < Points.Count; i++)
{
if (i < Points.Count - 1)
{
Sum2 = Sum2 + (Points.Y * Points[i + 1].X);
}
else
{
Sum2 = Sum2 + (Points.Y * Points[0].X);
}
}
return (Sum1 - Sum2) / 2;
}


Hello,
Thank you for sharing your code snippet!

Best regards,
Catherine.

Re: How to Find Area and Perimeter of a Closed LWPolygon

Posted: 10 Feb 2023, 14:21
by RhondaDoe
support wrote:
08 Aug 2022, 10:42
Masoud wrote:
07 Aug 2022, 00:20
hello
How to Find Area and Perimeter of a Closed LWPolygon in cadpicturebox?
Hello,
CAD .NET doesn't have ready-to-use Area and Perimeter functions. However, you can implement them yourself. For example, you could use ready-to-use api DPoint.Distance

Code: Select all

double distance_between_points = DPoint.Distance(point1, point2);
to get the distance between points and then calculate the sum of all the distances to get the perimeter of the closed LW Polyline.
As for the Area. You can calculate the area of a closed LW Polyline by using polygon area formulas (e.g. https://en.wikipedia.org/wiki/Shoelace_formula). To get information about vertexes, please, use

Code: Select all

CADLWPolyLine vLWPolyline = new CADLWPolyLine();
   CADEntityCollection vertices = vLWPolyline.Vertexes;
Best regards,
Catherine.
Thanks for making my day. If I face any problem, I will message you.