https://gitorious.org/ocamlp3l/ocamlp3l_cvs.git
Raw File
Tip revision: ad58ae426e7e9200530d18bf439d02657503426c authored by fclement on 23 November 2010, 11:33 UTC
Ignore all generated files.
Tip revision: ad58ae4
mandeldoc.ml
open Graphics;;

let size = 300;; (* The size of the square screen windows in pixels      *)
let resolution = 100;; (* The resolution: maximum number of iterations allowed *)
let threshold = 4.0;; (* The thresold: the maximum size allowed. *)
let distance = 2.0 /. (float size -. 1.0);; (* The distance between two rows. *)

(* Convert an integer in the range [0..resolution] into a screen color *)
let color_of c resolution =
  Pervasives.truncate ((float c /. float resolution) *. float white);;

(* Compute the color of a pixel by iterating z_m+1 = z_m^2 + c          *)
(* j is the k-th row, initialised so that j.(i), k  are the coordinates *)
(* of the pixel (i, k)                                                  *)

let pixel_row (j, k, resolution, size) =
  let zr = ref 0.0
  and zi = ref 0.0
  and cr = ref 0.0
  and ci = ref 0.0
  and zrs = ref 0.0
  and zis = ref 0.0

  let coloured_row = Array.create size black in

  for s = 0 to size - 1 do
    let j1 = ref (float  j.(s)) in
    let k1 = ref (float  k) in
    zr := !j1 *. distance -. 1.0;
    zi := !k1 *. distance -. 1.0;
    cr := !zr;
    ci := !zi;
    zrs := 0.0;
    zis := 0.0;
    for i = 0 to resolution - 1 do
      if not (!zrs +. !zis > threshold) then begin
        zrs := !zr *. !zr;
        zis := !zi *. !zi;
        zi := 2.0 *. !zr *. !zi +. !ci;
        zr := !zrs -. !zis +. !cr;
        Array.set coloured_row s (color_of i resolution);
      end
    done
  done;
  (coloured_row, k);;

(* draw a line on the screen using fast image functions *)

let show_a_result (col,j) = draw_image (make_image [| col |]) 0 j;;

(* generate the tasks *)

let gen_rows =
  let seed = ref 0 in
  let iniv = Array.init size (fun i -> i) in
  (function () ->
    if !seed >= size then raise End_of_file else
    let r = (iniv, !seed, resolution, size) in
    incr seed;
    r;;

(* initialize the stop node: open the graphics window *)

let stopinitf () =
  print_string "opening..."; print_newline ();
  open_graph (Printf.sprintf " %ix%i" size size);;

(* finalize: close the window *)

let stopfinalize () =
  Unix.sleep 120;
  close_graph ();;

(* the skeleton expression to compute the image *)

let prog () =
  startstop
    (gen_rows, ignore)
    (show_a_result, stopinitf, stopfinalize)
    (farm (seq (pixel_row), 10)) in
pardo prog;;
back to top