(* Dessin de l'ensemble de Mandelbrot, avec des couleurs et une possibilité de zoom. *) open Graphics let width = 800 let height = 800 let k = 100 let color i = if i = k then black else let f = truncate (255. *. float i /. float k) in rgb f f f let norm2 x y = x *. x +. y *. y let mandelbrot a b = let rec mandel_rec x y i = if i = k || norm2 x y > 4. then color i else let x' = x *. x -. y *. y +. a in let y' = 2. *. x *. y +. b in mandel_rec x' y' (i + 1) in mandel_rec 0. 0. 0 (* On commence par ajouter des paramètres xmin, ymin (le poitn inférieur gauche du dessins) et scale (l'échelle du dessin, c'est-à-dire la largeur représentée par les 800 picels). *) let xmin = ref (-2.) let ymin = ref (-2.) let scale = ref 4. let draw () = for w = 0 to width - 1 do for h = 0 to height - 1 do let a = !scale *. float w /. float width +. !xmin in let b = !scale *. float h /. float height +. !ymin in set_color (mandelbrot a b); plot w h done done (* On écrit ensuite une boucle infinie (while true) qui dessine puis adapte les paramètres du dessin en cas de clic souris. On sort de la boucle infinie en appuyant que la touche 'q'. Note : pour un dessin encore plus joli, il faudrait aussi augmenter le nombre d'itérations (k) lorsque l'on zoome. *) let () = let dim = Printf.sprintf " %dx%d" width height in open_graph dim; while true do draw (); let st = wait_next_event [Key_pressed; Button_down] in if st.keypressed && st.key = 'q' then exit 0; if st.button then begin let delta = !scale /. 20. in xmin := !xmin +. !scale *. float st.mouse_x /. float width -. delta; ymin := !ymin +. !scale *. float st.mouse_y /. float width -. delta; scale := 0.1 *. !scale end done