Skip to content

Instantly share code, notes, and snippets.

@astrojuanlu
Last active December 29, 2015 12:59
Show Gist options
  • Save astrojuanlu/7674552 to your computer and use it in GitHub Desktop.
Save astrojuanlu/7674552 to your computer and use it in GitHub Desktop.
Proof of concept: translations through rotations. Inspired by http://mathgifs.blogspot.com.es/2013/11/translations-through-rotations.html
from __future__ import division
from functools import partial
import numpy as np
from numpy import cos, sin, pi
import matplotlib.pyplot as plt
from matplotlib import animation
from matplotlib import rcParams
def Point(*xy):
return np.array(xy)
def main(traj, cline, size, nframes, npoints):
centers = [cline(t) for t in np.linspace(0, 1, num=npoints)]
offset = -1.0 / (npoints - 1)
dpi = rcParams['figure.dpi']
fig, ax = plt.subplots(figsize=(size[0] / dpi, size[1] / dpi))
def init():
for i, center in enumerate(centers):
ax.plot(*(center + traj(0 + i * offset)), marker='o', color='#dddddd', mew=0)
ax.set_xlim(0, size[0])
ax.set_ylim(0, size[1])
ax.axis('off')
fig.patch.set_facecolor('black')
fig.subplots_adjust(left=0.0, right=1.0)
def step(i):
t = i / nframes
print(i)
for i, p in enumerate(ax.get_lines()):
p.set_data(*(centers[i] + traj(t + i * offset)))
anim = animation.FuncAnimation(fig, step, frames=nframes, init_func=init, interval=100)
#fig.show()
anim.save("mathgif.mp4", fps=10, savefig_kwargs={'facecolor': 'black'})
def circle(t, R):
"""Parametric equation of a circle, clockwise.
"""
x = R * cos(2 * pi * t)
y = -R * sin(2 * pi * t)
return Point(x, y)
def hline(t):
"""Parametric equation of an horizontal segment.
"""
return Point(t, 0)
if __name__ == '__main__':
WIDTH = 360
HEIGHT = 113
RADIUS = 50
NFRAMES = 30
NPOINTS = 15
traj = partial(circle, R=RADIUS)
def cline(t):
return Point(hline(t * WIDTH)[0], HEIGHT / 2)
main(traj, cline, size=(WIDTH, HEIGHT), nframes=NFRAMES, npoints=NPOINTS)
@afelopez
Copy link

genial! un muy bonito programa :)

@astrojuanlu
Copy link
Author

@ingratosocial ¡Gracias! :)

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