mono / SkiaSharp

SkiaSharp is a cross-platform 2D graphics API for .NET platforms based on Google's Skia Graphics Library. It provides a comprehensive 2D API that can be used across mobile, server and desktop models to render images.
MIT License
4.52k stars 539 forks source link

[QUESTION] Is there a way to calculate SKPath.Area? #1876

Closed leonbohmann closed 2 years ago

leonbohmann commented 2 years ago

After searching the web for quite some time now, I can't find appropriate answers, for which the questions are quite simple:

a) Is there a method to calculate the area of an SKPath? b) Is there a way I can iterate through the SKPath and calculate the area on my own? With Gauss, maybe?

AnarchyMob commented 2 years ago

You can do this:

var pathMeasure = new SKPathMeasure(path);
float length = pathMeasure.Length;
float side = length / 4f;
float area = side * side;
leonbohmann commented 2 years ago

Sure, but that is only valid for rectangles. What if I add a beziers?

AnarchyMob commented 2 years ago

In theory, it should work with any curves. Need to test with closed curves.

leonbohmann commented 2 years ago

See below picture. There, I don't even have 4 'sides'. grafik I can't calculate that using (side)², can I?

Of course I need some kind of abbreviation since a bezier can't be integrated easily. What I could do is using triangles to approximate curves.

AnarchyMob commented 2 years ago

We just interpret the length of the curve as a square, that's all.

AnarchyMob commented 2 years ago

Yes, I was wrong, the algorithm is somewhat more complicated. you need to split the curve into polygons and calculate their total area.

StackOverflow Google groups

AnarchyMob commented 2 years ago

You can try using SKRegion with CreateRectIterator...

leonbohmann commented 2 years ago

That is an awesome idea! I tried it with SkiaSharpFiddle: grafik I will iterate over the rectangles and calculate their area and just sum it up.

Thanks a lot!