不规则形状的面积

发布于 2024-08-27 23:30:06 字数 100 浏览 4 评论 0原文

我有一组位于图像上的点。这组点形成一个不规则的闭合形状。我需要找到这个形状的面积。是否有任何机构用于计算面积的正常算法?或者是否有诸如 boost 之类的库提供支持?我正在使用 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++.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

红焚 2024-09-03 23:30:06

如果您的多边形很简单(除了成对的连续线段之外,它没有任何共同点),那么维基百科可以帮助您:

面积的公式为

alt text

(假设最后一个点与第一个点相同)

您可以轻松地将其实现为

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;

当然顶点必须根据它们的顺序进行排序多边形中的自然跟随..

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..

天邊彩虹 2024-09-03 23:30:06

有一个求和公式

There's a summation formula for that.

三月梨花 2024-09-03 23:30:06

您可能想要更精确,甚至可能提供一个图形示例。

例如,如果您拥有的点只是像素,那么像素数等于面积。但如果这些点是多边形的角,那么多边形的面积就不容易确定。您可以使用多边形三角剖分,并对获得的三角形面积求和。

You might want to be more precise, possibly even providing a graphical example.

For instance, if the points you have are merely pixels, then the number of pixels equals the area. But if the points are the corners of a polygon, then the area of the polygon isn't that easily determined. You'd use polygon triangulation, and sum the areas of the triangles obtained.

半葬歌 2024-09-03 23:30:06

注意:如果您不知道点的顺序并且不能保证多边形是凸的,则无法确定形状的顺序,因为产生多边形的点可能有不止一种可能的顺序。如果您确实知道多边形是凸多边形,则确定点的顺序很容易。仅按与一个特定点的角度对点进行排序,第一个点是在其自身和初始点之间形成一条线的点,以便所有其他点都位于该线的同一侧。通过此过程形成的三角形也可用于计算面积。

Note: If you don't know the order of the points and cannot guarantee that your polygon is convex, it is not possible to determine the ordering of the shape, since there may be more than one possible order the points which produces a polygon. If you do know that the polygon is convex, determining the ordering of the points is easy. Merely sort the points by angle from one particular point., with the first point being the one that forms a line between itself and the initial point such that all the other points are on the same side of the line. The triangles formed by this process can also be used to calculate the area.

昔日梦未散 2024-09-03 23:30:06

Boost.Geometry 支持多边形面积计算(Boost.Geometry 尚未接受该功能,并且使用起来非常混乱)。
否则,您必须首先确定由您的点定义的多边形。从外观上看,所有点都是多边形的顶点,因此这只是正确排序点集的问题。另一种可能性是您正在寻找点集的凸包(请参阅 http://en.wikipedia .org/wiki/Convex_hull_algorithms)。

There is support for area calculation of polygons in Boost.Geometry (which isn't yet accepted into boost and which is very confusing to use).
Otherwise you would have to determine the polygon that is defined by your points first. From the looks of it all of your points are vertices of the polygon so this is a simply a matter of ordering your point sets correctly. Another possibility is that you are looking for the convex hull of your point set (see http://en.wikipedia.org/wiki/Convex_hull_algorithms).

悸初 2024-09-03 23:30:06

Without modesty, I refer you to my answer to another question Combined area of overlapping circles. Monte Carlo is robust, easy-to-parallelise and will, eventually, give you an answer to the accuracy you require.

小女人ら 2024-09-03 23:30:06

最简单的方法可能是对形状进行三角测量并计算三角形的面积。 Dave Eberly 有一个名为(Boost 许可证)的库,可以帮助进行三角测量; 此处有更多信息。例如,查找 TriangulateEC。

The simplest way to do this is probably to triangulate your shape and calculate the area of the triangles. Dave Eberly has a library called (Boost license) that may help with the triangulation; there is more information here. Look for TriangulateEC, for example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文