PDA

View Full Version : Finding the area of a polygon

cutting_crew
08-28-2008, 02:34 PM
i have a polygon with a set of known vertices but i keep getting negative numbers for my areas. i was wondering what i am doing wrong?

i looked up the formula here. (http://en.wikipedia.org/wiki/Polygon) where it talks about in the middle about getting the area and the centroid and in this case i want to find A.
the point A is what i want to find. the first set of points happens to be:

-89.409 30.306
-89.409 30.308
-89.408 30.305
-89.407 30.304
-89.409 30.306

as you can see by the points this polygon going clockwise, therefore it is possible to get a negative number(this is referred to in the wiki article) but i wanted to be sure that i got the correct answer and am doing this correctly.

area = (-89.409 * 30.308 + -89.409 * 30.305 + -89.408 * 30.304 + -89*407 * 30.306) - (30.306 * -89.409 + 30.308 * -89.408 + 30.305 * -89.407 + 30.304 * -89.409)

area = area / 2;

answer = -0.2. can anyone confirm? and then thus taking the absolute value would give a final answer of 0.2. thanks all for the help!

Rosario Leonardi
08-28-2008, 04:56 PM

The vertices must be ordered clockwise or counterclockwise; if they are ordered clockwise the area will be negative but correct in absolute value.
It also state that the formula work if the polygon is not overlapping.
http://img409.imageshack.us/img409/7538/polygonareazr5.jpg (http://imageshack.us)
Notice that your polygon have a double point (A = E)
BTW The area of this polygon is (OC*AB)/2 = 10^-6

cutting_crew
08-28-2008, 08:00 PM

The vertices must be ordered clockwise or counterclockwise; if they are ordered clockwise the area will be negative but correct in absolute value.
It also state that the formula work if the polygon is not overlapping.
http://img409.imageshack.us/img409/7538/polygonareazr5.jpg (http://imageshack.us)
Notice that your polygon have a double point (A = E)
BTW The area of this polygon is (OC*AB)/2 = 10^-6

yes but the first and last point are always duplicates for the purpose of closing the polygon. if you draw out each line segment you would see that this is possible. also as far as i can read this is part of the formula that is requires for calculation concave polygons.

someone else might be able to correct me on that.

Rosario Leonardi
08-29-2008, 06:59 AM
Yep you are right* :)
I write a simple C++ program to try the formula and it works, the result is
-1.00016 e-6

If I invert the order of the point (counter clockwhise) I get a positive value
9.96806e-007
The errors are caused by floating approximation, this algorithm work with large number to produce small result, this lead to a non-irrelevant cancellation error.

You can reduce the approximation error translating all the point near the origin.

Here is the code:

#include "stdafx.h"
#include <iostream>
struct point{
double x, y;
};

int main(int argc, char* argv[])
{
point polygon[5];
float area = 0.0;
polygon[0].x = -89.409;
polygon[0].y = 30.306;
polygon[1].x = -89.409;
polygon[1].y = 30.308;
polygon[2].x = -89.408;
polygon[2].y = 30.305;
polygon[3].x = -89.407;
polygon[3].y = 30.304;
polygon[4].x = -89.409;
polygon[4].y = 30.306;

for(int i = 0; i < 4; ++i)
area += polygon[i].x * polygon[i+1].y - polygon[i+1].x * polygon[i].y;
area *= 0.5;

std::cout << area;

return 0;
}

I write a C++ code cause I tried a lot of time with calculator but I always get a different result. :D

Note:
*I must stop posting in the night.