Calculate the area of a polygon in C#

Calculate polygon area

Trapezoid area

You can calculate the area of a polygon by adding the areas of the trapezoids defined by the polygon’s edges dropped to the X-axis. If two adjacent points along the polygon’s edges have coordinates (x1, y1) and (x2, y2) as shown in the picture on the right, then the area (shown in blue) of that side’s trapezoid is given by:

area = (x2 - x1) * (y2 + y1) / 2

When the program adds up all of the trapezoid areas, the sides on the polygon’s bottom give negative areas because x1 > x2. Those areas cancel out the parts of the other trapezoids that lie outside of the polygon as shown in the picture below.

Combining trapezoid areas

This method gives strange results for self-intersecting polygons, although it does work if the polygon intersects the X axis.

The following code shows how the example’s Polygon class calculates a polygon’s “signed” area. The result is either the area or the negative off the area if the polygon is oriented backwards. (In other words, if it turns out the program adds the bottom area and subtracts the top area in the picture above.)

// Return the polygon's area in "square units."
// The value will be negative if the polygon is
// oriented clockwise.
private float SignedPolygonArea()
    // Add the first point to the end.
    int num_points = Points.Length;
    PointF[] pts = new PointF[num_points + 1];
    Points.CopyTo(pts, 0);
    pts[num_points] = Points[0];

    // Get the areas.
    float area = 0;
    for (int i = 0; i < num_points; i++)
        area +=
            (pts[i + 1].X - pts[i].X) *
            (pts[i + 1].Y + pts[i].Y) / 2;

    // Return the result.
    return area;

This code creates an array of PointF objects and copies the polygon’s points into it. It then repeats the first point in the last array entry to make looping over the polygon’s line segments easier.

The code then loops over the polygon’s segments, calculates the area under each, adds them up, and returns the total.

The total calculated area is negative if the polygon is oriented clockwise. (I’ll post more on polygon orientation soon.)

The following PolygonArea method simply returns the absolute value of the result given by the SignedPolygonArea method.

// Return the polygon's area in "square units."
public float PolygonArea()
    // Return the absolute value of the signed area.
    // The signed area is negative if the polyogn is
    // oriented clockwise.
    return Math.Abs(SignedPolygonArea());

Download Example   Follow me on Twitter   RSS feed

This entry was posted in algorithms, geometry, graphics, mathematics and tagged , , , , , , , , , , , , , . Bookmark the permalink.

14 Responses to Calculate the area of a polygon in C#

  1. Pingback: Determine a polygon's is orientation in C#

  2. Pingback: Find the centroid of a polygon in C# |

  3. Pingback: Perform geometric operations on polygons in C# |

  4. I’ve Been thinking 3 days long on how to solve this polygon area and orientation issue!
    I’ve tried cross-product with no results (that was a big failure and waste of time), and concluded that concave polygons orientation can’t be predicted in general with vector-product. I’ve tried a crazy integration algorithm and finally somehow solved it, but the code is a mess and it’s not a general solution.

    Today I’ve stumbled on your solution and it’s beautifully elegant, this is what I was looking for. Thank you!

  5. kavin says:

    similarly how can i get area of rectangle??

  6. Andre Belcher says:

    Tested this on various shapes – thank you.

  7. R. Hoek says:

    One more thing about the CounterClockwise orientation of the polygon.
    I’ve tried this, but it gives me the exact opposite conclusion (area is negative when counterclockwise oriented).

    Maybe it’s because my polygons are in a normal mathematical xy grid.
    When I then look at a CounterClockwise polygon, the lower vertices al have x1 < x2 and thus result in a positie surface size. Where as the upper vertices have x1 > x2 and thus result in negative surface sizes (which are the lager surface sizes). So when these are summed, the CounterClockwise polygons have a negative summed surface size.

  8. R. Hoek says:

    One small thing about this sites comment option: it seems to allow html tags. So I had to escape the < and > characters html based using &lt; and &gt;

    • RodStephens says:

      Yes, it does allow a limited number of HTML features, so yes you need to escape <, >, ≤, ≥, and other special symbols. I try to edit peoples’ comments to fix things like unescaped characters if I can tell there’s a problem. I don’t think it allows javascript, though.

      It does allow anchors like this one.

      One of the more useful things is the ability to use fonts. For example, you can format code like this:


  9. Houcine says:

    Use iteration from 0 to modulus num_point instead of copying last point to first one.

Comments are closed.