Accueil > > > IMPLÉMENTATION DE LA TORTUE LOGO, APPLICATION AUX FRACTALES
IMPLÉMENTATION DE LA TORTUE LOGO, APPLICATION AUX FRACTALES
Information sur la source
Description
Ce code implémente une tortue logo (simple). Pour ceux qui ne connaissent pas, il s'agit d'une manière différente de dessiner : on peut donner plusieurs ordres à la tortue: Celle ci étant initialisée(abscisse x, ordonnée y, angle angle, crayon posé ...) - avance(l) : si le crayon est posé, un trait de longueur l est tracé dans la direction angle à partir de la position de la tortue - tourne_gauche(theta) : tourne à gauche d'un angle theta - tourne_droite(theta) : idem à droite ... C'est alors un outil de choix pour dessiner des fractales comme le flocon de Koch, un arbre fractal randomisé et la courbe du dragon donnés en exemple.
Source
- #! /usr/bin/python
- # -*- coding:utf-8 -*-
-
- from Tkinter import *
- from math import radians,cos,sin,sqrt
- from random import randrange,uniform
-
- class tortue:
- """Tortue(canvas,x=0,y=0,angle=0,crayon=1,couleur='black',epaisseur=1)"""
- def __init__(self,canvas,x=0,y=0,angle=0,crayon=1,couleur='black',epaisseur=1):
- self.canvas=canvas
- self.crayon=crayon
- self.x=x
- self.y=y
- self.angle=angle
- self.couleur=couleur
- self.epaisseur=epaisseur
- def __repr__(self):
- return "canvas : %s\ncrayon :%d\nx : %d\ny : %d\nangle : %d" %(self.canvas,self.crayon,self.x,self.y,self.angle)
- def avance(self,l):
- """avance(l)\n\nl:distance dont on souhaite avancer"""
- X,Y=self.x+l*cos(radians(self.angle)),self.y-l*sin(radians(self.angle))
- if self.crayon:
- self.canvas.create_line(self.x,self.y,X,Y,fill=self.couleur,width=self.epaisseur)
- self.x,self.y=X,Y
- def tourne_gauche(self,theta):
- """tourne_gauche(theta)\n\nTourne à gauche d'un angle theta"""
- self.angle+=theta
- def tourne_droite(self,theta):
- """tourne_droite(theta)\n\nTourne à droite d'un angle theta"""
- self.tourne_gauche(-theta)
- def pose_crayon(self):
- """pose_crayon()\n\nPose le crayon"""
- self.crayon=1
- def leve_crayon(self):
- """leve_crayon()\n\nLève le crayon"""
- self.crayon=0
-
- def koch(T,l,n):
- # Fractacle de Koch
- if n<=0:
- T.avance(l)
- else:
- koch(T,l/3,n-1)
- T.tourne_gauche(60)
- koch(T,l/3,n-1)
- T.tourne_droite(120)
- koch(T,l/3,n-1)
- T.tourne_gauche(60)
- koch(T,l/3,n-1)
-
- def flocon(T,l,n):
- # Flocon de Koch
- koch(T,l,n)
- T.tourne_droite(120)
- koch(T,l,n)
- T.tourne_droite(120)
- koch(T,l,n)
-
- def arbre(T,l,n):
- # arbre fractal
- if n<=0:
- T.avance(l)
- T.avance(-l)
- else:
- T.avance(0.7*l)
- T.tourne_gauche(30)
- arbre(T,2*l/3,n-1)
- T.tourne_droite(60)
- arbre(T,2*l/3,n-1)
- T.tourne_gauche(30)
- T.avance(-0.7*l)
-
- def arbre_random(T,l,n):
- # arbre fractal randomisé
- if n<=0:
- T.avance(l)
- T.avance(-l)
- else:
- longueur=uniform(0.5*l,0.8*l)
- tampon=T.epaisseur
- T.epaisseur=int(longueur/6)
- T.avance(longueur)
- angle_g=randrange(10,45)
- T.tourne_gauche(angle_g)
- arbre_random(T,4*l/5,n-1)
- angle_d=randrange(10,45)
- T.tourne_droite(angle_g+angle_d)
- arbre_random(T,4*l/5,n-1)
- T.tourne_gauche(angle_d)
- T.avance(-longueur)
- T.epaisseur=tampon
-
- def dragon(T,l,n):
- # fractale du dragon
- # (récursivité croisée)
- k=sqrt(2)/2
- def dragon_endroit(T1,l1,n1):
- if n1<=0:
- T1.avance(l1)
- else:
- T1.tourne_gauche(45)
- dragon_endroit(T1,l1*k,n1-1)
- T1.tourne_droite(90)
- dragon_envers(T1,l1*k,n1-1)
- T1.tourne_gauche(45)
- def dragon_envers(T2,l2,n2):
- if n2<=0:
- T2.avance(l2)
- else:
- T2.tourne_droite(45)
- dragon_endroit(T2,l2*k,n2-1)
- T2.tourne_gauche(90)
- dragon_envers(T2,l2*k,n2-1)
- T2.tourne_droite(45)
- dragon_endroit(T,l,n)
-
-
- if __name__=='__main__':
- root=Tk()
- can=Canvas(root,height=400,width=1000,bg='white')
- can.pack()
- T=tortue(can)
- T.y=150
- flocon(T,300,5)
- T.x=475
- T.y=350
- T.angle=90
- arbre_random(T,100,10)
- T.x=750
- T.y=250
- T.angle=0
- dragon(T,200,15)
- root.mainloop()
-
#! /usr/bin/python
# -*- coding:utf-8 -*-
from Tkinter import *
from math import radians,cos,sin,sqrt
from random import randrange,uniform
class tortue:
"""Tortue(canvas,x=0,y=0,angle=0,crayon=1,couleur='black',epaisseur=1)"""
def __init__(self,canvas,x=0,y=0,angle=0,crayon=1,couleur='black',epaisseur=1):
self.canvas=canvas
self.crayon=crayon
self.x=x
self.y=y
self.angle=angle
self.couleur=couleur
self.epaisseur=epaisseur
def __repr__(self):
return "canvas : %s\ncrayon :%d\nx : %d\ny : %d\nangle : %d" %(self.canvas,self.crayon,self.x,self.y,self.angle)
def avance(self,l):
"""avance(l)\n\nl:distance dont on souhaite avancer"""
X,Y=self.x+l*cos(radians(self.angle)),self.y-l*sin(radians(self.angle))
if self.crayon:
self.canvas.create_line(self.x,self.y,X,Y,fill=self.couleur,width=self.epaisseur)
self.x,self.y=X,Y
def tourne_gauche(self,theta):
"""tourne_gauche(theta)\n\nTourne à gauche d'un angle theta"""
self.angle+=theta
def tourne_droite(self,theta):
"""tourne_droite(theta)\n\nTourne à droite d'un angle theta"""
self.tourne_gauche(-theta)
def pose_crayon(self):
"""pose_crayon()\n\nPose le crayon"""
self.crayon=1
def leve_crayon(self):
"""leve_crayon()\n\nLève le crayon"""
self.crayon=0
def koch(T,l,n):
# Fractacle de Koch
if n<=0:
T.avance(l)
else:
koch(T,l/3,n-1)
T.tourne_gauche(60)
koch(T,l/3,n-1)
T.tourne_droite(120)
koch(T,l/3,n-1)
T.tourne_gauche(60)
koch(T,l/3,n-1)
def flocon(T,l,n):
# Flocon de Koch
koch(T,l,n)
T.tourne_droite(120)
koch(T,l,n)
T.tourne_droite(120)
koch(T,l,n)
def arbre(T,l,n):
# arbre fractal
if n<=0:
T.avance(l)
T.avance(-l)
else:
T.avance(0.7*l)
T.tourne_gauche(30)
arbre(T,2*l/3,n-1)
T.tourne_droite(60)
arbre(T,2*l/3,n-1)
T.tourne_gauche(30)
T.avance(-0.7*l)
def arbre_random(T,l,n):
# arbre fractal randomisé
if n<=0:
T.avance(l)
T.avance(-l)
else:
longueur=uniform(0.5*l,0.8*l)
tampon=T.epaisseur
T.epaisseur=int(longueur/6)
T.avance(longueur)
angle_g=randrange(10,45)
T.tourne_gauche(angle_g)
arbre_random(T,4*l/5,n-1)
angle_d=randrange(10,45)
T.tourne_droite(angle_g+angle_d)
arbre_random(T,4*l/5,n-1)
T.tourne_gauche(angle_d)
T.avance(-longueur)
T.epaisseur=tampon
def dragon(T,l,n):
# fractale du dragon
# (récursivité croisée)
k=sqrt(2)/2
def dragon_endroit(T1,l1,n1):
if n1<=0:
T1.avance(l1)
else:
T1.tourne_gauche(45)
dragon_endroit(T1,l1*k,n1-1)
T1.tourne_droite(90)
dragon_envers(T1,l1*k,n1-1)
T1.tourne_gauche(45)
def dragon_envers(T2,l2,n2):
if n2<=0:
T2.avance(l2)
else:
T2.tourne_droite(45)
dragon_endroit(T2,l2*k,n2-1)
T2.tourne_gauche(90)
dragon_envers(T2,l2*k,n2-1)
T2.tourne_droite(45)
dragon_endroit(T,l,n)
if __name__=='__main__':
root=Tk()
can=Canvas(root,height=400,width=1000,bg='white')
can.pack()
T=tortue(can)
T.y=150
flocon(T,300,5)
T.x=475
T.y=350
T.angle=90
arbre_random(T,100,10)
T.x=750
T.y=250
T.angle=0
dragon(T,200,15)
root.mainloop()
Sources du même auteur
Sources de la même categorie
Commentaires et avis
|
Derniers Blogs
CSS CONTENT STATE SELECTORS (PERSONNAL DRAFT)CSS CONTENT STATE SELECTORS (PERSONNAL DRAFT) par FREMYCOMPANY
Bonjour à tous, Je viens de publier une proposition comprenant 5 pseudo-classes pour le CSS Working Group ayant trait à l'état de chargement d'un élément (ex: IMG,VIDEO,AUDIO,OBJECT pour l'HTML.). Si le c½ur vous en dit, vous pouvez retrouver cette p...
Cliquez pour lire la suite de l'article par FREMYCOMPANY MBA : POURQUOI FAIRE ET COMMENT LE CHOISIR ?MBA : POURQUOI FAIRE ET COMMENT LE CHOISIR ? par ROMELARD Fabrice
Formation initiale Durant la formation, le découpage classique est le suivant (je donnerai les équivalences Suisse lorsque je les connaîtrais) : Ecole primaire jusqu'au Collège : Formation générale permettant d'obtenir les méthodes...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice Y'A DES ERREURS QUI PEUVENT RENDRE LE DéVELOPPEUR VIOLENTY'A DES ERREURS QUI PEUVENT RENDRE LE DéVELOPPEUR VIOLENT par Aleks
Quand on a ce genre d'erreur sans log :
Et bas on a juste envie de choper le gas de Microsoft qu'a développé ça et lui foutre des baffes de Coboye ! ...
Cliquez pour lire la suite de l'article par Aleks [HYPER-V 3] PRéSENTATION DES COMMANDLETS POWERSHELL[HYPER-V 3] PRéSENTATION DES COMMANDLETS POWERSHELL par Pierrick CATRO-BROUILLET
Avec la sortie prochaine de la Beta Consumer Preview de Windows 8, j'avais envie de revenir sur une des fonctionnalités que j'attends le plus et que, en bon geek que je suis, j'utilise déjà : Hyper-V 3 ainsi son module PowerShell.
Il y a déjà pléthor...
Cliquez pour lire la suite de l'article par Pierrick CATRO-BROUILLET IIS7 - COMPRESSION GZIPIIS7 - COMPRESSION GZIP par cyril
La compression GZIP permet d'améliorer les performances de navigation en compressant ce qu'envoie le serveur à un client. Pour comprendre comment cela fonctionne, regardons ce qu'il se passe au niveau HTTP lorsqu'un client tente d'accéder à une ress...
Cliquez pour lire la suite de l'article par cyril
Forum
PYVISA PROBLèMEPYVISA PROBLèME par sandrine44
Cliquez pour lire la suite par sandrine44
Logiciels
Easy-Planning (1.0.0.1)EASY-PLANNING (1.0.0.1)Basé sur les mêmes principes que MyPlanning, Easy-Planning permet de créer des plannings sous la ... Cliquez pour télécharger Easy-Planning Academy System (17.1.3.0)ACADEMY SYSTEM (17.1.3.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System COLLECTOR PLUS (3.00B)COLLECTOR PLUS (3.00B)COLLECTOR PLUS version 3.00B est un logiciel utilisant une base de données alimentée par :
- L... Cliquez pour télécharger COLLECTOR PLUS PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO LettresFaciles 2011 (8.0.0.1)LETTRESFACILES 2011 (8.0.0.1)LettresFaciles est un logiciel facilitant la création et la rédaction de lettres types.
Son inte... Cliquez pour télécharger LettresFaciles 2011
|