Correction en OCaml
Exercice 1.
let add (r1, i1) (r2, i2) = (r1 +. r2, i1 +. i2)
let mult (r1, i1) (r2, i2) =
((r1 *. r2) -. (i1 *. i2), (r1 *. i2) +. (r2 *. i1))
let norme2 (r, i) = sqrt (r *. r +. i *. i)
Exercice 2
let limite_iteration = 50
let limite_norme2 = 4.
let divergence c =
let rec iter i z_i =
if i >= limite_iteration || norme2 z_i >= limite_norme2
then i
else iter (succ i) (add (mult z_i z_i) c)
in iter 0 (0., 0.)
Exercice 3
#load "graphics.cma"
let gris n =
let c = (255 * n) / limite_iteration in
Graphics.rgb c c c
Exercice 4
let point (taille_x, taille_y) (x, y) =
(float (2 * x) /. float taille_x -. 1.5,
float (2 * y) /. float taille_y -. 1.)
Exercice 5
let mandelbrot taille_x taille_y =
Graphics.open_graph (Printf.sprintf " %dx%d" taille_x taille_y);
for i = 0 to taille_x - 1 do
for j = 0 to taille_y - 1 do
let () = Graphics.set_color
(gris (divergence (point (taille_x, taille_y) (i, j)))) in
Graphics.plot i j
done
done;
let _ = Graphics.read_key () in
Graphics.close_graph ()
let () = mandelbrot 800 600