09.Jun.16

Vladimir Milev

Vladimir Milev

Code Bites: Calculating area of a shape in a GraphicsPath object

The Problem


I have recently been writing a lot of code related to drawing geometrical shapes in Winforms. It is of course natural to use the GraphicsPath object for most of the work. While the APIs provided by .NET/GDI are quite sufficient for most tasks there was a client requirement that was a bit more unusual - we needed to be able to calculate the surface area of a polygon shape displayed on the screen.

The Solution


It turns out there is surprisingly little resources for this task readily available online. The problem is also not super trivial. After some research on the topic I've found out that there is a simple enough algorithm that while not perfect is good enough for most of our needs and is trivial to implement - enter the shoelace formula. Explained simply, this formula works in the following way: Calculate the product of the current point X coordinate and the next point Y coordinate on one hand and the product of the current point Y coordinate and the next point X coordinate. Subtract the second from the first, sum up all these results and take their absolute. At the end you should divide by two.

I've implemented this algorithm using C# and for convenience packaged it as an extension method to the GraphicsPath object. To calculate the area of a figure you should simply include a using statement for the GraphicsPathExtensions namespace and call the .ComputeArea() method which will return a float value with the area calculation.

Here is my implementation:

A note on multiple polygons in a single GraphicsPath object.

The current implementation works for just one polygon in a GraphicsPath. If you wish feel free to extend it. You will have to look at the PointData and check the point types. If you have multiple shapes each new figure point type will be zero. You can use that information to split the point data for each individual shape, use the above algorithm for each one and sum up the results.

And as always... happy coding!

Need consulting on this topic?

Yes No