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
TECHDAYS PARIS 2010 : PLAN DE MIGRATION VERS SHAREPOINT 2010TECHDAYS PARIS 2010 : PLAN DE MIGRATION VERS SHAREPOINT 2010 par ROMELARD Fabrice
Animé par: Arnault Nouvel et Antoine Dongois Le processus à prendre : Apprendre (découvrir la plateforme) Préparer (documenter l'historique et choisir la méthode de MAJ) Test (Test de MAJ) Implémenter (Effectuer la MAJ) Valid...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice TECHDAYS PARIS 2010 : LA PLEINIèRE DU SECOND JOURTECHDAYS PARIS 2010 : LA PLEINIèRE DU SECOND JOUR par ROMELARD Fabrice
Après un retour sur l'histoire des TechDays de Paris et le fait que ce soit le plus gros event MS au monde (du fait de sa gratuité), le président de MS France (Eric Boustoullier) a fait une présentation de la vision Microsoft pour les années à venir...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice CRéATION D'UNE BASE DE DONNéE SOUS SQL AZURECRéATION D'UNE BASE DE DONNéE SOUS SQL AZURE par junarnoalg
Sans rentrer dans les détails, je me propose ici de faire un rapide tour de ce que propose SQL Azure.
SQL Azure est avant tout un service d'hébergement de base de données relationnelles construit sur SQL Server. Il permet aux entreprises d...
Cliquez pour lire la suite de l'article par junarnoalg TECHDAYS PARIS 2010 : LES SERVICES D'APPLICATIONS DANS SHAREPOINT 2010TECHDAYS PARIS 2010 : LES SERVICES D'APPLICATIONS DANS SHAREPOINT 2010 par ROMELARD Fabrice
Animé par: Xavier Moreels et Julien Bakmezdjian Ce sujet est lié au partage des applications comme services dans SharePoint 2010, ceci représente la possibilité de créer sa propre application qui sera utilisable comme ceux en standard : Search...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice
Logiciels
DB-MAIN (9.1.0)DB-MAIN (9.1.0)DB-MAIN is a data-modeling and data-architecture tool. It is designed to help developers and anal... Cliquez pour télécharger DB-MAIN Xilisoft DPG Convertisseur (5.1.37.0120)XILISOFT DPG CONVERTISSEUR (5.1.37.0120)Xilisoft DPG Convertisseur offre aux fans de Nintendo DS une bonne solution leur permettant de dé... Cliquez pour télécharger Xilisoft DPG Convertisseur GraphicsGale (2.01.01)GRAPHICSGALE (2.01.01)GraphicsGale est un logiciel de PixelArt avec de nombreuse fonctionnalités permettant de réalisé ... Cliquez pour télécharger GraphicsGale Architecte 3D (Platinum 2010)ARCHITECTE 3D (PLATINUM 2010)Architecte 3D Platinium vous permet de concevoir facilement les plans votre future maison, de l'é... Cliquez pour télécharger Architecte 3D TeamViewer 5 (TeamViewer 5)TEAMVIEWER 5 (TEAMVIEWER 5)Dépanner un ami,expliquer une manipulation devient un jeu d'enfant.
Prise en main d'un autre ord... Cliquez pour télécharger TeamViewer 5
|