Skip to content

Instantly share code, notes, and snippets.

@yangyushi
Created November 30, 2020 21:17
Show Gist options
  • Save yangyushi/5b4cf5f3665d4d4e4b2d0504900a00e7 to your computer and use it in GitHub Desktop.
Save yangyushi/5b4cf5f3665d4d4e4b2d0504900a00e7 to your computer and use it in GitHub Desktop.
applying alignment spherical boundary
import numpy as np
import matplotlib.pyplot as plt
N = 5
v = np.random.random((N, 3))
v = v / np.linalg.norm(v, axis=1)[:, None]
r = np.random.randn(N, 3)
r = r / np.linalg.norm(r, axis=1)[:, None]
angle = np.arccos(np.einsum('ij,ij->i', v, r))
i = angle < np.pi/2
a = v.copy()
a[i] = np.cross(r[i], np.cross(v[i], r[i]))
a = a / np.linalg.norm(a, axis=1)[:, None]
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
ax.scatter(*r.T, color='teal')
ax.quiver(*r.T, *v.T, color='teal')
ax.quiver(*r[i].T, *a[i].T, color='tomato')
ax.scatter(0, 0, 0, color='k')
for i in range(N):
ax.plot(*np.stack((np.zeros(3), r[i])).T, color='k')
ax.set_xlim(-2, 2)
ax.set_ylim(-2, 2)
ax.set_zlim(-2, 2)
plt.xlabel("X")
plt.ylabel("Y")
ax.set_zlabel("Z")
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment