249 lines
9.0 KiB
OCaml
249 lines
9.0 KiB
OCaml
(*open Spectrum*)
|
|
|
|
let rec print_mat m =
|
|
for i = 0 to (Array.length m)-1 do
|
|
print_char ' ';
|
|
for j = 0 to (Array.length m.(i))-1 do
|
|
if m.(i).(j) <> -1 then
|
|
print_int m.(i).(j)
|
|
else
|
|
print_char '_';
|
|
print_char ' ';
|
|
print_char '|';
|
|
print_char ' ';
|
|
done;
|
|
print_char '\n';
|
|
for j = 0 to (Array.length m.(i))-1 do
|
|
print_char '-';
|
|
print_char '-';
|
|
print_char '-';
|
|
print_char '+';
|
|
done;
|
|
print_char '\n';
|
|
done ;;
|
|
|
|
exception ReturnBool of bool ;;
|
|
|
|
let pi = 3.14159265358979343 ;;
|
|
|
|
let abs x =
|
|
if x >= 0 then x else -x ;;
|
|
|
|
let absf x =
|
|
if x >= 0. then x else -1. *. x ;;
|
|
|
|
let is_rempaceable c arr =
|
|
try
|
|
for i = 0 to (Array.length arr - 1) do
|
|
if c = arr.(i) then
|
|
raise (ReturnBool true)
|
|
else
|
|
()
|
|
done;
|
|
raise (ReturnBool false)
|
|
with
|
|
| ReturnBool b -> b ;;
|
|
|
|
let draw_line_bresenham mat cls origin x1 y1 x2 y2 cutoff =
|
|
let slope = ref 0. in
|
|
let override_arr = [|' '; '.'; '|'; '-'|] in
|
|
if x2 <> x1 || y2 <> y1 then
|
|
slope := (float_of_int (y2 - y1) /. float_of_int (x2 - x1))
|
|
else
|
|
();
|
|
(*Printf.printf "(%f)\n" !slope;*)
|
|
if absf (!slope) < 1. then
|
|
if x1 < x2 then
|
|
for k = x1 to x2 do
|
|
let cur_y = ref ((!slope) *. float_of_int (k - x1) +. float_of_int y1) in
|
|
if !slope = 0. then cur_y := float_of_int y2 else ();
|
|
if is_rempaceable mat.(k).(int_of_float (!cur_y)) override_arr then begin
|
|
if x2 - k <= cutoff || k mod 2 = 0 || cls.(k).(int_of_float (!cur_y)) = 0 then
|
|
cls.(k).(int_of_float (!cur_y)) <- origin+1
|
|
else
|
|
();
|
|
if x2 - k > cutoff then
|
|
mat.(k).(int_of_float (!cur_y)) <- '|'
|
|
else
|
|
mat.(k).(int_of_float (!cur_y)) <- 'v'
|
|
end
|
|
else
|
|
()
|
|
done
|
|
else
|
|
for k = x1 downto x2 do
|
|
let cur_y = ref ((!slope) *. float_of_int (k - x1) +. float_of_int y1) in
|
|
if !slope = 0. then cur_y := float_of_int y2 else ();
|
|
if is_rempaceable mat.(k).(int_of_float (!cur_y)) override_arr then begin
|
|
if k - x2 <= cutoff || k mod 2 = 0 || cls.(k).(int_of_float (!cur_y)) = 0 then
|
|
cls.(k).(int_of_float (!cur_y)) <- origin+1
|
|
else
|
|
();
|
|
if k - x2 > cutoff then
|
|
mat.(k).(int_of_float (!cur_y)) <- '|'
|
|
else
|
|
mat.(k).(int_of_float (!cur_y)) <- '^'
|
|
end
|
|
else
|
|
()
|
|
done
|
|
else
|
|
if y1 < y2 then
|
|
for l = y1 to y2 do
|
|
let cur_x = ref (float_of_int x2) in
|
|
if (!slope) <> 1.0 /. 0.0 && (!slope) <> (-. 1.) /. 0. then
|
|
cur_x := ((float_of_int l) +. ((!slope) *. (float_of_int x1) -. (float_of_int y1))) /. (!slope)
|
|
else
|
|
();
|
|
if is_rempaceable mat.(int_of_float (!cur_x)).(l) override_arr then begin
|
|
if y2 - l <= cutoff || l mod 2 = 0 || cls.(int_of_float (!cur_x)).(l) = 0 then
|
|
cls.(int_of_float (!cur_x)).(l) <- origin+1
|
|
else
|
|
();
|
|
if y2 - l > cutoff then
|
|
mat.(int_of_float (!cur_x)).(l) <- '-'
|
|
else
|
|
mat.(int_of_float (!cur_x)).(l) <- '>'
|
|
end
|
|
else
|
|
()
|
|
done
|
|
else
|
|
for l = y1 downto y2 do
|
|
let cur_x = ref (float_of_int x2) in
|
|
if (!slope) <> 1.0 /. 0.0 && (!slope) <> (-. 1.) /. 0. then
|
|
cur_x := ((float_of_int l) +. ((!slope) *. (float_of_int x1) -. (float_of_int y1))) /. (!slope)
|
|
else
|
|
();
|
|
if is_rempaceable mat.(int_of_float (!cur_x)).(l) override_arr then begin
|
|
if l - y2 <= cutoff || l mod 2 = 0 || cls.(int_of_float (!cur_x)).(l) = 0 then
|
|
cls.(int_of_float (!cur_x)).(l) <- origin+1
|
|
else
|
|
();
|
|
if l - y2 > cutoff then
|
|
mat.(int_of_float (!cur_x)).(l) <- '-'
|
|
else
|
|
mat.(int_of_float (!cur_x)).(l) <- '<'
|
|
end
|
|
else
|
|
()
|
|
done;;
|
|
|
|
let display mat cls =
|
|
let colors = [|"\027[40m"; "\027[41m"; "\027[42m"; "\027[43m"; "\027[44m"; "\027[45m"; "\027[46m"; "\027[47m"; "\027[100m"; "\027[101m"; "\027[102m"; "\027[103m"; "\027[104m"; "\027[105m"; "\027[106m"; "\027[107m"|] in
|
|
for i = 0 to (Array.length mat -1) do
|
|
for j = 0 to (Array.length mat.(i) -1) do
|
|
if cls.(i).(j) = 0 then
|
|
print_string colors.(0)
|
|
else
|
|
print_string colors.(max 1 (cls.(i).(j) mod (Array.length colors)));
|
|
if mat.(i).(j) = '&' then
|
|
print_char ' '
|
|
else
|
|
print_char mat.(i).(j)
|
|
done;
|
|
print_string "\027[0m";
|
|
print_char '\n'
|
|
done;;
|
|
|
|
let display_specific mat cls digit =
|
|
let colors = [|"\027[0m"; "\027[41m"; "\027[42m"; "\027[43m"; "\027[44m"; "\027[45m"; "\027[46m"; "\027[47m"; "\027[100m"; "\027[101m"; "\027[102m"; "\027[103m"; "\027[104m"; "\027[105m"; "\027[106m"; "\027[107m"|] in
|
|
for i = 0 to (Array.length mat -1) do
|
|
for j = 0 to (Array.length mat.(i) -1) do
|
|
if cls.(i).(j) <> digit then
|
|
print_string colors.(0)
|
|
else
|
|
print_string colors.(max 1 (cls.(i).(j) mod (Array.length colors)));
|
|
if mat.(i).(j) = '&' then
|
|
print_char ' '
|
|
else
|
|
print_char mat.(i).(j)
|
|
done;
|
|
print_string "\027[0m";
|
|
print_char '\n'
|
|
done;;
|
|
|
|
let extend mat cls i0 j0 dst =
|
|
let ni = Array.length mat in
|
|
let nj = Array.length mat.(0) in
|
|
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;
|
|
if abs i = dst || abs j = dst then
|
|
mat.(i0-i).(j0-j) <- '*'
|
|
else if i <> 0 || j <> 0 then
|
|
mat.(i0-i).(j0-j) <- '&'
|
|
else
|
|
()
|
|
end
|
|
else
|
|
()
|
|
done
|
|
done ;;
|
|
|
|
let identity x = x ;;
|
|
|
|
let extremely_fancy_graph_printing g size wmult mode =
|
|
(* creation of the image *)
|
|
let px = Array.make (size) [||] in
|
|
for i = 0 to (size-1) do
|
|
px.(i) <- Array.make (wmult*size) ' '
|
|
done;
|
|
|
|
(* color matrix *)
|
|
let cls = Array.make (size) [||] in
|
|
for i = 0 to (size-1) do
|
|
cls.(i) <- Array.make (wmult*size) 0
|
|
done;
|
|
|
|
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 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;
|
|
coords.(k) <- (!i, wmult* !j);
|
|
done;
|
|
|
|
(* 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
|
|
done
|
|
done;
|
|
|
|
(* show the image *)
|
|
ignore (Sys.command "clear");
|
|
if mode = "SPECIFIC" then begin
|
|
Printf.printf "Enter the node you want to highlight (type -1 to show all; or -2 to exit)\n";
|
|
ignore (Sys.command "clear");
|
|
let nd = ref (-2) in
|
|
|
|
nd := Scanf.bscanf Scanf.Scanning.stdin "%d\n" identity;
|
|
|
|
if !nd >= 0 && !nd < Array.length g then
|
|
display_specific px cls (!nd+1)
|
|
else if !nd = -1 then
|
|
display px cls
|
|
else
|
|
()
|
|
end
|
|
else
|
|
display px cls ;;
|
|
|
|
|
|
let gr = [|[|3; 5; 7|]; [|0|]; [|1; 7; 8|]; [|2; 6|]; [|0; 1; 3|]; [|6; 7|]; [|0; 1; 2|]; [|8|]; [|0; 7; 6|]|] ;;
|
|
|
|
(*print_mat gr ;;*)
|
|
|
|
extremely_fancy_graph_printing gr 44 4 "EVERYTHING" ;
|
|
|
|
(* ocamlfind ocamlc -linkpkg -package unix pretty_printing.ml *) |