(* to run : $ ocaml graphics.cma fractale.ml *)
open Graphics;;
let dX = 400;;
let dY = 400;;
let echelle=100.;;
let maxI=125;; (*le nombre maximal d'iterations*)
(*fait "comme" une boucle for, mais en recursif et en tail-rec*)
let rec each from toend fonction =
if from = toend then ()
else (fonction from; each (from+1) toend fonction);;
open Complex;;
let c= {re=0.-.0.727; im=0.1888};; (*parametre*)
(*calcule le nombre d'iterations*)
let rec complexe2int n z =
if (norm z) < 4. & n < maxI
then complexe2int (n+1) (add (mul z z) c)
else n
(*renvoie la couleur a partir du nombre complexe*)
let fonction_complexe_to_color z =
let n = (complexe2int 0 z) in
rgb (255*(maxI+1)/(n+1)) (255*(n+1)/(maxI+1)) 0;;
(*paint le point (x, y)*)
let paintPoint x y=
let z= {re=(float_of_int (x-dX/2)) /. echelle; im=(float_of_int (y-dY/2)) /. echelle}
in set_color ( fonction_complexe_to_color(z) ); plot x y;;
(*main-like*)
let () =
let height, width = dX, dY in
open_graph (Printf.sprintf " %ix%i" height width) ;
each 0 dX (fun x -> each 0 dY (fun y -> paintPoint x y));
let rec main_loop () = main_loop () in main_loop ();;