Channels ▼
RSS

.NET

The PolyArea Project: Part 1


Existing Algorithms

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.

Terminology

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).

    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).

    Figure 2

    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).

    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)

  • Figure 4


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Video