views:

256

answers:

4

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?

+6  A: 

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);
}
Dead account
+1 I think guys are too busy implementing to upvote you! :)
TheMachineCharmer
when I sleep I dream code
Dead account
I dream about heavenly bodies ;)
TheMachineCharmer
Hmm, nice but does this take care of concavity (tweesting polygone :) ?
Manitra Andriamitondra
TheMachineCharmer
As I've mentioned in my answer, there is no ready-made polygon: you will need to create one.
Jacob
+1  A: 

Defining the "area" of your collection of points may be hard, e.g. if you want to get the smallest region with straight line boundaries which enclose your set then I'm not sure how to proceed. Probably what you want to do is calculate the area of the convex hull of your set of points; this is a standard problem, a description of the problem with links to implementations of solutions is given by Steven Skiena at the Stony Brook Algorithms repository. From there one way to calculate the area (it seems to me to be the obvious way) would be to triangulate the region and calculate the area of each individual triangle.

Nathan
The "smallest region with straight line boundaries" has area 0 and encloses a minimum spanning tree.
Svante
Yep, fair enough comment. I suppose there may be reasonable constraints that can be put on this problem to give solutions other than the convex hull (e.g. allowing for "large" holes by allowing for arbitrary edges with the restriction that the number of edges is O(n^{1/2})), but any such problem is likely to be extremely hard to solve.
Nathan
Picking nits here, but the region with the shortest straight line boundaries would actually be a Steiner tree, which is in general shorter than the MST.
The only guarantee you can make with the area of convex hull is that it will be an **upper bound**.
Jacob
+1  A: 

You can use Timothy Chan's algorithm for finding convex hull in nlogh, where n is the number of points, h is the number of convex hull vertices. If you want an easy algorithm, go for Graham scan.

Also, if you know that your data is ordered like a simple chain, where the points don't cross each other, you can use Melkman's algorithm to compute convex hull in O(N).

Also, one more interesting property of convex hull is that, it has the minium perimeter.

Algorist
A: 

Your problem does not directly imply that there's a ready-made polygon (which is assumed by this answer). I would recommend a triangulation such as a Delaunay Triangulation and then trivially compute the area of each triangle. OpenCV (I've used it with a large number of 2D points and it's very effective) and CGAL provide excellent implementations for determining the triangulation.

Jacob