Skip to content

Instantly share code, notes, and snippets.

@vexx32
Last active March 24, 2020 01:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vexx32/c02da9980054a1a345f142faadfad6af to your computer and use it in GitHub Desktop.
Save vexx32/c02da9980054a1a345f142faadfad6af to your computer and use it in GitHub Desktop.
Gets the approximate area enclosed by an SKPath instance by comparing its total bounded area to the percentage of evenly-distributed points that are "contained" within the path.
internal static float GetEnclosedArea(this SKPath path)
{
SKRect bounds = path.TightBounds;
var boundedArea = bounds.Width * bounds.Height;
var totalPoints = 10000;
var enclosedPoints = 0;
for (float x = bounds.Left; x < bounds.Right; x += bounds.Width / (float)Math.Sqrt(totalPoints))
{
for (float y = bounds.Top; y < bounds.Bottom; y += bounds.Height / (float)Math.Sqrt(totalPoints))
{
if (path.Contains(x, y))
{
enclosedPoints++;
}
}
}
var enclosedAreaRatio = enclosedPoints / totalPoints;
return enclosedAreaRatio * boundedArea;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment