Last active
March 24, 2020 01:38
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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