Is there any algorithm for calculating area of a shape given co-ordinates that define the shape?

 

Questions


So I have some function that receives N random 2D points.

Is there any algorithm to calculate area of the shape defined by the input points?

 

 

————————————————-

Answer

You want to calculate the area of a polygon?

(Taken from link, converted to C#)

class Point { double x, y; } 

double PolygonArea(Point[] polygon)
{
   int i,j;
   double area = 0; 

   for (i=0; i < polygon.Length; i++) {
      j = (i + 1) % polygon.Length;

      area += polygon[i].x * polygon[j].y;
      area -= polygon[i].y * polygon[j].x;
   }

   area /= 2;
   return (area < 0 ? -area : area);
}

algorithm,c#,geometry

Facebook Comments

Post a comment