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
| # -*- coding: utf-8 -*-
"""Tracé de polygones étoilés
avec le module Tkinter
"""
import Tkinter #le module graphique
import math #module mathématique
"""paramètres du graphique"""
Width=300 #largeur de la fenêtre en pixels
Height=300 #hauteur de la fenêtre en pixels
Unit=15 #taille en pixels du vecteur unité
Midx=Width/2 #abscisse du centre du graphique
Midy=Height/2 #ordonnée du centre du graphique
racine=Tkinter.Tk() # la fenêtre de l'application
racine.title("Exemple de représentations graphiques")
fond=Tkinter.Canvas(racine, width=Width, height=Height, background='black')#aire de dessin
def convert(x,y):
"""calcule les coordonnées en pixels sur l'image du point de coordonnées x, y réels"""
m=int (x*Unit)+Midx
n=Midy-int(y*Unit)
return m,n
def packall(): #initialisation des composants
fond.pack()
def Polxy(n):
"""Sommets du polygone régulier"""
return [(9*math.cos(2*k*math.pi/n),9*math.sin(2*k*math.pi/n)) for k in xrange(0,n)]
def Star(n):
"""Tracé polygone étoilé"""
if(n%2):
StarImpair(n)
else:
StarPair(n)
def StarImpair(n):
""" cas impair"""
P=Polxy(n)
Q=[convert(x,y) for (x,y) in P]
for i in xrange(0,2*n,2):
fond.create_line(Q[i%n][0],Q[i%n][1],Q[(i+2)%n][0],Q[(i+2)%n][1],fill="red")
def StarPair(n):
"""cas pair"""
P=Polxy(n)
Q=[convert(x,y) for (x,y) in P]
for i in xrange(0,n,2):
fond.create_line(Q[i][0],Q[i][1],Q[(i+2)%n][0],Q[(i+2)%n][1],fill="white")
for i in xrange(1,n,2):
fond.create_line(Q[i][0],Q[i][1],Q[(i+2)%n][0],Q[(i+2)%n][1],fill="white")
def main():
"""fonction principale"""
packall()#disposition des composants
Star(5)
Star(6)
racine.mainloop()#boucle de traitement des évènements souris-clavier
main() |
Partager