There are several algorithms to find the area of a polygon with and without triangulation:
- Direct area calculation. There are formulas for directly calculating the area of a simple polygon given its vertices or given the length of its sides and the exterior angles are known. I will not repeat them here because they don't provide an interesting programming target. You can read about it here: http://en.wikipedia.org/wiki/Polygon#Area_and_centroid
- Polygon triangulation. Polygon triangulation is an algorithmic process. One well know algorithm is called "Ear clipping". The idea is that every simple polygon has at least one "ear", where an ear is a triangle that's wholly contained inside the polygon and whose vertices are three consecutive vertices of the polygon. Once you find such an ear you can remove it and end up with a polygon with one less vertex. If you repeat this process you will end up with a triangle (the last ear) and then you're done. This is an elegant algorithm with a non-trivial theoretical background (the proof that any simple polygon always has ears is not trivial). Another algorithm uses sweep lines to decompose the original polygon to monotone polygons (http://en.wikipedia.org/wiki/Monotone_polygon), which are easy to triangulate.
- The poly area algorithm is a hybrid of these two algorithms using a divide-and-conquer approach. It is looking for an ear to clip, but it's not looking very hard. If it runs into trouble it splits the polygon into two separate polygons and starts all over with each one of the smaller polygons. Over time the polygon may be split into multiple sub-polygons and the algorithm manages all of them and collectes all the removed triangles.
I will use the following terms to describe the algorithm so let's define them clearly.
- Top point: The polygon vertex with the highest Y coordinate. If there are multiple vertices with the same high Y coordinate, then the vertex with lowest X coordinate (leftmost) is the top point.
- Second top point: The vertex with the highest Y coordinate that is smaller than the top point's Y coordinate. If there are multiple such points then one of them is selected based on some rules. Note, that there may be vertices that are not the top point whose (Y coordinate is equals to the top point's Y coordinate) that are above the second top point.
- Top triangle: There are two cases here. The first case is if the top point is followed by a point whose Y coordinate is smaller. That means the top point is a spike (see Figure 1).
The second case is that the top point is followed by a point with the same Y coordinate and it forms a plateau at the top (see Figure 2). Note that the top point is always preceded by a point with a lower Y coordinate (emergent property from the definition of the top point).
In the first case the top triangle will consist of the top point and the two points that are the intersection of a horizontal sweep line that goes through the second top point and the two sides of the polygon that meet at the top point. The second top point may or may not be in the top triangle (see Figure 3).
In the second case the top triangle consists of the top point, the point that follows it and the intersection of the left side of the polygon that ends in the top point with the sweep-line that goes through the second top point. Again, this intersection point may or may not be the second top point itself (see Figure 4)