Skip to content

Instantly share code, notes, and snippets.

@silgon
Created April 30, 2021 15:24
Show Gist options
  • Save silgon/6f029742c054a18ef2829dce4291853d to your computer and use it in GitHub Desktop.
Save silgon/6f029742c054a18ef2829dce4291853d to your computer and use it in GitHub Desktop.
import numpy as np
import matplotlib.pyplot as plt
n_dimensions = 2
A = np.random.rand(n_dimensions) # source
B = np.random.rand(n_dimensions) # target
# linear function
n_steps = 50
delta = (A-B)/n_steps
delta_array = np.ones((n_dimensions, n_steps))*delta[:,np.newaxis]
trajectory = A[:,np.newaxis]+delta_array.cumsum(1)
# plt.scatter(trajectory[0], trajectory[1], alpha=np.linspace(0.3,1, n_steps))
# plt.show()
import scipy.interpolate as si
import numpy as np
import matplotlib.pyplot as plt
def bspline(cv, n=100, degree=3):
cv = np.asarray(cv)
count = cv.shape[0]
degree = np.clip(degree, 1, count-1)
kv = np.array([0]*degree + list(range(count-degree+1))
+ [count-degree]*degree, dtype='int')
u = np.linspace(0, (count-degree), n)
return np.array(si.splev(u, (kv, cv.T, degree))).T
cv = np.array([[50., 25.], # A point
[57., 2.], # intermediate point
[40., 4.], # intermediate point
[40., 14.]]) # B Point
p = bspline(cv, n=n_steps, degree=3)
plt.scatter(p[:, 0], p[:, 1], alpha=np.linspace(0.3, 1, n_steps))
plt.show()
@silgon
Copy link
Author

silgon commented Apr 30, 2021

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment