pythonsvgsvg-path

How do I get a coordinates list from an svgpathtools bezier curve?


I have python code to create a bezier curve, from which I create a bezier path.

Here are my imports:

import from svgpathtools import Path, Line, CubicBezier

Here is my code:

    bezier_curve = CubicBezier(start_coordinate, control_point_1, control_point_2, end_coordinate)
    bezier_path = Path(bezier_curve)

I would like to create a list of coordinates that make up this curve, but none of the documentation I am reading gives a straightforward way to do that. bezier_curve and bezier_path only have parameters for the start point, end point, and control point.


Solution

  • Seems like a pretty reasonable question. Surprised there's no answer. I had to do this myself recently, and the secret is point().

    Here's how I got it done, using your boilerplate as a starting point:

    from svgpathtools import Path, Line, CubicBezier
    
    bezier_curve = CubicBezier(start=(300+100j), control1=(100+100j), control2=(200+200j), end=(200+300j))
    bezier_path = Path(bezier_curve)
    
    NUM_SAMPLES = 10
    
    myPath = []
    for i in range(NUM_SAMPLES):
        myPath.append(bezier_path.point(i/(NUM_SAMPLES-1)))
    
    print(myPath)
    

    Output:

    [(300+100j), (243.8957475994513+103.56652949245542j), (206.72153635116598+113.71742112482853j), (185.1851851851852+129.62962962962962j), (175.99451303155004+150.480109739369j), (175.85733882030178+175.44581618655695j), (181.4814814814815+203.7037037037037j), (189.57475994513032+234.43072702331963j), (196.84499314128942+266.8038408779149j), (200+300j)]