## Area of a polygon

I was recently asked to find an algorithm to calculate the area of a polygon. I was given the co-ordinates of the vertices in order and I was told to assume that the polygon did not intersect itself. I was able to come up with some solution which involved triangulating the polygon, however that solution was no good as it would not work with certain types of polygons. How would you come up with this algorithm? Remember that your solution has to also work for polygons that are wierdly shaped. You cannot assume that the polygon is convex. Here are some examples….

Suppose the vertices of the polygon are given by where the nth set of co-ordinates is equal to the first, then the area of the polygon is given by the nice formula

.

A really nice solution to this problem is using Green’s Theorem. This theorem equates a line integral around the boundary of a simple region to a double integral. The theorem states that if C is a piecewise smooth, simple, closed curve in a plane and it encloses a region D, then

If we can choose L and M such that , then the double integral on the right hand side in Green’s Theorem will just calculate the area of the region D. One option is to take and .

If D is the polygon and C is the boundary, all we need to do to calculate the area of D is to calculate the line integral on the left hand side in Green’s Theorem. Consider the path integral from to of . The path is parametrized by a function from to the line joining to , and is given by

So the path integral becomes

So we are integrating from 0 to 1, the function

This simplifies to

and this is simply . So if we take the line integral over the closed path C, we get the formula for the area which is written above. Of course, if we get a negative number for the area, this would mean that C was not positively oriented and we should take the absolute value to get the area.

Source: Area Measurement: Planimeters & Green’s Theorem

### 6 Responses to “Area of a polygon”

1. haris Says:

Nice article.
It might be worth mentioning that there is a relatively easy way to check if the points are ordered in a counter-clockwise way even if they were not given as such. It’s described for instance in “Introduction to Algorithms”.
It is probably not too hard to check whether the polygon is simple either, though off the top of my head I don’t see if one can do it in linear time.

The formula kind of makes sense I guess. Here’s another way to obtain it:

If you imagine that the polygon is star-shaped, then there is a center O such that the segments joining each vertex with O are fully contained in the polygon, Then, if we imagine that the coordinate axes are centered at O, then we can join the O to each vertex, and compute the areas of the resulting triangles. The area of the triangle formed by points i and i+1 would be half the length of the cross-products , which is exactly what your formula says.

In fact, one can do an induction to get it in the general case (once the answer is known of course, or guessed by the reasoning above). Notice that if are consecutive points, and they are such that the triangle they form is contained in the polygon, then we can join with a straight line, and break the polygon into two polygons, one the triangle, and the other the polygon arising after omitting . Notice that the above formulas applied to the two polygons and then added give us exactly the formula for the big polygon, since they will have the term appearing with opposite signs, and everything else as it should be. Hence by induction that would tell us that the formula does indeed express the area of the polygon, once we know it for triangles.

All that remains now would be to show that in any polygon there should be at least three consecutive points such that the triangle they form is contained in the polygon. It is clear from the pictures, but I’m not yet 100% sure how to prove it, though I feel it should follow from the methods that tell us about the correct orientation of the vertices.

2. Apoorva Says:

Good work, Haris ! GI and I have just a couple of small observations to make, on this:

Firstly, the assumption you quote in your last paragraph is equivalent to the existence of a triangulation for any polygon, so I think one can safely assume it.

Next, instead of “star-shaped”, choose and fix ANY point in the plane, which we use to join to each vertex. If it helps, you could even put the point in “general position”, so that no two vertices are in the same line with this point. (It’s not needed for the proof.)

How does this not matter in the guessing of the formula? As you say, guess the area of a triangle, formed by joining to – and now sum this cyclically over i. The point is that the area is just the determinant with rows , and if you look at the coefficients of a,b, then they cancel out in the cyclic sum! So you are left with exactly the sum of the cross-term differences.

And finally, given that you are assuming the existence of a triangulation, you do not need to say “by induction” in proving it, once you know the guess. The moment you sum up “correctly” over any such triangulation,

(i) the areas outside the polygon cancel each other off, since they do so for every sub-summation over a triangle contained in the triangulation, and

(ii) once you sum up correctly, the triangles formed by “inside edges” also cancel each other off, being counted once each in opposite directions.

So you are left with exactly what you need – and it equals the sum of the cross-term differences.

3. Venu Says:

Here is another trignometric proof. Consider any single edge. Then drop vertical lines onto the x-axis so that it forms a trapezoid. Now as we move clockwise and sum the area of the trapezoids you should get the area of the polygon (note, some trapezoids have negative area using this notation).

If edge vertices are x1,y1 and x2,y2. The area of one trapezoid is: 0.5(X2Y2-X1Y1+X2Y1-X1Y2). The first two terms cancel out as you move around a closed polygon. Hence you are left with summation of 0.5(X2Y1-X1Y2) which is what the original author got (except for the sign).

At first glance it also appears that it should work for self intersecting poylgons too.

4. zum Says:

It works for self-intersecting polygons, yes, but looks like the self-intersected area will be included twice.

I actually found this page while looking for a solution for the three-dimensional case (finding the volume of a closed 3-dimensional mesh defined by a set of triangles), which I later found here. The solution and the proof is here:

http://www.math.niu.edu/~rusin/known-math/95/volume.poly

My guess (to which I arrived at through some serious hand-waving) is that it can be generalized into n dimensions as:

forall (t in boundary elements) sum(det(t’s vertices)) / n!

5. Arti Says:

Hello

I am trying to figure out how to calculate the number of hecka lines(no two lines parallel no two lines concurrent) if they interesect 2926 times in a plane.

any ideas on how to solve this?

thanks

All you needed to do was triangulate the polygon, such as you did, and add the signed areas of the constituent triangles together. Any area excedents and deficits will cancel each other out.

For instance, say you have a quadrilateral. To compute its area you would simply adds these 2 signed areas:

A(1,2,3) + A(1,3,4)

For a pentagon you would add

A(1,2,3) + A(1,3,4) + A(1,4,5)

For a hexagon of points 7 through 12, the area would be:

(x9(y8-y10) + x10(y9-y11) + x11(y10-y12) + x12(y11-y7) + x7(y12-y8) + x8(y7-y9))/2

And so on for however many sides. Elegant, isn’t it?

This would always give you the correct result regardless whether the given polygon is convex or not, as long as the vertices are numbered consecutively. It won’t work for computing the inner area of self intersecting polygons, though, which is what brought me here after a google search.

It would give you the inside areas minus the areas of the aberrant regions, depending on which you consider to be the inside areas.