让我们使用一个简单的 PyX 图来生成一些路径:

路径也可以来自以解析模式读取的 SVG 文件。
一旦有了 PyX 路径,就可以使用 PyX 功能来获取有关路径的更多信息。在下面的简单版本中,我计算了每条路径上的几个点并总结了它们的距离。(我使用以 结尾的方法名称_pt
,它在 PostScript 点中工作。它比使用 PyX 单元快一点。此外,我在开始时将所有路径显式转换为规范路径。虽然这不是必需的,但它有助于减少一些函数调用内部。)
这是完整的代码(包括生成示例路径的图表):
import math
from pyx import *
# create some data (and draw it)
g = graph.graphxy(width=10, x=graph.axis.lin(min=0, max=2*math.pi))
qpi = g.plot(graph.data.function("y(x)=sin(x)"))
opi1 = g.plot(graph.data.function("y(x)=sin(x)+0.1*sin(10*x)"))
opi2 = g.plot(graph.data.function("y(x)=sin(x)+0.2*sin(20*x)", points=1000))
g.writePDFfile()
# get the corresponding PyX paths
qpath = qpi.path.normpath()
opath1 = opi1.path.normpath()
opath2 = opi2.path.normpath()
# now analyse it
POINTS = 10
qpath_arclen_pt = qpath.arclen_pt()
qpath_points_pt = qpath.at_pt([qpath_arclen_pt*i/(POINTS-1) for i in range(POINTS)])
for opath in [opath1, opath2]:
opath_arclen_pt = opath.arclen_pt()
opath_points_pt = opath.at_pt([opath_arclen_pt*i/(POINTS-1) for i in range(POINTS)])
print(sum(math.sqrt((qpoint_x_pt-opoint_x_pt)**2 + (qpoint_y_pt-opoint_y_pt)**2)
for (qpoint_x_pt, qpoint_y_pt), (opoint_x_pt, opoint_y_pt) in zip(qpath_points_pt, opath_points_pt)))
该程序只是打印出来:
25.381154890630064
56.44386644062556
这表明虚线比虚线更接近实线。
您还可以比较切线、曲率、弧线本身等。根据您的需要,有很多选择。