Area of a irregular shape

后端 未结 7 1681
挽巷
挽巷 2021-02-06 05:09

I have set of points which lies on the image. These set of points form a irregular closed shape. I need to find the area of this shape. Does any body which is the normal algorit

7条回答
  •  爱一瞬间的悲伤
    2021-02-06 05:55

    If you polygon is simple (it doesn't have any point in common except for the pairs of consecutive segments) then wikipedia comes to help you:

    The formula for the area is

    alt text

    (it assumes that the last point is the same of the first one)

    You can easily implement it as

    float area = 0.0f;
    
    for (int i = 0; i < numVertices - 1; ++i)
      area += point[i].x * point[i+1].y - point[i+1].x * point[i].y;
    
    area += point[numVertices-1].x * point[0].y - point[0].x * point[numVertices-1].y;
    
    area = abs(area) / 2.0f;
    

    Of course vertices must be ordered according to their natural following in the polygon..

提交回复
热议问题