【问题标题】:How do I get a coordinates list from an svgpathtools bezier curve?如何从 svgpathtools 贝塞尔曲线中获取坐标列表?
【发布时间】:2017-09-09 01:02:58
【问题描述】:

我有创建贝塞尔曲线的 python 代码,我从中创建贝塞尔路径。

这是我的导入:

import from svgpathtools import Path, Line, CubicBezier

这是我的代码:

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

我想创建一个组成这条曲线的坐标列表,但我正在阅读的documentation 没有一个提供直接的方法来做到这一点。 bezier_curve 和 bezier_path 只有起点、终点和控制点的参数。

【问题讨论】:

  • “构成这条曲线的坐标”是什么意思? bezier_curve.bpoints() 将返回元组 (start_coordinate, control_point_1, control_point_2, end_coordinate),但听起来这不是你要找的。贝塞尔曲线是多项式,例如,三次贝塞尔曲线完全由它的四个系数决定。有关更多信息,请查看:en.wikipedia.org/wiki/…

标签: python svg svg-path


【解决方案1】:

似乎是一个非常合理的问题。很惊讶没有答案。我最近不得不自己做,秘密是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)

输出:

[(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)]

【讨论】:

    【解决方案2】:

    上面给出的答案对我来说非常有效。我只需要对代码做一点小小的修改:

    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/(**float(NUM_SAMPLES)**-1)))
    
    print(myPath)
    

    更改 i/(NUM_SAMPLES -1) by i/(float(NUM_SAMPLES) -1) 可确保当曲线从 0 参数化到 1 时行为正确。否则只会产生整数除法。

    【讨论】:

      【解决方案3】:
      #to demonstrate lines and cubics, improving readibility
      
      from svgpathtools import Path, Line, CubicBezier
      
      cubic = CubicBezier(300+100j, 100+100j, 200+200j, 200+300j)  # A cubic beginning at (300, 100) and ending at (200, 300)
      line = Line(200+300j, 250+350j)  # A line beginning at (200, 300) and ending at (250, 350)
      
      number_of_points = 10
      
      cubic_points = []
      
      for i in range(number_of_points):
          cubic_points.append(cubic.point(i/(NUM_SAMPLES-1)))
      
      print('cubic points', path_points)
      
      line_points = []
      
      for i in range(number_of_points):
          line_points.append(line.point(i/(NUM_SAMPLES-1)))
      
      print('line points', path_points)
      

      【讨论】:

      • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-25
      • 2014-12-27
      • 2014-11-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-10
      • 1970-01-01
      相关资源
      最近更新 更多