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.

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()); }

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

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

Pingback: Perform geometric operations on polygons in C# |

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!

similarly how can i get area of rectangle??

For rectangles it’s easy. Just multiply the width by the height. Or to put it differently, multiply the lengths of two adjacent sides.

Well, since a rectangle is also a polygon, you can use the same function.

But the fastest way is dx*dy.

Tested this on various shapes – thank you.

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.

Yes, that’s it exactly. A lot of computer graphics seems a bit backwards because of the way Y increases downward.

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 < and >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:

<pre><tt>code

code

code</tt></pre>

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

That would work. The modulus operator will slow things down a little, but it shouldn’t matter unless the polygon has many thousands of vertices.