Given an SVG (which contains a single path object), I want to divide the path into n segments of equal length. The returned value should be list of (n+1) points (x, y) coordinates in rasterized svg indicating the ends of every segment
Example:
Given SVG : (this example has only straight lines but the solution must work for all types of paths including cubic and quadratic beziers)
<svg height="210" width="400">
<path d="M150 0 L75 200 L225 200 Z" fill="none" stroke="black" stroke-width="4" />
</svg>
renders as
Expected output : coordinates of each of the red dots below considering bottom left corner as origin. The SVG is rendered at width=100px always
I am using python and svgpathtools, how can I achieve this ?
My approaches :
- Take a look at SVG rasterizers. See how they handle paths. I took a look at this but sadly couldn't understand the code well enough to implement it myself
- Add a
stroke-dasharrayto my path.stroke-dasharraywill "break" the continuous stroke into equal sized segments automatically. Then rasterize this modified SVG, then use something like OpenCV to cluster the rendered stroke. Then find midpoint of these clusters to get the answer. This approach is terribly slow due to multiple slow processes (rasterization + clustering) involved in it. It is also not very flexible in case a path crosses itself (infinity sign) since thestroke-dasharraywill also intersect giving a weird intersection point - I had a look at this question but I am not sure how this will work in my case where the SVG is always rendered at width=100px (height autocalculated based on aspect ratio of SVG), moreover the approach mentioned here is supposedly "slow". Moreover, I am not sure if this linked question gets me points "on" the path or the control points for svg path
While approach 2 works for many images, I certainly don't want to go that way because it's horribly slow. Is there a better approach ?

