begin process at 2010 07 29 16:00:59
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

Math & Algorithmes

 > IMPLÉMENTATION DE LA TORTUE LOGO, APPLICATION AUX FRACTALES

IMPLÉMENTATION DE LA TORTUE LOGO, APPLICATION AUX FRACTALES


 Information sur la source

Note :
10 / 10 - par 3 personnes
10,00 / 10

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10
Catégorie :Math & Algorithmes Classé sous :tortue, fractale Niveau :Débutant Date de création :16/08/2006 Vu / téléchargé :3 091 / 143

Auteur : jpountz

Ecrire un message privé
Commentaire sur cette source (0)
Ajouter un commentaire et/ou une note

 Description

Cliquez pour voir la capture en taille normale
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()



 Fichier Zip

Les Membres Club peuvent télécharger directement un fichier contenu dans le zip sans télécharger le zip en entier !

Télécharger le zip


 Sources du même auteur

Source avec Zip Source avec une capture MISE EN ORBITE D'UN SATELLITE
Source avec une capture COURSE DE CHEVAUX
Source avec une capture CONVERTISSEUR FICHIER CAML -> FICHIER HTML

 Sources de la même categorie

CALCUL LIST DE NOMBRES PREMIERS par White541
TROUVER TOUT LES QUADRILATÉRES POSSIBLES AVEC N POINTS ALEAT... par Buenol
Source avec Zip GÉNÉRATION D'UN LABYRINTHE AVEC RECHERCHE DU CHEMIN LE PLUS ... par mehdicherti
Source avec Zip Source avec une capture DIVISIONS AVEC PRÉCISION RÉGLABLE par Clempython
Source avec Zip Source avec une capture LE CALCULATOR DE RAYGOLD VERSION 3.1 par raygold

Commentaires et avis

Aucun commentaire pour le moment.

 Ajouter un commentaire




Nos sponsors


Sondage...

Comparez les prix

CalendriCode

Juillet 2010
LMMJVSD
   1234
567891011
12131415161718
19202122232425
262728293031 

Consulter la suite du CalendriCode

 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 2,012 sec (4)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales