import matplotlib.pyplot as plt
import numpy as np
def bezier_curve(control_points, num_points=1000):
T = np.linspace(0, 1, num_points, dtype=np.float64)
n = len(control_points) - 1
curve = np.zeros((num_points, 3))
for num, t in enumerate(T):
point = control_points.copy()
for i in range(n):
for j in range(len(point) - 1):
point[j] = (1 - t) * point[j] + t * point[j + 1]
point = np.delete(point, len(point) - 1, axis=0)
curve[num, :] = point
return curve
# 控制点
points = np.array([[0, 0, 0], [1, 2, 3], [3, 4, 2], [9, 5, 3]], dtype=np.float64)
# 计算贝塞尔曲线
curve_points = bezier_curve(points)
# 绘制
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot(points[:, 0], points[:, 1], points[:, 2], 'ro--', label='Control Points')
ax.plot(curve_points[:, 0], curve_points[:, 1], curve_points[:, 2], 'b-', label='Bezier Curve')
ax.legend()
plt.show()
贝塞尔曲线python代码
最新推荐文章于 2024-08-15 01:55:01 发布