mathandy / svgpathtools

A collection of tools for manipulating and analyzing SVG Path objects and Bezier curves.
MIT License
532 stars 134 forks source link

Method to get centroid of path #173

Closed niutech closed 2 years ago

niutech commented 2 years ago

I would like to scale a closed path (shape) using its centroid as an origin. How can I get the centroid? Could you please add this method to the Path class?

mathandy commented 2 years ago

This would be a great addition if you want to implement such a method (add tests for it) and make a pull request.

Thanks for the feature request!

niutech commented 2 years ago

Could you tell me how to calculate it? There is a formula for a polygon, but what about Bezier curves? And how to get coordinates of all points of a path?

mathandy commented 2 years ago

I'm unaware of any exact formula for bezier curves, but you can estimate the centroid (I imagine quite accurately) using the scipy quad function and stoke's theorem. Here's an explanation https://math.stackexchange.com/questions/3274160/finding-centroid-of-a-curve