Tree display incoming...
This commit is contained in:
parent
a90bc92f7b
commit
3e71a8e07d
Binary file not shown.
Binary file not shown.
|
@ -337,10 +337,75 @@ let fancy_dfs gr coords px bcls =
|
|||
|
||||
(* --------------------------------------| TESTS |-------------------------------------- *)
|
||||
|
||||
|
||||
(*
|
||||
let gr = [|[|3; 5; 7|]; [|0|]; [|1; 7; 8|]; [|2; 6|]; [|0; 1; 3|]; [|6; 7|]; [|0; 1; 2|]; [|8|]; [|0; 7; 6|]; [||]; [||]; [|9|]|] ;;
|
||||
|
||||
let (coords, map, blank_map, color_map, blank_color_map) = extremely_fancy_graph_printing gr 46 3 "SHOW" ;;
|
||||
|
||||
fancy_dfs gr coords map blank_color_map ;;
|
||||
*)
|
||||
|
||||
(* compilation command : ocamlfind ocamlc -linkpkg -package unix pretty_printing.ml *)
|
||||
(* --------------------------------------------------------------------------------------------------------- *)
|
||||
(* --------------------------------------------------------------------------------------------------------- *)
|
||||
(* --------------------------------------------------------------------------------------------------------- *)
|
||||
(* --------------------------------------------------------------------------------------------------------- *)
|
||||
open Graphics ;;
|
||||
|
||||
type 'a tree = Leaf of 'a | Node of 'a * 'a tree * 'a tree ;;
|
||||
|
||||
(*
|
||||
STRUCT : (digit, xcoord, ycoord)
|
||||
*)
|
||||
|
||||
let rec pw x n = match n with
|
||||
| 0 -> 1
|
||||
| 1 -> x
|
||||
| k when k mod 2 = 0 -> let res = pw x (n/2) in res*res
|
||||
| k -> let res = pw x (n/2) in res*res*x ;;
|
||||
|
||||
let rec depth_of_tree t = match t with
|
||||
| Leaf _ -> 1
|
||||
| Node (_, g, d) -> 1 + max (depth_of_tree g) (depth_of_tree d) ;;
|
||||
|
||||
let fill_data te ystep sx sy r =
|
||||
let depth = depth_of_tree te in
|
||||
let res = Array.make (depth+1) [] in
|
||||
let rec aux t cur_x cur_d spacing = match t with
|
||||
| Node (x, g, d) -> begin
|
||||
aux g (cur_x - spacing) (cur_d+1) (spacing/2);
|
||||
res.(cur_d) <- ((x, (cur_x, sy - r - ystep * cur_d)))::(res.(cur_d));
|
||||
aux d (cur_x + spacing) (cur_d+1) (spacing/2);
|
||||
end
|
||||
| Leaf x -> begin
|
||||
res.(cur_d) <- ((x, (cur_x, sy - r - ystep * cur_d)))::(res.(cur_d));
|
||||
end
|
||||
in aux te (sx/2) 0 (r/2 + r * ((pw 2 (depth-1)) - 1)); res ;;
|
||||
|
||||
let rec draw_list l d r = match l with
|
||||
| [] -> ()
|
||||
| h::t -> draw_circle (fst (snd h)) (snd (snd h)) r; draw_list t d r ;;
|
||||
|
||||
let even_more_pretty_printing t r ystep =
|
||||
open_graph " 1600x800" ;
|
||||
let sx = Graphics.size_x () in
|
||||
let sy = Graphics.size_y () in
|
||||
|
||||
let graphdata = fill_data t ystep sx sy r in
|
||||
|
||||
print_int 3;
|
||||
|
||||
for dpth = 0 to (Array.length graphdata -1) do
|
||||
draw_list graphdata.(dpth) dpth r
|
||||
done;
|
||||
|
||||
ignore (Sys.command "sleep 1");
|
||||
|
||||
close_graph () ;
|
||||
() ;;
|
||||
|
||||
even_more_pretty_printing (Node (2, Node (3, Leaf 1, Leaf 6), Node (9, Leaf 0, Node (4, Leaf 0, Node (2, Leaf 5, Leaf 2))))) 25 100 ;
|
||||
|
||||
(* compilation command : ocamlfind ocamlc -linkpkg -package unix -linkpkg -package graphics pretty_printing.ml *)
|
||||
print_int 0 ;;
|
||||
print_char '\n' ;;
|
Loading…
Reference in New Issue