Calculate the surface/area of an irregular polygon

0 Flares Twitter 0 Facebook 0 Google+ 0 Email -- 0 Flares ×

There are many ways to calculate the surface of a polygon. It all depends on what kind of data you have to deal with. If you have only vertices (points) or if you have the segments associated to them as well.
The most evident solution would be to divide the polygon in triangles and calculate the surface of them. But this can quickly become complicated as you will have to test every segment of every triangle and check if they are intersecting with a current polygon’s segment or if it goes outside the polygone. Many calculations that would lead to many problems…

The solution

But fortunately for us, there is a simple way to solve this problem, as long as the polygon’s segments don’t intersect.
Here is a sample code in ActionScript 3 (that can be easily translated in other languages):

[sourcecode language=’as3′]
// populate an array with each vertex of the polygon (vertices must be following)
var a:Array = new Array();
a.push(new Point(0, 0));
a.push(new Point(4, 0));
a.push(new Point(4, 1));
a.push(new Point(6, 1));
a.push(new Point(6, 0));
a.push(new Point(10, 0));
a.push(new Point(10, 4));
a.push(new Point(6, 4));
a.push(new Point(6, 3));
a.push(new Point(4, 3));
a.push(new Point(4, 4));
a.push(new Point(0, 4));

// loop through each vertex and calculate the surface/area
var i:int = 0;
var j:int = 0;
var n:int = a.length;
var surface:Number = 0.0;

for(i=0 ; iWhat does it do ?

It takes the x coordinate of the current vertex and multiply it by the y coordinate of the next vertex.

[sourcecode language=’as3′]
surface += Point(a[i]).x * Point(a[j]).y;

Then it subtracts from it the result of the y coordinate of the current vertex multiplied by the x coordinate of the next vertex.

[sourcecode language=’as3′]
surface -= Point(a[i]).y * Point(a[j]).x;

Then divide the result by 2 to get the surface/area.


If you get a negative number, it simply means that you took the vertexes in an anti-counterclockwise order. Just take the absolute value of it and you’ll be done.


0 Flares Twitter 0 Facebook 0 Google+ 0 Email -- 0 Flares ×


  1. ricky

    this just helped me. thanks for posting.

  2. Chiwek

    Helped me too, thanks!

  3. Michael

    Works wonderful! Thanks a lot=D

Leave a Comment