Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Area of a irregular shape

Tags:

c++

math

shapes

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

like image 594
Naveen Avatar asked Mar 31 '10 13:03

Naveen


3 Answers

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

like image 179
Jack Avatar answered Nov 12 '22 11:11

Jack


There's a summation formula for that.

like image 21
Ignacio Vazquez-Abrams Avatar answered Nov 12 '22 13:11

Ignacio Vazquez-Abrams


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.

like image 1
MSalters Avatar answered Nov 12 '22 12:11

MSalters