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++.
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
(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..
There's a summation formula for that.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With