不规则形状的面积 [英] Area of a irregular shape
问题描述
我有一套点在图像上。这些点形成不规则的闭合形状。我需要找到这个形状的区域。是否任何身体是用于计算面积的正常算法?或者在图书馆有任何支持,如提高?我使用C ++。
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 algorithm used for calculating the area ? Or is there any support available in libraries such as boost? I am using C++.
推荐答案
如果你的多边形是简单的(它没有任何共同点,连续段),wikipedia来帮助你:
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:
该区域的公式是
(假设最后一点与第一个相同)
(it assumes that the last point is the same of the first one)
您可以轻松实现
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..
这篇关于不规则形状的面积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!