Skip to content

Instantly share code, notes, and snippets.

@vmilev
Last active June 9, 2016 12:20
Show Gist options
  • Save vmilev/f3d65780351a4bfe248d24f3bd0bd01c to your computer and use it in GitHub Desktop.
Save vmilev/f3d65780351a4bfe248d24f3bd0bd01c to your computer and use it in GitHub Desktop.
An extension method to calculate the surface area (in squared pixels) of a single polygon represented by a GraphicsPath object
public static class GraphicsPathExtensions
{
public static float ComputeArea(this GraphicsPath graphicsPath)
{
var points = graphicsPath.PathPoints.ToList();
//Add the first point as the last in order to close the figure and compute area properly.
points.Add(points[0]);
return Math.Abs(points.Take(points.Count - 1).Select((p, i) => p.X * points[i + 1].Y - p.Y * points[i + 1].X).Sum()) / 2;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment