# Créé par Fred, le 30/12/2012 from __future__ import division from lycee import * from Tkinter import * from random import randrange from time import sleep """_____________________________________""" #exercices 5.8.2 à 5.8.4 def dessinligne(): global x1, y1, x2, y2, coul,large,haut x2=randrange(large) y2=randrange(haut) can.create_line(x1,y1,x2,y2,width=1,fill=coul) x1, y1 = x2, y2 def changecouleur(): global coul cl=['purple','cyan','maroon','green','red','blue','orange','yellow'] coul=cl[randrange(8)] def exercices_582_a_584(): x1, y1, x2, y2 = 10, 190, 190, 10 large, haut = 300,200 coul = 'cyan' fenetre = Tk() can = Canvas(fenetre,bg='ivory',height=haut,width=large) can.grid(column=0,row=0,rowspan=10) bou1 = Button(fenetre,text='Quitter',command=fenetre.quit) bou1.grid(column=1,row=9,sticky=W) bou2 = Button(fenetre,text=u'Tracé d\'une ligne', command=dessinligne) bou2.grid(column=1,row=0,sticky=W) bou3 = Button(fenetre,text='Autre couleur',command=changecouleur) bou3.grid(column=1,row=1,sticky=W) fenetre.mainloop() """_____________________________________""" #exercice 5.8.7 def dessin_rectangle(): global deja,nrect,L x0=pos_souris[0] y0=pos_souris[1] for k in range(nrect): ux=x0+direct[0]*k*ecartx+randrange(ecartx) uy=y0+direct[1]*k*ecarty+randrange(ecarty) x=int(ux) y=int(uy) u=x,y,x+ecartx,y+ecarty if deja==0: L.append(can.create_rectangle(u,fill=cl[k%len(cl)])) else: can.coords(L[k],u) sleep(0.01) deja=deja+1 fenetre.update() def bouge(event): global pos_souris,anc_pos,deja,direct if deja<0:deja=0 pos_souris=[event.x,event.y] u=math.atan2(pos_souris[1]-anc_pos[1],pos_souris[0]-anc_pos[0]) p=math.pi direct=(cos(p-u),sin(u-p)) etiquette.config(text=str(u)+'° ('+str(direct[0])+','+str(direct[1])+')') dessin_rectangle() anc_pos=pos_souris def exercice_5_8_7(): cl=['purple','cyan','maroon','green','red','blue','orange','yellow'] large, haut = 600,600 pos_souris=[300,300];anc_pos=pos_souris;direct=(1,0) deja=-1;nrect=20;L=[];ecartx=15;ecarty=10 fenetre = Tk() can = Canvas(fenetre,bg='ivory',height=haut,width=large) can.grid(column=0,row=0) bou1 = Button(fenetre,text='Quitter',command=fenetre.quit) bou1.grid(column=1,row=1) etiquette=Label(fenetre) etiquette.grid(column=0,row=1) can.bind('',bouge) fenetre.mainloop() """_____________________________________""" #exercice 5.8.12 def distance_astres(etat): d=((as1[0]-as2[0])**2+(as1[1]-as2[1])**2)**0.5 return d def force_grav(d): G=(haut**2+large**2)**0.5 #6.7e-11 if d==0:return as1[5]*as2[5]/G else: return as1[5]*as2[5]/(d**2) def dessinastres(etat): ds=distance_astres(etat) maxF=force_grav(0) if ds>0.1: F=force_grav(ds) h_rectF=int(0.9*haut*F/maxF) else: F=1;h_rectF=0.9*haut S='distance = '+"%5.2f" % ds+' / force = '+"%7f" %F etiquette.configure(text=S) if etat==0: can.create_rectangle(large-50,haut,large-10,haut-h_rectF,fill='grey',tags='t4') can.create_oval(as1[0]-as1[2],as1[1]-as1[2],as1[0]+as1[2],as1[1]+as1[2],fill=as1[4],tags='as1') can.create_text(as1[0],as1[1]+2*as1[2],text='M1='+str(as1[5]),fill=as1[6],tags='t1') can.create_oval(as2[0]-as2[2],as2[1]-as2[2],as2[0]+as2[2],as2[1]+as2[2],fill=as2[4],tags='as2') can.create_text(as2[0],as2[1]+2*as2[2],text='M2='+str(as2[5]),fill=as2[6],tags='t2') can.create_line(as1[0],as1[1],as2[0],as2[1],tags='t3') elif etat==1: can.coords('as1',as1[0]-as1[2],as1[1]-as1[2],as1[0]+as1[2],as1[1]+as1[2]) can.coords('t1',as1[0],as1[1]+2*as1[2]) can.itemconfigure('as1',fill=as1[4]) can.coords('as2',as2[0]-as2[2],as2[1]-as2[2],as2[0]+as2[2],as2[1]+as2[2]) can.itemconfigure('as2',fill=as2[4]) can.coords('t2',as2[0],as2[1]+2*as2[2]) can.coords('t3',as1[0],as1[1],as2[0],as2[1]) can.coords('t4',large-50,haut,large-10,haut-h_rectF) fenetre.update() def clic(event): global pos_souris, as_actif,as1, as2 pos_souris=[event.x,event.y] if as_actif!=0: as_actif=0; as1[4]='cyan';as2[4]='orange' else: if (as1[0]-as1[2]',clic) can.bind('',bouge) fenetre.mainloop() """---------------------------------------------------------------------------""" #exercices_582_a_584() #exercice_5_8_7() exercice_5_8_12()