import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as ani
import matplotlib.patches as patches
#座標の準備
fig = plt.figure()
fig.suptitle("2006京大数学 理系第5問",x=0.96,y=0.96,
color="0.7",ha="right")
ax = fig.add_subplot(111)
ax.axis("off")
ax.set_aspect("equal")
#三角形たちを準備
a=np.array([2/3,3/4])
b=np.array([1,0])
c=np.array([0,0])
ABC = patches.Polygon(xy=[a,b,c],fill=False,ec="0.7",
linewidth=4,joinstyle="round")
ax.add_patch(ABC)
PQR = patches.Polygon(xy=[a,a,c],fc="palegoldenrod",
alpha=0.7,zorder=-20)
ax.add_patch(PQR)
l1,l2,l3 = ax.plot([],[],[],[],[],[],
color="w",lw=0.8,zorder=-10)
#軌跡の準備
trajectory,= ax.plot([],[],"steelblue",lw=7,alpha=0.4)
G,= ax.plot([],[],"slategrey",marker="o",markersize=7)
#イージング関数
def easing(x):
if x<0.5:
return 2*x**2
else:
return 1-(-2*x+2)**2/2
#ぬりぬりする関数
def nurinuri(i,p1,p2,p3):
p = p1
k = i/199
q = (1-k)*p1+k*p2
k = np.sin(25*k*np.pi/2)**2
r = (1-k)*p3+k*p2
PQR.set_xy([p,q,r])
l1.set_data(zip(p,(q+r)/2))
l2.set_data(zip(q,(r+p)/2))
l3.set_data(zip(r,(p+q)/2))
g = (p+q+r)/3
G.set_data([g[0]],[g[1]])
tx = np.append(trajectory.get_data()[0],g[0])
ty = np.append(trajectory.get_data()[1],g[1])
trajectory.set_data(tx,ty)
#上演
def update(i):
if i<200:
nurinuri(i,a,b,c)
elif i<400:
nurinuri(i-200,b,c,a)
elif i<600:
nurinuri(i-400,c,a,b)
elif i<630:
alpha = 1-(i-600)/29
G.set_alpha(alpha)
mov = ani.FuncAnimation(fig,update,660,interval=100)
plt.show()