1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
| # Programme simu.py
# Guillaume, mars 2016.
#
# Simulation billard.
###### Importation des modules
import numpy as np
import matplotlib.pyplot as plt
###### Definition des constantes
# dimensions billard : bornes de l'espace
l = 0.0572 #rayon de la boule
xmin =0
xmax = 2.54
ymin = 0
ymax = 1.27
# tableau/coordonnées
nombre = 16 #nombre de boules
x = np.zeros(nombre)
y = np.zeros(nombre)
vx = np.zeros(nombre)
vy = np.zeros(nombre)
#conditions initiales
x[0]=0.635#float(input("Entrez la position initiale en x de la boule blanche (inferieure a 0.636): "))# doit être inférieur à 0.635
y[0]=0.635#float(input("Entrez la position initiale en y de la boule blanche (inferieure à 1.27): "))
vx[0]= 10 #float(input("Entrez la composante en x de la vitesse de la boule blanche: "))
vy[0]= 1 #float(input("Entrez la composante en y de la vitesse de la boule blanche: "))
x[1]=1.905
y[1]= 0.635
vx[1]=0
vy[1]=0
y[2]=y[1]-l
y[3]=y[1]+l
y[4]=y[1]-2*l
y[5]=y[1]
y[6]=y[1]+2*l
y[7]=y[1]-3*l
y[8]=y[1]-l
y[9]=y[1]+l
y[10]=y[1]+3*l
y[11]=y[7]-l
y[12]=y[7]+l
y[13]=y[1]
y[14]=y[13]+2*l
y[15]=y[14]+2*l
for i in range(2,4):
x[i]=x[1]+np.sqrt(3)*l
vx[i]=vx[1]
vy[i]=vy[1]
for i in range(4,7):
x[i]=x[1]+2*np.sqrt(3)*l
vx[i]=vx[1]
vy[i]=vy[1]
for i in range(7,11):
x[i]=x[1]+3*np.sqrt(3)*l
vx[i]=vx[1]
vy[i]=vy[1]
for i in range(11,16):
x[i]=x[1]+4*np.sqrt(3)*l
vx[i]=vx[1]
vy[i]=vy[1]
tabx = [[x[0]],[x[1]],[x[2]],[x[3]],[x[4]],[x[5]],[x[6]],[x[7]],[x[8]],[x[9]],[x[10]],[x[11]],[x[12]],[x[13]],[x[14]],[x[15]]]
taby = [[y[0]],[y[1]],[y[2]],[y[3]],[y[4]],[y[5]],[y[6]],[y[7]],[y[8]],[y[9]],[y[10]],[y[11]],[y[12]],[y[13]],[y[14]],[y[15]]]
for i in range(nombre):
plt.plot(x[i],y[i],'o',markersize=15)#boules à la position initiale
plt.ylim(0,1.27)
plt.xlim(0,2.54)
plt.show()
plt.close()
#fonctions
def posx(vx,t,x0):
return (vx*t)+x0
def posy(vy,t,y0):
return (vy*t)+y0
def choc(x1,y1,x2,y2,vx1,vy1,vx2,vy2):
theta=np.arctan((y1-y2)/(x1-x2))
V=np.sqrt((vx**2)+(vy**2))
vf1x=-V+(2*(np.cos(theta)**2)*V)
vf1y=2*np.sin(theta)*np.cos(theta)*V
vf2x=2*V*(np.sin(theta)**2)
vf2y=2*np.sin(theta)*np.cos(theta)
return vf1x,vf1y,vf2x, vf2y
def chocbord(x,y,vx,vy):
g = 5
d = 2
b = 19
h = 3
if x<l:
print("Choc avec le bord gauche")
x = posx(vx,t,x)
y = posy(vy,t,y)
vx = -vx
vy = vy
g=1
elif x>xmax-l:
print("Choc avec le bord droit")
x = posx(vx,t,x)
y = posy(vy,t,y)
vx = -vx
vy = vy
d=1
elif y<l:
print("Choc avec le bord bas")
x = posx(vx,t,x)
y = posy(vy,t,y)
vx = vx
vy = -vy
b=1
elif y>ymax-l:
print("Choc avec le bord haut")
x = posx(vx,t,x)
y = posy(vy,t,y)
vx = vx
vy = -vy
h=1
if g==b or g == h or d == b or d == h or (b==1 and x==xmax/2) or (h==1 and x==xmax/2):
print("boule",i,"empochée")
x=10
y=10
vx=0
vy=0
return vx,vy
#programme principal
for t in np.linspace(0,10,1000):
for i in range(nombre):
for j in range(nombre):
if i != j:
x[i]=posx(vx[i],t,x[i])
y[i]=posy(vy[i],t,y[i])
if np.sqrt((((vx[i]*t)-(vx[j]*t))**2)+(((vy[i]*t)-(vy[j]*t))**2))<2*l:
if vx[j]==0 and vy[j]==0:
vx[i],vy[i],vx[j],vy[j] = choc(x[i],y[i],x[j],y[j],vx[i],vy[i],vx[j],vy[j])
tabx[i].append(x[i])
taby[i].append(y[i])
tabx[j].append(x[j])
taby[j].append(y[j])
elif x[i]<l or x[i]>xmax-l or y[i]<l or y[i]<ymax-l:
vx[i],vy[i] = chocbord(x[i],y[i],vx[i],vy[i])
tabx[i].append(x[i])
taby[i].append(y[i])
else: pass
for i in range (nombre):
#print(tabx[i][:],taby[i][:])
plt.plot(tabx[i][:],taby[i][:])
plt.ylim(0,1.27)
plt.xlim(0,2.54)
plt.show()
print(t,'secondes')
plt.close()
for i in range(nombre):
plt.plot(x[i],y[i],'o',markersize=15)#boules à la position finale
plt.ylim(0,1.27)
plt.xlim(0,2.54)
plt.show() |
Partager