18. Polygon area

The hint said to consider the trapezoids defined by the polygon's edges, the X axis, and sides dropped vertically from the polygon's vertices to the X axis. The following diagram shows a polygon with one of those trapezoids shaded:

Recall that a trapezoid's bases are its parallel sides. Also recall that a trapezoid with height h and base lengths b1 and b2 has an area of h × (b1 + b2) / 2. To calculate a polygon's area, you can simply add the areas of its trapezoids.

You need to understand two non-obvious facts to make this work. First, notice that the shaded trapezoid in the preceding diagram includes an area between the polygon and the X axis that obviously should not be included in the polygon's area. To see why this isn't a problem, note that the X coordinate of point F is greater than the coordinate of point G, so when you subtract Gx – Fx, you get a negative number. That means the trapezoid that uses the F-G segment as its top subtracts some of the area that lies below the polygon.

The trapezoids that use the polygon's top edges add the extra space below the polygon, but then the trapezoids that use the polygon's bottom edges subtract that space out again leaving only the polygon's area.

The second non-obvious fact is that the sum of the trapezoids' areas may be negative depending on the polygon's orientation. In the preceding diagram, the polygon is oriented clockwise so, for example, the X coordinate of point C is greater than the X coordinate of point B. That means the top trapezoids such as the shaded one have positive areas.

If the polygon's points were oriented counter-clockwise, then those trapezoids would have negative areas. When you add all of the areas, the final result will be negative. If the result is negative, simply take its absolute value and you'll have the polygon's correct area.

This effect is reversed in C# because Y coordinates increase from the top down instead of from the bottom up.

The following PolygonArea method uses this technique to calculate a polygon's area:

// Calculate the polygon's area.
private float PolygonArea(List<Point> points)
{
int numPoints = points.Count;
if (numPoints < 3)
throw new Exception(
"The polygon must have at least three vertices");

// Repeat the first point at the end for convenience.
points.Add(points[0]);

// Loop over the polygon's segments.
float area = 0;
for (int i = 0; i < numPoints; i++)
{
float width = points[i + 1].X - points[i].X;
area += width * (points[i + 1].Y + points[i].Y) / 2f;
}

// Remove the repeated first point.
points.RemoveAt(numPoints);

return Math.Abs(area);
}

This method first verifies that the polygon has at least three points and throws an exception if it does not.

It then copies the polygon's first point to the end of the polygon's point list to make it easier to loop over the polygon's edges.

Next, the method loops over the polygon's edges and adds the areas of their trapezoids to a total. After it finishes, the method removes the copy of the first point that it added to the point list and returns the total area.

Download the PolygonArea example solution to see additional details.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.77.5