3

我有 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)

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

4

3 回答 3

6

似乎是一个非常合理的问题。很惊讶没有答案。我最近不得不自己做这个,秘诀是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)]
于 2017-08-22T01:42:03.650 回答
1

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

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)

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

于 2018-06-08T16:54:47.730 回答
0
#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)
于 2021-10-28T22:54:12.527 回答