【问题标题】:Python: Equidistant points along a line joining set of pointsPython:沿着连接点集的线的等距点
【发布时间】:2019-01-01 21:03:33
【问题描述】:

作为 numpy.linspace 给出线性连接的两点之间的等距点。我可以得到连接一组点的直线上的等距点吗?

例如:

import numpy as np
npts = 10
xcoords, ycoords = [0,1], [0,1]
xquery = np.linspace(xcoords[0],xcoords[1], npts)
yquery = np.linspace(ycoords[0],ycoords[1], npts)

这里我需要在连接一组点的线上等距查询点

xcoords, ycoords = [0,1,5,8], [0,3,6,7]

【问题讨论】:

  • 那么,您需要在您指定的 2 个 4D 点之间等距 4D 点吗?像网格一样?
  • 如果不清楚,请见谅。它们只是二维点 (0,0)、(1,3)、(5,6) 和 (8,7)。我想沿着连接上述二维点的线获得等距点。与此处使用 QGIS 完成的类似(gis.stackexchange.com/questions/27102/…)。
  • 您正在寻找已实施的解决方案(我不知道)还是您想自己实施?

标签: python python-3.x numpy matplotlib scipy


【解决方案1】:

编辑:澄清此答案仅提供 x 方向上的等距点。我对问题的误解

我相信您正在寻找的是插值? scipy.interpolate 的文档在这里:https://docs.scipy.org/doc/scipy-1.0.0/reference/tutorial/interpolate.html#d-interpolation-interp1d

但是为了快速展示你的例子:

from scipy.interpolate import interp1d
x=[0,1,5,8]
y=[0,3,6,7]
f=interp1d(x,y)

然后只需像这样将您希望查询的新 x 点输入到 f 中(xnew 不能超过 x 的最小/最大界限)

xnew=np.linspace(0,8,10)
ynew=f(xnew)

然后看一下情节

import matplotlib.pyplot as plt
plt.plot(xnew,ynew,'ro',x,y,'x')
plt.show()

【讨论】:

  • 是的,我们可以进行插值,但它不会给出沿线的等距点。根据您的解决方案,前两个红色圆圈点之间的距离大于接下来两个红色点之间的距离。
  • 哦,我明白了。是的,我所做的插值在 x 中是等距的,但不是 y
【解决方案2】:

将 2D 分段线细分为等长部分:

import matplotlib.pyplot as plt
%matplotlib inline

from scipy.interpolate import interp1d
import numpy as np


x = [0, 1, 8, 2, 2]
y = [1, 0, 6, 7, 2]

# Linear length on the line
distance = np.cumsum(np.sqrt( np.ediff1d(x, to_begin=0)**2 + np.ediff1d(y, to_begin=0)**2 ))
distance = distance/distance[-1]

fx, fy = interp1d( distance, x ), interp1d( distance, y )

alpha = np.linspace(0, 1, 15)
x_regular, y_regular = fx(alpha), fy(alpha)

plt.plot(x, y, 'o-');
plt.plot(x_regular, y_regular, 'or');
plt.axis('equal');

【讨论】:

  • 非常感谢@xdze2。您的回答解决了我的问题,对我有很大帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
  • 2013-04-08
  • 1970-01-01
相关资源
最近更新 更多