In [1]:
import math
import matplotlib.pyplot as plt
from celluloid import Camera
import numpy as np
import random
from IPython.display import HTML
In [2]:
Px = []
Py = []
Qx = []
Qy = []
R = 4
num = 200
alpha = random.random()*2*math.pi
Ax = R*math.cos(alpha)
Ay = R*math.sin(alpha)
In [3]:
def q(m):
    a = -((m**2-1)/(1+m**2))*Ax+((2*m)/(1+m**2))*Ay
    b = -(2*Ay-2*m*Ax)/(1+m**2)+Ay
    return a,b
In [4]:
beta = alpha
incbeta = 2*math.pi/(num-1)
for k in range(0,num):
    beta += incbeta
    px = R*math.cos(beta)
    py = R*math.sin(beta)
    qx,qy = q(math.tan(beta))
    Px.append(px)
    Py.append(py)
    Qx.append(qx)
    Qy.append(qy) 
Px = np.array(Px, dtype=np.float64)
Py = np.array(Py, dtype=np.float64)
Qx = np.array(Qx, dtype=np.float64)
Qy = np.array(Qy, dtype=np.float64)
In [5]:
def disegna():
    n = 3*(num)
    puntix = []
    puntiy = []
    for k in range(0,num):
        puntix.extend([Px[k],Qx[k],Px[k]])
        puntiy.extend([Py[k],Qy[k],Py[k]])
        plt.plot(np.array(puntix),np.array(puntiy),'b-')
        camera.snap()  
In [6]:
figura = plt.figure(figsize=(10,10))
camera = Camera(figura)
plt.axis('off')
xM = R*1.05
plt.xlim([-xM, xM])
plt.ylim([-xM, xM])
disegna()
animation = camera.animate(blit=True, interval=100)
animation.save('figure/cardioide.mp4')
plt.show()
In [8]:
HTML("""
<div align="middle">
<video width="80%" controls>
      <source src="figure/cardioide.mp4" type="video/mp4">
</video></div>""")
Out[8]:
In [ ]: