matplotlib 在缓和曲线中生成等距点

mrwjdhj3  于 2023-06-23  发布在  其他
关注(0)|答案(1)|浏览(88)

我有这样的代码来生成螺旋中的点:

import numpy as np
import matplotlib.pyplot as plt

initial_radius = 0.01
n_turns = 4

final_radius = 0.1

b = (final_radius - initial_radius)/(2*np.pi*n_turns)

start_theta = np.pi/2
end_theta = 2*np.pi*n_turns

s = np.linspace(start_theta, end_theta, 300)
x = (initial_radius + b*s)*np.cos(s)
y = (initial_radius + b*s)*np.sin(s)

plt.scatter(x,y, label="spiral")
plt.gca().set_aspect('equal')

但是,这些点并不是沿着缓和曲线以相等的间距生成的。
如何更改s的采样以使它们等距?

ie3xauqp

ie3xauqp1#

一种方法是计算总长度作为s的函数,所以L(s)。然后我们可以通过插值来反转这个关系,以找到s(L)。我们可以使用它来找到s值,给出等间距的点!要了解更多信息,你可能想google一下“arc-length parameterization”。

import numpy as np
import matplotlib.pyplot as plt

initial_radius = 0.01
n_turns = 4
final_radius = 0.1

b = (final_radius - initial_radius)/(2*np.pi*n_turns)

start_theta = np.pi/2
end_theta = 2*np.pi*n_turns

s = np.linspace(start_theta, end_theta, 300)
x = (initial_radius + b*s)*np.cos(s)
y = (initial_radius + b*s)*np.sin(s)

# Calculate the arc length
L = np.cumsum(np.sqrt(np.gradient(x)**2 + np.gradient(y)**2))
Ls = np.linspace(0, L[-1], 300)

# Find s values for equally spaced arc lengths
new_s = np.interp(Ls, L, s)

# Calculate x and y for equally spaced arc lengths
x = (initial_radius + b*new_s)*np.cos(new_s)
y = (initial_radius + b*new_s)*np.sin(new_s)

plt.scatter(x,y, label="spiral")
plt.gca().set_aspect('equal')
plt.show()

另一种选择是通过分析方法确定弧长参数化。

相关问题