no way to stop SageMath Animation ??
HI the only way I found to stop my animation is to reboot my computer.
a little weird ! ;-)
%matplotlib notebook
import numpy as np
from scipy.spatial import distance
from sage.plot.point import Point
import numpy as np
import matplotlib.pyplot as plt
#from matplotlib.animation import FuncAnimation
#import animatplot as amp
print("https://www.youtube.com/watch?v=L5GEnUuhilw&t=204s")
#show(LatexExpr(r"\mathrm{rotation_matrix} = \begin{bmatrix}\cos{2\pi \over n} & -\sin{2\pi \over n} \\[8pt] \sin{2\pi \over n} & \cos{2\pi \over n}\end{bmatrix} \qquad \mathrm{s}_0 = \begin{bmatrix}1 & 0 \\ 0 & -1\end{bmatrix}"))
show(LatexExpr(r"\mathrm{rotation_{matrix}} = \begin{bmatrix}\cos{2\pi \over n} & -\sin{2\pi \over n} \\[8pt] \sin{2\pi \over n} & \cos{2\pi \over n}\end{bmatrix} "))
show(LatexExpr(r"\mathrm{reflexion_{matrix}} = \left(\begin{array}{rr} \cos 2 \theta & \sin 2 \theta \\ \
\sin 2 \theta & -\cos 2 \theta \end{array}\right)"))
var('n')
n=6
maL=[]
shiftAngle=-2*pi/(2*n)
for i in range(1,n+1) :
maL.append([cos(i*2*pi/n+shiftAngle),sin(i*2*pi/n+shiftAngle)])
show(maL)
#mL=[[1,1],[-1,1],[-1,-1],[1,-1]]
m=matrix(maL).transpose()
rotationNumber=3
def chooseColor(colorsNumber):
if 'colorL' not in locals():
colorL=[]
for i in range(0,colorsNumber) :
colorL.append([random(),random(),random()])
#show(colorL)
D=distance.cdist(colorL, colorL, 'cityblock')
Dm=matrix(D)
DvL=[]
for v in (Dm.columns()):
DvL.append((sum(v)))
#show(DvL)
keys = range(len(DvL))
DvDict=dict(zip(DvL, keys))
sorted_x = sorted(DvDict.items(), key=lambda kv: kv[1])
#show(sorted_x)
keys = range(len(DvL))
DvDict=dict(zip(DvL, keys))
#show(DvDict)
DvDictSorted={k: v for k, v in sorted(DvDict.items(), key=lambda item: item[0],reverse=True)}
#show(DvDictSorted)
colorSortedL=[]
for key,val in DvDictSorted.items():
colorSortedL.append(colorL[val])
#print (key, "=>", val)
return colorSortedL
colorSortedL=None
if colorSortedL==None :
colorSortedL=chooseColor(2*rotationNumber)
#show(colorSortedL)
rot=matrix(SR,[[cos(2*pi/n),-sin(2*pi/n)],[sin(2*pi/n),cos(2*pi/n)]])
ref=matrix(SR,[[cos(2*2*pi/n),sin(2*2*pi/n)],[sin(2*2*pi/n),-cos(2*2*pi/n)]])
show("rotation Matrix :\t",rot)
show("reflexion Matrix :\t",ref)
def getRotAndReflexionMatrix(rotAndRefSerie,m) :
#show(rot,ref)
mrt=m
for c in rotAndRefSerie :
if c=='r' :
mrt=rot*mrt
elif c=='t' :
mrt=ref*mrt
else :
print("only r or t please")
return mrt
def buildMatriceFigPlots(matricesL):
figPlotsL=[]
figPlot=plot(0, ymin=-1.25, ymax=1.25)
for matrice in matricesL :
matriceL=list(matrice.transpose())
figPlot=(list_plot(matriceL,color='black',size=40,figsize=(8,8)))
for i in range(0,len(matriceL)) :
#show(matriceL[i])
figPlot+=text(str(i), matriceL[i], fontsize=50, rgbcolor=colorSortedL[i] )
figPlot += circle((0,0), 1)
figPlot += polygon(matriceL,fill=False, thickness=4, color='orange')
figPlotsL.append(figPlot)
#for c in range(0,t.dimensions()[1]) :
#for r in range(0,t.dimensions()[0]) :
# show(r,":\t",r,":\t",t[r,c])
return figPlotsL
TestAnimation=True
if TestAnimation :
#Test Animation
max = 1.5
matricesL=[]
for k in range(0,4) :
matrice=((rot^k)*m)
if k==3 :
matrice=((ref^1)*matrice) # commute trigo to clockWise
#t=((ref^2)*m) # no change
matricesL.append(matrice)
#show(list(t.transpose()))
#show("matriceL :\n", matricesL)
#PtsL.append(showFig(tL))
matriceFigPlotsL=buildMatriceFigPlots(matricesL)
#for p in MatriceFigPlotsL :
# show(p)
animate(matriceFigPlotsL,ymin=-max,ymax=max,axes_labels=['x','y'],fontsize=12,figsize=(8,8)).show(delay=100)
else:
# choose rotations and reflexion sequence by keyboard
# example enter the sequence:rrrtrtr
rotAndRefSerieString = input("rotation =r and reflexion =t chain :\t")
rotAndRefSerie=list(rotAndRefSerieString)
print("rotAndRefSerie :\t",rotAndRefSerie)
rotAndReflexionMatrix=getRotAndReflexionMatrix(rotAndRefSerie,m)
show("rotAndReflexionMatrix : \t",rotAndReflexionMatrix)
# plot list of matrices, so embed the matrix in a list
matriceFigPlotsL=buildMatriceFigPlots([rotAndReflexionMatrix])
#show("len(matriceFigPlotsL) :\t",len(matriceFigPlotsL))
for p in matriceFigPlotsL :
show(p)