How to Find Area and Perimeter of a Closed LWPolygon

Discuss and ask questions about CAD .NET library.

Moderators: SDS, support, admin

Post Reply
Masoud
Posts: 13
Joined: 25 May 2022, 18:57

How to Find Area and Perimeter of a Closed LWPolygon

Post by Masoud » 07 Aug 2022, 00:20

hello
How to Find Area and Perimeter of a Closed LWPolygon in cadpicturebox?

support
Posts: 3254
Joined: 30 Mar 2005, 11:36
Contact:

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

Post by support » 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.
Technical Support E-mail: support@cadsofttools.com
Chat support on Skype: cadsofttools.support

Masoud
Posts: 13
Joined: 25 May 2022, 18:57

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

Post by Masoud » 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;
}

support
Posts: 3254
Joined: 30 Mar 2005, 11:36
Contact:

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

Post by support » 08 Aug 2022, 14:38

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.
Technical Support E-mail: support@cadsofttools.com
Chat support on Skype: cadsofttools.support

RhondaDoe
Posts: 1
Joined: 06 Feb 2023, 14:26

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

Post by RhondaDoe » 10 Feb 2023, 14:21

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.
Last edited by RhondaDoe on 18 Feb 2023, 14:53, edited 1 time in total.

Post Reply