Is there any algorithm for calculating area of a s

2019-03-13 08:32发布

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?

5条回答
叛逆
2楼-- · 2019-03-13 09:06

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.

查看更多
乱世女痞
3楼-- · 2019-03-13 09:15

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);
}
查看更多
仙女界的扛把子
4楼-- · 2019-03-13 09:21

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.

查看更多
虎瘦雄心在
5楼-- · 2019-03-13 09:26

I found another function written in Java , so i traslated it to C#

public static double area(List<Double> lats,List<Double> lons)
{       
double sum=0;
double prevcolat=0;
double prevaz=0;
double colat0=0;
double az0=0;
for (int i=0;i<lats.Count;i++)
{
    double colat=2*Math.Atan2(Math.Sqrt(Math.Pow(Math.Sin(lats[i]*Math.PI/180/2), 2)+ Math.Cos(lats[i]*Math.PI/180)*Math.Pow(Math.Sin(lons[i]*Math.PI/180/2), 2)),
        Math.Sqrt(1-  Math.Pow(Math.Sin(lats[i]*Math.PI/180/2), 2)- Math.Cos(lats[i]*Math.PI/180)*Math.Pow(Math.Sin(lons[i]*Math.PI/180/2), 2)));
    double az=0;
    if (lats[i]>=90)
    {
        az=0;
    }
    else if (lats[i]<=-90)
    {
        az=Math.PI;
    }
    else
    {
        az=Math.Atan2(Math.Cos(lats[i]*Math.PI/180) * Math.Sin(lons[i]*Math.PI/180),Math.Sin(lats[i]*Math.PI/180))% (2*Math.PI);
    }
    if(i==0)
    {
         colat0=colat;
         az0=az;
    }           
    if(i>0 && i<lats.Count)
    {
        sum=sum+(1-Math.Cos(prevcolat  + (colat-prevcolat)/2))*Math.PI*((Math.Abs(az-prevaz)/Math.PI)-2*Math.Ceiling(((Math.Abs(az-prevaz)/Math.PI)-1)/2))* Math.Sign(az-prevaz);
    }
    prevcolat=colat;
    prevaz=az;
}
sum=sum+(1-Math.Cos(prevcolat  + (colat0-prevcolat)/2))*(az0-prevaz);
return 5.10072E14* Math.Min(Math.Abs(sum)/4/Math.PI,1-Math.Abs(sum)/4/Math.PI);
}
查看更多
Explosion°爆炸
6楼-- · 2019-03-13 09:27

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.

查看更多
登录 后发表回答