(* la tortue *) #open "graphics";; open_graph "";; let turtle_x = ref 0.0 and turtle_y = ref 0.0 and turtle_h = ref 0.0 and turtle_p = ref false;; let draw () = let x = int_of_float !turtle_x in let y = int_of_float !turtle_y in if !turtle_p then lineto x y else moveto x y;; let forward d = turtle_x := !turtle_x +. d *. cos !turtle_h; turtle_y := !turtle_y +. d *. sin !turtle_h; draw ();; let pi = 4.0 *. atan 1.0;; let pi2 = pi /. 2.0;; let pi180 = atan 1.0 /. 45.0;; let turn t = turtle_h := t *. pi180 +. !turtle_h;; let pen_up () = turtle_p := false and pen_down () = turtle_p := true;; let center_x = float_of_int (size_x ()) /. 2.0;; let center_y = float_of_int (size_y ()) /. 2.0;; let center () = turtle_x := center_x; turtle_y := center_y; draw ();; let origine () = turtle_x := 0.0; turtle_y := 0.0; draw ();; let head_up () = turtle_h := pi2;; let cs () = clear_graph (); pen_up (); center (); pen_down (); head_up ();; (* Sierpinski *) (* on change la couleur du crayon à chaque trait pour faire plus joli *) let turtle_color = ref 0;; let next_color () = turtle_color := (!turtle_color + 1) mod 255; match !turtle_color mod 7 with | 0 -> black | 1 -> red | 2 -> green | 3 -> cyan | 4 -> magenta | 5 -> blue | _ -> yellow;; let forward' n = set_color (next_color ()); forward n;; (* Sierpinski *) let rec motif_s a d n = if n = 0 then forward d else begin turn (a); motif_s (-. a) (d/.2.) (n-1); turn (-. a); motif_s a (d/.2.) (n-1); turn (-. a); motif_s (-. a) (d/.2.) (n-1); turn (a) end;; let sierpinski d n = cs (); pen_up (); origine (); turn (-90.0); pen_down (); motif_s 60.0 d n;; let s n = sierpinski 400.0 n;; s 12;; (* Von Koch *) let rec motif_k d n = if n = 1 then forward d else begin motif_k (d /. 3.0) (n - 1); turn 60.0; motif_k (d /. 3.0) (n - 1); turn (-. 120.0); motif_k (d /. 3.0) (n - 1); turn 60.0; motif_k (d /. 3.0) (n - 1) end;; let koch d n = pen_up (); center (); pen_down (); motif_k d n; turn (-. 120.0); motif_k d n; turn (-. 120.0); motif_k d n; turn (-. 120.0);; let flocon n = clear_graph (); koch 250.0 n;; flocon 10;; (* Hilbert *) let rec motif_h a d n = if n < 0 then begin turn (a); turn (a) end else begin (* 1 *) turn (a); motif_h (-. a) d (n - 1); turn (a); (* 2 *) forward' d; motif_h (a) d (n - 1); (* 3 *) turn (-. a); forward' d; turn (-. a); motif_h (a) d (n - 1); (* 4 *) forward' d; turn (a); motif_h (-. a) d (n - 1); turn (a) end;; let hilbert taille n = pen_up (); origine (); head_up (); let d = taille /. 2.0 ** (float_of_int n) in forward (d /. 2.0); turn (-90.0); forward (d /. 2.0); turn 90.0; pen_down (); motif_h (-. 90.0) d n;; let h n = hilbert 137.0 n;; h 10;; (* Le dragon *) let rec motif_d a d n = if n = 0 then forward d else begin turn a; motif_d (abs_float a) d (n - 1); turn (-2.0 *. a); motif_d (-. (abs_float a)) d (n - 1); turn a end;; let dragon taille n = cs (); let d = taille /. sqrt 2.0 ** (float_of_int n) in motif_d 45.0 d n;; let d n = dragon 100.0 n;; d 3;;