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
[RIA SERVICES] MAITRE - DéTAIL ET DOMAINDATASOURCE[RIA SERVICES] MAITRE - DéTAIL ET DOMAINDATASOURCE par Audrey
A l'occasion d'un projet client, j'ai utilisé RIA Services avec Silverlight 3 (mais cela fonctionne aussi avec la version 4), et je l'ai utilisé pour une interface façon Maitre / Détail. Voici comment j'ai procédé pour arriver à mes fins. Nous allons pren...
Cliquez pour lire la suite de l'article par Audrey CSDL FUNCTIONCSDL FUNCTION par Matthieu MEZIL
Dans mon post précédent , j'ai utilisé une CSDL Function afin de générer une requête SQL avec un DateDiff utilisant la date courante sur la BD à partir d'une requête LINQ. Dans le cadre de ce post , vous avez probablement remarqué que dans le cadre de plu...
Cliquez pour lire la suite de l'article par Matthieu MEZIL LINQ TO ENTITIESLINQ TO ENTITIES par Matthieu MEZIL
Cette semaine je suis à Montréal en tant que speaker sur Entity Framework pour l'évènement confoo . J'en profite pour remercier les organisateurs de cet évènement de m'avoir fait confiance et Access-IT de m'avoir permis d'y participer. En parallèle, j'ai ...
Cliquez pour lire la suite de l'article par Matthieu MEZIL FAIRE APPARAITRE L'ONGLET 'DéVELOPPEUR' DANS OFFICE 2010FAIRE APPARAITRE L'ONGLET 'DéVELOPPEUR' DANS OFFICE 2010 par neodante
La nouvelle interface d'Office 2010 à amener quelques modifications par rapport à celle de 2007. Certes mineures, ces modifications ont fait disparaître la case à cocher de l'onglet 'Développeur' en première page du panneau du 'bouton Office' (dans Office...
Cliquez pour lire la suite de l'article par neodante [ASTUCE] PATCH POUR MICROSOFT FORUMS NNTP BRIDGE V1[ASTUCE] PATCH POUR MICROSOFT FORUMS NNTP BRIDGE V1 par pierre
Si vous avez téléchargé comme moi Microsoft Forums NNTP Bridge V1 avant le 11 mars 2010 (voir [Astuce] Disponibilité de Microsoft Forum NNTP Bridge Version 1.0), un problème de date localisée pour les non anglais était présent. Un patch est disponibl...
Cliquez pour lire la suite de l'article par pierre
Logiciels
Xilisoft Convertisseur Vidéo Ultimate (5.1.39.0305)XILISOFT CONVERTISSEUR VIDéO ULTIMATE (5.1.39.0305)Xilisoft Convertisseur Vidéo Ultimate est un outil puissant de conversion vidéo, facile à utilise... Cliquez pour télécharger Xilisoft Convertisseur Vidéo Ultimate Xilisoft DVD Ripper Ultimate (5.0.64.0304)XILISOFT DVD RIPPER ULTIMATE (5.0.64.0304)Xilisoft DVD Ripper Ultimate est un logiciel excellent pour copier et convertir DVD vers presque ... Cliquez pour télécharger Xilisoft DVD Ripper Ultimate Rigs of Rods (63.3)RIGS OF RODS (63.3)c'est un jeu de multi-simulation camions,autobus voitures, avions, bateaux, hélicoptère avec défo... Cliquez pour télécharger Rigs of Rods Konvertor (4.00)KONVERTOR (4.00)Le logiciel est un gestionnaire multimedia affichant, jouant et convertissant plus de 2000 format... Cliquez pour télécharger Konvertor
|