Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Simple mandelbrot program

28 views
Skip to first unread message

WJ

unread,
Nov 27, 2015, 11:23:10 AM11/27/15
to
WARNING: BE SURE TO REDIRECT THE OUTPUT TO A FILE OR
YOU MAY HAVE TO REBOOT YOUR COMPUTER!


(*
! ! ! Redirect output to file with .ppm extension! ! ! !
*)

module Cm = Complex

let max_iterations = 512
let step_size = 0.0025
let x_min = (-2.0)
let x_max = 1.0000001 -. step_size
let y_min = (-1.2)
let y_max = 1.2000001 -. step_size
let pixels_per_row = (truncate ((x_max -. x_min) /. step_size)) + 1
let pixels_per_column = (truncate ((y_max -. y_min) /. step_size)) + 1;;

Printf.eprintf "pixels per row: %d\n" pixels_per_row;;
Printf.eprintf "pixels per column: %d\n" pixels_per_column;;

(* Printing without added carriage-returns: *)
set_binary_mode_out stdout true ;;


Printf.printf "P5\n%d %d\n255\n" pixels_per_row pixels_per_column;;

let float_lo_hi_by lo hi step func =
let x = ref lo in
while !x <= hi do
func !x ;
x := !x +. step
done

let calc_color count =
let tmp =
if count < 2 then
20
else if count=max_iterations then
0
else
truncate (((log (float count)) /. (log (float max_iterations))) *. 256.)
in 255 - tmp

let mandel () =
float_lo_hi_by y_min y_max step_size
(fun y ->
float_lo_hi_by x_min x_max step_size
(fun x ->
let a = {Complex.re = x; im = y} in
let rec z_loop z count =
if (count = max_iterations) || (Cm.norm2 z) > 4.0 then
let color = calc_color count in
Printf.printf "%c" (char_of_int color)
else
z_loop (Cm.add a (Cm.mul z z)) (count + 1)
in z_loop a 0) ;
Printf.eprintf "." ; flush_all ()) ;;

mandel ()
0 new messages