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
| import math
import Tkinter as tk
def Vecteur(A,B):
""" Vecteur joignant deux points"""
return B[0]-A[0],B[1]-A[1]
def Norme(V):
"""Norme d'un vecteur"""
return math.sqrt(V[0]*V[0]+V[1]*V[1])
def Div (V,k):
"""Division du vecteur V par le scalaire lambda"""
return V[0]/float(k),V[1]/float(k)
def Normal(A,B):
"""Vecteur n unitaire normal à (AB) tel que AB,n soit direct"""
V=Vecteur(A,B)
W=-V[1],V[0]
N=Div(W,Norme(W))
return N
def PointM(A,B,e):
"""Point M tel que M est à distance e de (AB) et AM orthogonal à (AB) et AB,AM direct"""
N=Normal(A,B)
W=Div(N,1.0/e)
return A[0]+W[0],A[1]+W[1]
def Droite(A,B,e):
"""Equation de la droite parallèle à (AB) et à distance e de (AB) du côté 'gauche'"""
V=Vecteur(A,B)
u,v=-V[1],V[0]
P=PointM(A,B,e)
w=-u*P[0]-v*P[1]
return u,v,w
def Intersection(D1,D2):
"""Intersection de deux droites (formules de Cramer)"""
u1,v1,w1=D1[0],D1[1],D1[2]
u2,v2,w2=D2[0],D2[1],D2[2]
det=u1*v2-u2*v1
num1=-w1*v2+w2*v1
num2=-u1*w2+u2*w1
return num1/det,num2/det
def Translate(L,e):
""" Décalage du mur de la distance e vers la gauche """
Resultat=[]
Resultat.append(PointM(L[0],L[1],e)) # premier point
for i in range(1,len(L)-1):
A,B,C=L[i-1],L[i],L[i+1]
D1=Droite(A,B,e)
D2=Droite(B,C,e)
print D1
print D2
I=Intersection(D1,D2)
Resultat.append(I)
P=PointM(L[-2],L[-1],e)
Z=Vecteur(L[-2],L[-1])
Q=P[0]+Z[0],P[1]+Z[1]
Resultat.append(Q) #dernier point
return Resultat
def DessineSegment(S,c,c_width,c_height):
scale=100
x1,x2=int(scale*S[0][0])+c_width/2,int(scale*S[1][0])+c_width/2
y1,y2=c_height/2-int(scale*S[0][1]),c_height/2-int(scale*S[1][1])
c.create_line(x1,y1,x2,y2)
def DessineLigne(L,c,c_width,c_height):
for i in range(0,len(L)-1):
S=L[i],L[i+1]
DessineSegment(S,c,c_width,c_height)
def main():
"""Programme principal"""
root = tk.Tk()
root.title("Murs")
c_width = 600
c_height = 600
c = tk.Canvas(root, width=c_width, height=c_height, bg= 'white')
c.pack()
Mur1=[ (-2,1), (-1,1), (1,0) , (1,-1), (-1,-2)]
Mur2=Translate(Mur1,1)
DessineLigne(Mur1,c,c_width,c_height)
DessineLigne(Mur2,c,c_width,c_height)
root.mainloop()
if __name__=='__main__':
main() |
Partager