(* Le flocon de Koch *)

open Graphics

let () = open_graph " 600x200"

let pi_3 = 4. *. atan 1. /. 3.
let cos_pi_3 = cos pi_3
let sin_pi_3 = sin pi_3

(* Trace la courbe entre les points (x1,y1) et (x2,y2) avec n ├ętapes. *)
let rec von_koch x1 y1 x2 y2 n =
  if n = 0 then begin
    moveto (truncate x1) (truncate y1);
    lineto (truncate x2) (truncate y2)
  end else begin
    let dx = (x2 -. x1) /. 3. in
    let dy = (y2 -. y1) /. 3. in
    let x3 = x1 +. dx in
    let y3 = y1 +. dy in
    let x5 = x1 +. 2. *. dx in
    let y5 = y1 +. 2. *. dy in
    let x4 = x3 +. cos_pi_3 *. dx -. sin_pi_3 *. dy in
    let y4 = y3 +. sin_pi_3 *. dx +. cos_pi_3 *. dy in
    von_koch x1 y1 x3 y3 (n-1);
    von_koch x3 y3 x4 y4 (n-1);
    von_koch x4 y4 x5 y5 (n-1);
    von_koch x5 y5 x2 y2 (n-1);
  end

let () =
  von_koch 0. 0. 600. 0. 8;
  ignore (read_key ())


This document was generated using caml2html