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
[WP7] AJOUTER DES IMAGES DANS LA MEDIA LIBRARY D'UN WINDOWS PHONE 7[WP7] AJOUTER DES IMAGES DANS LA MEDIA LIBRARY D'UN WINDOWS PHONE 7 par Audrey
L'émulateur Windows Phone 7, fourni avec la version Beta des outils développeurs n'inclut aucune image dans sa bibliothèque. Pas très pratique de tester son application lorsque l'on souhaite que l'utilisateur puisse choisir une image présente dans le télé...
Cliquez pour lire la suite de l'article par Audrey VIVE LES MOCKS ET LES POCOSVIVE LES MOCKS ET LES POCOS par vLabz
J'observe régulièrement autour de moi de la confusion à propos de ces deux termes et j'aimerais juste rappeler ce qu'ils signifient. Je ne suis bien sûr pas le mieux placé pour faire une leçon mais je vais faire de mon mieux pour mettre en valeur ce q...
Cliquez pour lire la suite de l'article par vLabz [WF4] WORKFLOW AND CUSTOM ACTIVITIES - BEST PRACTICES (4/5)[WF4] WORKFLOW AND CUSTOM ACTIVITIES - BEST PRACTICES (4/5) par JeremyJeanson
Vendredi dernier Microsoft a publié le quatrième épisode des bonnes pratiques pour coder ses activités custom dans WF4 : endpoint.tv - Workflow and Custom Activities - Best Practices (Part 4) . Tout comme pour les précédents épisodes, j'ai pris le temps d...
Cliquez pour lire la suite de l'article par JeremyJeanson DéVELOPPEMENT MOBILE : .NET COMPACT FRAMEWORK & LIMITATIONSDéVELOPPEMENT MOBILE : .NET COMPACT FRAMEWORK & LIMITATIONS par Pi-R
Introduction :
Le développement d'applications mobiles est quelque peu différent du développement d'applications sous Windows. En effet, le développement d'applications mobiles se base sur le .NET Compact Fra...
Cliquez pour lire la suite de l'article par Pi-R IPHONE VERSUS WP7 CODINGIPHONE VERSUS WP7 CODING par Nicolas
Je relais une présentation sur slideshare.net, qui compare le développement sur Iphone et Windows Phone 7, qui ma fait sourire. I phone versus windows phone 7 coding View more presentations from www.donburnett.com. J'aurais bien aimé une comparai...
Cliquez pour lire la suite de l'article par Nicolas
Logiciels
Xilisoft HD Vidéo Convertisseur 6 (6.0.3.0421)XILISOFT HD VIDéO CONVERTISSEUR 6 (6.0.3.0421)Xilisoft HD Vidéo Convertisseur est un outil professionnel de conversion HDTV, conçu pour transfo... Cliquez pour télécharger Xilisoft HD Vidéo Convertisseur 6 Xilisoft MP4 Convertisseur 6 (6.0.2.0415)XILISOFT MP4 CONVERTISSEUR 6 (6.0.2.0415)Xilisoft MP4 Convertisseur est un outil puissant pour la conversion de vidéo MP4, qui peut conver... Cliquez pour télécharger Xilisoft MP4 Convertisseur 6 Vade Retro Desktop (3.03)VADE RETRO DESKTOP (3.03)Le logiciel antispam Vade Retro pour Microsoft Outlook®, Outlook Express® et Windows Mail®(Vista)... Cliquez pour télécharger Vade Retro Desktop Malwarebytes Anti Malwares (1.46)MALWAREBYTES ANTI MALWARES (1.46)Malwarebytes' Anti-Malware est un anti-malware qui peut éliminer même les plus avancés des logic... Cliquez pour télécharger Malwarebytes Anti Malwares
|