Back

Quand on s’ennuie…

2012-03-18

… on dessine des fractales (à la main bien entendu).

Voici le résultat : dossier de fractales

Pour ceux que ça intéresse, le code en Caml qui a permi de les générer :

#open "graphics";;
open_graph " 1024x768+0-0";;

(* HSV to RGB *)
let hsv h s v =
    let c = v *. s in
    let hh = (h mod 360)/60 in
    let hhf = (mod_float (float_of_int h) 360.) /. 60. in
    let x = c *. (1. -. (abs_float (mod_float hhf 2. -. 1.))) in
    let m = v -. c in
    let cc = int_of_float ((c +. m) *. 255.) in
    let xx = int_of_float ((x +. m) *. 255.) in
    let mm = int_of_float (m *. 255.) in
    match hh with
    | 0 -> rgb cc xx mm
    | 1 -> rgb xx cc mm
    | 2 -> rgb mm cc xx
    | 3 -> rgb mm xx cc
    | 4 -> rgb xx mm cc
    | 5 -> rgb cc mm xx
    | _ -> rgb mm mm mm;;


(* complex numbers *)
let zeroC = (0., 0.);;
let addC (a, b) (c, d) = (a+.c, b+.d);;
let mulC (a, b) (c, d) = (a*.c -. b*.d, a*.d +. b*.c);;
let absC (a, b) = sqrt(a**2. +. b**2.);;

(* drawing *)
let pos (xmin, xmax, ymin, ymax) (x, y) =
    int_of_float((x-.xmin)*.(float_of_int(size_x()))/.(xmax-.xmin)),
    int_of_float((y-.ymin)*.(float_of_int(size_y()))/.(ymax-.ymin));;
let pos_inv (xmin, xmax, ymin, ymax)  (x, y) =
    float_of_int(x)*.(xmax-.xmin)/.(float_of_int(size_x()))+.xmin,
    float_of_int(y)*.(ymax-.ymin)/.(float_of_int(size_y()))+.ymin;;


(* mandelbrot & julia fractal *)
let iter = 300;;

let color_frac n =
    if n = iter then set_color black
    else set_color (hsv (n*4+200) 0.7 0.6);;

let mandel_pt c =
    let rec aux z n =
        if n = iter then n
        else if absC z >= 2. then n
        else aux (addC (mulC z z) c) (n+1)
    in
        aux zeroC 0;;

let mandel repere =
    for i = 0 to size_x() do
        for j = 0 to size_y() do
            let x, y = pos_inv repere (i, j) in
            let n = mandel_pt (x, y) in
            let px, py = pos repere (x, y) in
            color_frac n;
            plot i j
        done
    done;
    set_color black;
    moveto 10 10;
    draw_string ("mandelbrot  iter = " ^ (string_of_int iter))  ;;

let julia_pt c z0 =
    let rec aux z n =
        if n = iter then n
        else if absC(z) >= 2. then n
        else aux ((addC (mulC z z) c)) (n+1) 
    in aux z0 0;;
    
let julia repere (a, b) =
    (* clear_graph(); *)
    for i = 0 to size_x() do
        for j = 0 to size_y() do
            let x, y = pos_inv repere (i, j) in
            let n = julia_pt (a, b) (x, y) in
            let px, py = pos repere (x, y) in
            color_frac n;
            plot i j
        done
    done;
    set_color black;
    moveto 10 10;
    draw_string ("julia " ^ (string_of_float a)
                                ^ " + " ^ (string_of_float b)
                                ^ "i   iter = " ^ (string_of_int iter));;

let repere = (-.1.6, 1.6, -.1.5, 1.5);;
mandel repere;;
julia repere (0.3, 0.01);;
julia repere (0.3, 0.024);;
julia repere (0.33, 0.024);;
julia repere (0.18, 0.7);;
julia repere (-.0.54, 0.55);;
julia repere (-0.4, 0.6);;
julia repere (0.285, 0.01);;
julia repere (-0.70176, 0.3842);;
julia repere (-0.835, 0.2321);;
julia repere (-0.8, 0.156);;

Pour pouvoir profiter du spectacle, je vous invite à utiliser ce code avec un IDE permettant l’execution pas-à-pas du code (LinCaml, WinCaml ou autre MacCaml), ou à le rentrer directement à l’invite OCaml.