Skip to content

Instantly share code, notes, and snippets.

@MarynaLongnickel
Created April 6, 2018 15:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MarynaLongnickel/0b1e1c569ebac4cea26a2197bcd0d2c1 to your computer and use it in GitHub Desktop.
Save MarynaLongnickel/0b1e1c569ebac4cea26a2197bcd0d2c1 to your computer and use it in GitHub Desktop.
rolling ball animation
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
p = np.linspace(-np.pi/2,np.pi/2,10)
x = np.sin(p)
v = np.column_stack((np.concatenate((x,x)),np.concatenate((np.cos(p),-np.cos(p))),[1]*len(p)*2))
def R(theta):
return np.matrix([[np.cos(theta), -np.sin(theta), 0],[np.sin(theta), np.cos(theta), 0], [0,0,1]])
def T(dx, dy):
return np.matrix([[1,0,dx],[0,1,dy],[0, 0, 1]])
x, y, z = v[::,0:1:], v[::,1:2:], v[::,2:3:]
x = [i for s in x for i in s]
y = [i for s in y for i in s]
z = [i for s in z for i in s]
ln, = plt.plot(x, y, 'ro', animated=True)
plt.close()
fig, ax = plt.subplots()
ax.set_aspect(1)
def init():
ax.set_xlim(0, 15)
ax.set_ylim(-1, 3)
return ln,
z0 = [x,y,z]
def update(t):
if t <=70 :
z1 = T(t/(70/13)+1,1) * R(np.pi/180*((t*30)%360)) * z0
else:
z1 = T((140-t)/(70/13)+1,1) * R(-np.pi/180*((t*30)%360)) * z0
ln, = plt.plot(z1.tolist()[0], z1.tolist()[1], 'ro', animated=True)
return ln,
ani = FuncAnimation(fig, update, frames= 140, init_func=init, interval=70, blit=True)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment