Dessin d'une fractale : l'ensemble de Mandelbrot
<sxh python; title : ensemble_mandelbrot.py> #!/usr/bin/env python # -*- coding: utf-8 -*- # version un peu aménagée du travail de BF, ba2 chimie 2012-2013 # ref : http://fr.wikipedia.org/wiki/Ensemble_de_Mandelbrot
from Tkinter import * from random import randrange
def mandel2(c):
z=0 for h in range(0,50): #nombre d'iteration z = z**2 + c if abs(z) > 2: #abs(z) correspond au module de z break #arrête l'execution du for si la condition est remplie if abs(z) >= 2: return False else: return True
root = Tk() w = Canvas(root, width=600, height=600, background='white' ) w.pack()
for hx in range(0,600,50):
w.create_line(0,hx,600,hx,fill="blue")
for hy in range(0,600,50):
w.create_line(hy,0,hy,600,fill="blue")
print (“Initializing…”)
for x in range(0,600):
real = x / 200.0 -2 for y in range(0,600): img = y / 200.0 -1.5 c = complex(real, img) if mandel2(c): w.create_line(x,600-y,x+1,601-y,fill="black") w.pack()
print (“Complete!”)
root.mainloop()
</sxh>
Améliorations possibles : cf. l'article https://www.researchgate.net/publication/272679245_NumPy__SciPy_Recipes_for_Image_Processing_Creating_Fractal_Images