numpy 从曲线形状获取等间距点

jogvjijk  于 2023-02-12  发布在  其他
关注(0)|答案(3)|浏览(182)

如何获取一个在曲线上创建了更多点的形状,并对其进行细分,以使这些点沿着曲线更均匀地分布?在我的研究中,我认为numpyinterp可能是正确的函数,但我不知道该使用什么作为参数(xxpfpleftright,& period).任何帮助都将非常感谢!
下面是显示所需输出的动画。

以下是输入圆角矩形的代码:

from matplotlib import pyplot as plt
import numpy as np

x_values = [1321.4, 598.6, 580.6, 563.8, 548.6, 535.4, 524.5, 516.2, 511,
509.2, 509.2, 511, 516.2, 524.5, 535.4, 548.6, 563.8, 580.6, 598.6, 1321.4, 1339.4,
1356.2, 1371.4, 1384.6, 1395.5, 1403.8, 1409, 1410.8, 1410.8, 1409, 1403.8, 1395.5,
1384.6, 1371.4, 1356.2, 1339.4, 1321.4]
y_values = [805.4, 805.4, 803.5, 798.3, 790.1,
779.2, 766, 750.8, 734, 716, 364, 346, 329.2, 314, 300.8, 289.9, 281.7, 276.5, 274.6,
274.6, 276.5, 281.7, 289.9, 300.8, 314, 329.2, 346, 364, 716, 734, 750.8, 766, 779.2,
790.1, 798.3, 803.5, 805.4]

fig, ax = plt.subplots(1)
ax.plot(x_values,y_values)
ax.scatter(x_values,y_values)
ax.set_aspect('equal')
plt.show()

谢谢大家!

jm81lzqq

jm81lzqq1#

from matplotlib import pyplot as plt
import numpy as np

x = np.array([1321.4, 598.6, 580.6, 563.8, 548.6, 535.4, 524.5, 516.2, 511,
509.2, 509.2, 511, 516.2, 524.5, 535.4, 548.6, 563.8, 580.6, 598.6, 1321.4, 1339.4,
1356.2, 1371.4, 1384.6, 1395.5, 1403.8, 1409, 1410.8, 1410.8, 1409, 1403.8, 1395.5,
1384.6, 1371.4, 1356.2, 1339.4, 1321.4])
y = np.array([805.4, 805.4, 803.5, 798.3, 790.1,
779.2, 766, 750.8, 734, 716, 364, 346, 329.2, 314, 300.8, 289.9, 281.7, 276.5, 274.6,
274.6, 276.5, 281.7, 289.9, 300.8, 314, 329.2, 346, 364, 716, 734, 750.8, 766, 779.2,
790.1, 798.3, 803.5, 805.4])

fig, ax = plt.subplots(1)
ax.set_aspect('equal')
ax.scatter(x, y, s=40, zorder=3, alpha=0.3)

# compute the distances, ds, between points
dx, dy = x[+1:]-x[:-1],  y[+1:]-y[:-1]
ds = np.array((0, *np.sqrt(dx*dx+dy*dy)))

# compute the total distance from the 1st point, measured on the curve
s = np.cumsum(ds)

# interpolate using 200 point
xinter = np.interp(np.linspace(0,s[-1], 200), s, x)
yinter = np.interp(np.linspace(0,s[-1], 200), s, y)

# plot the interpolated points
ax.scatter(xinter, yinter, s=5, zorder=4)
plt.show()
ippsafx7

ippsafx72#

一种方法是使用shapely库,你可以使用你的点来创建一个LineString对象,然后像你期望的那样使用numpy.linspace来插值。

import numpy as np
import matplotlib.pyplot as plt
import shapely

x_values = [1321.4, 598.6, 580.6, 563.8, 548.6, 535.4, 524.5, 516.2, 511,
509.2, 509.2, 511, 516.2, 524.5, 535.4, 548.6, 563.8, 580.6, 598.6, 1321.4, 1339.4,
1356.2, 1371.4, 1384.6, 1395.5, 1403.8, 1409, 1410.8, 1410.8, 1409, 1403.8, 1395.5,
1384.6, 1371.4, 1356.2, 1339.4, 1321.4]
y_values = [805.4, 805.4, 803.5, 798.3, 790.1,
779.2, 766, 750.8, 734, 716, 364, 346, 329.2, 314, 300.8, 289.9, 281.7, 276.5, 274.6,
274.6, 276.5, 281.7, 289.9, 300.8, 314, 329.2, 346, 364, 716, 734, 750.8, 766, 779.2,
790.1, 798.3, 803.5, 805.4]

coords = np.array([x_values, y_values]).T

poly = shapely.LineString(coords)

coords_new = poly.interpolate(np.linspace(0, 1, num=51), normalized=True)

x_new = [pt.x for pt in coords_new]
y_new = [pt.y for pt in coords_new]

plt.plot(x_new, y_new)
plt.scatter(x_new, y_new)
plt.show()

2exbekwf

2exbekwf3#

这是一个非常基本的解决方案,在一条线上的相邻点之间进行插值。
基本上,该代码所做的是,它将"临时"点放置在相邻点之间的直线上,这些点以大约stepsize的间距均匀分布。这将导致曲线上的间距近似均匀(stepsize越小,你拥有的点越密集)。现在如果你挑选它上面的每一个skipper-th点,这些点之间的间距将大约为skipper*stepsize
它有一个小缺陷,第一个和最后一个点不能保证彼此相距这么远,所以你必须使用skipper来修复这个问题。
代码:

stepsize = 10**-2

new_x = np.array([])
new_y = np.array([])

for i in range(len(x_values)):
    x0 = x_values[i]
    x1 = x_values[(i+1)%len(x_values)]
    y0 = y_values[i]
    y1 = y_values[(i+1)%len(x_values)]

    dx = x1-x0
    dy = y1-y0

    length = np.sqrt(dx**2+dy**2)
    N = int(length/stepsize)

    new_x = np.append(new_x,np.linspace(x_values[i],x_values[(i+1)%len(x_values)],N))
    new_y = np.append(new_y,np.linspace(y_values[i],y_values[(i+1)%len(x_values)],N))

skipper = 6650

fig, ax = plt.subplots(1)
ax.plot(x_values,y_values)
ax.scatter(new_x[::skipper],new_y[::skipper])
ax.set_aspect('equal')
plt.show()

相关问题