Skip to content

Instantly share code, notes, and snippets.

@crypdick
Created July 7, 2025 03:06
Show Gist options
  • Save crypdick/3d38427293a7faebd3777d0fc1fe09c1 to your computer and use it in GitHub Desktop.
Save crypdick/3d38427293a7faebd3777d0fc1fe09c1 to your computer and use it in GitHub Desktop.
for my blog post on multi-vector slerp
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# Create a 3D plot showing the vectors and interpolations
fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(111, projection='3d')
# Plot original vectors
ax.quiver(0, 0, 0, vecs[0][0], vecs[0][1], vecs[0][2],
color='black', arrow_length_ratio=0.1, label='v0 [1,0,0]')
ax.quiver(0, 0, 0, vecs[1][0], vecs[1][1], vecs[1][2],
color='black', arrow_length_ratio=0.1, label='v1 [0,1,0]')
# Plot lerp result
ax.quiver(0, 0, 0, avg_unit[0], avg_unit[1], avg_unit[2],
color='orange', arrow_length_ratio=0.1, label='lerp result')
# Plot slerp result
ax.quiver(0, 0, 0, interp_vec[0], interp_vec[1], interp_vec[2],
color='green', arrow_length_ratio=0.1, label='slerp result')
# Draw unit sphere wireframe
u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(0, np.pi, 100)
x_sphere = np.outer(np.cos(u), np.sin(v))
y_sphere = np.outer(np.sin(u), np.sin(v))
z_sphere = np.outer(np.ones(np.size(u)), np.cos(v))
ax.plot_wireframe(x_sphere, y_sphere, z_sphere, alpha=0.1, color='gray')
# Show where 30 degrees is with arrow from outside the sphere
start_point = np.array([np.cos(np.radians(30)) * 1.3, np.sin(np.radians(30)) * 1.3, 0])
end_point = np.array([np.cos(np.radians(30)), np.sin(np.radians(30)), 0])
ax.quiver(start_point[0], start_point[1], start_point[2],
end_point[0] - start_point[0], end_point[1] - start_point[1], end_point[2] - start_point[2],
color='red', arrow_length_ratio=0.2, label='30°')
# Set equal aspect ratio and limits
ax.set_box_aspect([1,1,1])
ax.legend()
ax.set_title('Linear vs Spherical Interpolation (top-down view)')
# Hide the axes
ax.set_axis_off()
# Top-down view
ax.view_init(elev=90, azim=0)
plt.tight_layout()
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment