fancy DFS implemented

This commit is contained in:
Alexandre 2024-05-20 15:48:36 +02:00
parent 7670c78cba
commit a90bc92f7b
4 changed files with 96 additions and 16 deletions

BIN
a.out

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -163,13 +163,14 @@ let display_specific mat cls digit =
print_char '\n'
done;;
let extend mat cls i0 j0 dst =
let extend dgt mat cls i0 j0 dst =
let ni = Array.length mat in
let nj = Array.length mat.(0) in
cls.(i0).(j0) <- dgt + 1;
for i = -dst to dst do
for j = -dst to dst do
if i0 - i >= 0 && j0 - j >= 0 && i0 - i < ni && j0 - j < nj then begin
cls.(i0-i).(j0-j) <- Char.code mat.(i0).(j0) - 48 + 1;
cls.(i0-i).(j0-j) <- dgt + 1;
if abs i = dst || abs j = dst then
mat.(i0-i).(j0-j) <- '*'
else if i <> 0 || j <> 0 then
@ -184,6 +185,39 @@ let extend mat cls i0 j0 dst =
let identity x = x ;;
let copy_arr src =
let dest = Array.make (Array.length src) [||] in
for i = 0 to (Array.length src -1) do
dest.(i) <- Array.make (Array.length src.(i)) src.(0).(0);
for j = 0 to (Array.length src.(i) -1) do
dest.(i).(j) <- src.(i).(j)
done
done;
dest ;;
let rec pw x n = match n with
| 0 -> 1
| 1 -> x
| k -> x * pw x (n-1) ;;
let to_hexa n =
let res = ref "" in
let remaining = ref n in
if n = 0 then
"0"
else begin
while !remaining > 0 do
let cd = ref (48 + !remaining mod 16) in
if !cd >= 58 then
cd := !cd + 7
else
();
res := ((String.make 1 (Char.chr !cd)) ^ !res);
remaining := !remaining / 16;
done;
!res ;
end;;
let extremely_fancy_graph_printing g size wmult mode =
(* creation of the image *)
let px = Array.make (size) [||] in
@ -197,26 +231,41 @@ let extremely_fancy_graph_printing g size wmult mode =
cls.(i) <- Array.make (wmult*size) 0
done;
if Array.length g >= 100 then
failwith "ERROR : graph is too big"
else
();
let coords = Array.make size (0, 0) in
(* placing the points on the trig circle *)
for k = 0 to Array.length g - 1 do
let theta = 2. *. pi *. (float_of_int k) /. (float_of_int (Array.length g)) in
let theta = 2. *. pi *. (float_of_int k) /. (float_of_int (Array.length g)) +. pi /. (float_of_int (Array.length g)) in
let i = ref (int_of_float ((float_of_int size) /. 2.) + int_of_float ((float_of_int size) /. 2. *. cos theta)) in
let j = ref (int_of_float ((float_of_int size) /. 2.) + int_of_float ((float_of_int size) /. 2. *. sin theta)) in
if !i < 0 then i := 0 else ();
if !j < 0 then j := 0 else ();
if !i >= size then i := size-1 else ();
if !j >= size then j := size-1 else ();
px.(!i).(wmult* !j) <- Char.chr (k + 48);
extend px cls !i (wmult* !j) 2;
if !i < 1 then i := 1 else ();
if !j < 1 then j := 1 else ();
if !i >= size-1 then i := size-1-1 else ();
if !j >= size-1 then j := size-1-1 else ();
px.(!i).(wmult* !j) <- '~';
extend k px cls !i (wmult* !j) 2;
let str_to_place = to_hexa k in
for sl = 0 to (String.length str_to_place -1) do
if !i + 1 < size && wmult* !j-sl-1 >= 0 && wmult* !j+sl-1 < Array.length px.(0) then
px.(!i + 1).(wmult* !j-sl-1) <- str_to_place.[sl]
else
()
done;
coords.(k) <- (!i, wmult* !j);
done;
let blankcls = copy_arr cls in
let bpx = copy_arr px in
(* draw the connections *)
for i = 0 to Array.length g -1 do
for j = 0 to Array.length g.(i) -1 do
draw_line_bresenham px cls i (fst coords.(i)) (snd coords.(i)) (fst coords.(g.(i).(j))) (snd coords.(g.(i).(j))) 4
draw_line_bresenham px cls i (fst coords.(i)) (snd coords.(i)) (fst coords.(g.(i).(j))) (snd coords.(g.(i).(j))) 0
done
done;
@ -253,14 +302,45 @@ let extremely_fancy_graph_printing g size wmult mode =
()
done
end
else if mode <> "HIDE" then
display px cls;
(coords, px, bpx, cls, blankcls) ;;
(* coords = (x, y) coords of graph point *)
(* px = 2D matrix containing what to display *)
(* cls = colors of associated pixel *)
let fancy_dfs gr coords px bcls =
Stdlib.print_endline "Enter any integer to begin DFS : ";
ignore (Scanf.bscanf Scanf.Scanning.stdin "%d\n" identity);
let n = Array.length gr in
let visited = Array.make n false in
visited.(0) <- true;
let rec explore nd =
if true then begin
for i = 0 to (Array.length gr.(nd) -1) do
draw_line_bresenham px bcls nd (fst coords.(nd)) (snd coords.(nd)) (fst coords.(gr.(nd).(i))) (snd coords.(gr.(nd).(i))) 4;
display px bcls;
Stdlib.print_endline "_";
ignore (Sys.command "sleep 1");
if visited.(gr.(nd).(i)) = false then begin
visited.(gr.(nd).(i)) <- true;
explore gr.(nd).(i);
end
done
end
else
display px cls ;;
()
in
explore 0;
display 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|]|] ;;
let gr = [|[|3; 5; 7|]; [|0|]; [|1; 7; 8|]; [|2; 6|]; [|0; 1; 3|]; [|6; 7|]; [|0; 1; 2|]; [|8|]; [|0; 7; 6|]; [||]; [||]; [|9|]|] ;;
(*print_mat gr ;;*)
let (coords, map, blank_map, color_map, blank_color_map) = extremely_fancy_graph_printing gr 46 3 "SHOW" ;;
extremely_fancy_graph_printing gr 44 4 "SPECIFIC" ;
fancy_dfs gr coords map blank_color_map ;;
(* ocamlfind ocamlc -linkpkg -package unix pretty_printing.ml *)
(* compilation command : ocamlfind ocamlc -linkpkg -package unix pretty_printing.ml *)