How can i calculate the area of a polygon in c++ only by knowing the x and y coordonates of the points which make the polygon?
-
3This is more a question for Math SE imo. Even then, you haven't shown any effort. – chris Mar 05 '13 at 19:20
-
2(1) Work out how you would do it on a piece of paper. (2) Translate into computer program. (3) Success! – us2012 Mar 05 '13 at 19:21
-
1**(4):** if !(3) post your question here. – Maroun Mar 05 '13 at 19:21
-
@MarounMaroun, `!(3)` is always false, though :p – chris Mar 05 '13 at 19:22
-
@chris sadly, he'll never post questions here xD – Maroun Mar 05 '13 at 19:23
-
if it helps somebody: I answered it here.. http://stackoverflow.com/a/43174368/3719699 – abe312 Apr 02 '17 at 23:01
2 Answers
A simple google search shows the answer provided that you are dealing with non-self-intersecting polygons. The sign of the area is positive if the points on the polygon are arranged in counterclockwise order. This formula does not assume that the polygon is convex.
http://mathworld.wolfram.com/PolygonArea.html
Here, the area is found by summing the determinent of neighboring points. Each determinent computes the area of the parallelogram formed by the vector e.g. (x1,y1) and (x2,y2) (where both vectors stem from the origin (0,0)). The division by 2 gives the area of a triangle. When traveling around the polygon, the triangles will have a positive area if your polygon is convex. Otherwise, negative areas of these triangles will cancel with their positive counterparts for the case of a concave polygon giving you the correct result.

- 146
- 9