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
|
import tkinter as tk
def combinaison_lineaire(A,B,u,v):
return [A[0]*u+B[0]*v,A[1]*u+B[1]*v]
def interpolation_lineaire(A,B,t):
return combinaison_lineaire(A,B,t,1-t)
def vecteur_unitaire(P1,P2):
Ux = P2[0]-P1[0]
Uy = P2[1]-P1[1]
norme = math.sqrt(Ux*Ux+Uy*Uy)
if norme!=0:
return [Ux/norme,Uy/norme]
else:
return False
def test_alignement_4pts(points,epsilon):
U1 = vecteur_unitaire(points[0],points[1])
U2 = vecteur_unitaire(points[1],points[2])
U3 = vecteur_unitaire(points[2],points[3])
if U2:
x = 2.0-(U1[0]*U2[0]+U1[1]*U2[1]+U2[0]*U3[0]+U2[1]*U3[1])
else:
x = 1.0-(U1[0]*U3[0]+U1[1]*U3[1])
if abs(x) < epsilon:
return True
else:
return False
def division_courbe_bezier_3(points_control):
P01 = interpolation_lineaire(points_control[0],points_control[1],0.5)
P12 = interpolation_lineaire(points_control[1],points_control[2],0.5)
P23 = interpolation_lineaire(points_control[2],points_control[3],0.5)
P01_12 = interpolation_lineaire(P01,P12,0.5)
P12_23 = interpolation_lineaire(P12,P23,0.5)
Q = interpolation_lineaire(P01_12,P12_23,0.5)
return ([points_control[0],P01,P01_12,Q],[Q,P12_23,P23,points_control[3]])
def courbe_bezier_3_recursif(points_control,epsilon,pile_points_courbe):
if test_alignement_4pts(points_control,epsilon):
pile_points_courbe.append(points_control[0])
else:
(points_1,points_2) = division_courbe_bezier_3(points_control)
courbe_bezier_3_recursif(points_1,epsilon,pile_points_courbe)
courbe_bezier_3_recursif(points_2,epsilon,pile_points_courbe)
def plot_points(points_courbe,style='-'):
x = []
y = []
for p in points_courbe:
x.append(p[0])
y.append(p[1])
plot(x,y,style)
class Courbe:
def __init__(self,P0,P1,P2,P3,epsilon):
self.points_courbe = []
self.epsilon = epsilon
self.bezier_3_recursif_init([P0,P1,P2,P3])
self.points_control = [P0,P1,P2,P3]
def bezier_3_recursif_init(self,points_control):
courbe_bezier_3_recursif(points_control,self.epsilon,self.points_courbe)
self.P2 = points_control[2]
self.P3 = points_control[3]
def ajouter(self,P2,P3):
P0 = self.P3
P1 = [P0[0]+P0[0]-self.P2[0],P0[1]+P0[1]-self.P2[1]]
self.bezier_3_recursif_init([P0,P1,P2,P3])
self.points_control.append(P1)
self.points_control.append(P2)
self.points_control.append(P3)
self.dernier_point = P3
def liste_points(self):
self.points_courbe.append(self.dernier_point)
return self.points_courbe
def liste_points_control(self):
self.points_control.append(self.dernier_point)
return self.points_control
def CalculeVitesse(L):
n = len(L)
V = []
T = []
for i in range(0,n):
x,y = L[i]
xp, yp = L[i-1]
xs, ys = L[(i+1)//n] # si i == n-1, remplacer i+1 par 0
dG = ((x-xp)**2 + (y - yp)**2)**.5
dD = ((x-xs)**2 + (y - ys)**2)**.5
v = int(((dG+dD)//2)+1)
V.append(v)
T.append(dD/v)
V.append(50)
return V, T
courbe = Courbe([121,276],[142,246],[169,212],[190,182],5e-3)
courbe.ajouter([269,146],[339,142])
courbe.ajouter([460,161],[491,154])
courbe.ajouter([514,151],[535,149])
courbe.ajouter([562,142],[571,132])
courbe.ajouter([605,112],[637,130])
courbe.ajouter([684,137],[713,96])
courbe.ajouter([782,114],[791,110])
courbe.ajouter([810,140],[815,145])
courbe.ajouter([600,440],[600,445])
courbe.ajouter([300,400],[300,405])
courbe.ajouter([121,275],[121,276])
points = courbe.liste_points() # la liste utile
vitesses,durees = CalculeVitesse(points)
class Circuit(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.size = 750
self.vitesses,self.durees = CalculeVitesse(points)
self.creer_widgets()
self.points=courbe.liste_points()
m=len(self.points)
o=self.points[0]
self.x=o[0]
self.y=o[1]
self.point = self.canv.create_oval(self.x, self.y,self.x+5,self.y+5,width=2, fill="black")
self.move()
def creer_widgets(self):
self.canv = tk.Canvas(self, bg="white", height=self.size,width=self.size)
self.canv.pack(side=tk.LEFT)
self.bouton_quitter = tk.Button(self, text="Quitter",command=self.destroy)
self.bouton_quitter.pack(side=tk.BOTTOM)
def stop(self, esc):
"""Quitte l'application."""
self.quit()
def move(self):
m=len(self.points)
for i in range(m):
o=self.points[i]
self.x=o[0]
self.y=o[1]
self.g=self.vitesses[i]
self.canv.coords(self.point, self.x, self.y, self.x+5,self.y+5)
self.after(self.g, self.move)
if __name__ == "__main__":
app = Circuit()
app.mainloop() |