Reworked structure
This commit is contained in:
parent
96aabd69bf
commit
f4307650bb
Binary file not shown.
Binary file not shown.
|
@ -20,6 +20,8 @@ let rec print_mat m =
|
||||||
print_char '\n';
|
print_char '\n';
|
||||||
done ;;
|
done ;;
|
||||||
|
|
||||||
|
exception ReturnBool of bool ;;
|
||||||
|
|
||||||
let pi = 3.14159265358979343 ;;
|
let pi = 3.14159265358979343 ;;
|
||||||
|
|
||||||
let abs x =
|
let abs x =
|
||||||
|
@ -28,19 +30,33 @@ let abs x =
|
||||||
let absf x =
|
let absf x =
|
||||||
if x >= 0. then x else -1. *. x ;;
|
if x >= 0. then x else -1. *. x ;;
|
||||||
|
|
||||||
let draw_line_bresenham mat x1 y1 x2 y2 neutral lnc cutoff =
|
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 x1 y1 x2 y2 cutoff =
|
||||||
let slope = ref 0. in
|
let slope = ref 0. in
|
||||||
|
let override_arr = [|' '; '.'; '|'; '-'|] in
|
||||||
if x2 <> x1 || y2 <> y1 then
|
if x2 <> x1 || y2 <> y1 then
|
||||||
slope := (float_of_int (y2 - y1) /. float_of_int (x2 - x1))
|
slope := (float_of_int (y2 - y1) /. float_of_int (x2 - x1))
|
||||||
else
|
else
|
||||||
();
|
();
|
||||||
|
(*Printf.printf "(%f)\n" !slope;*)
|
||||||
if absf (!slope) < 1. then
|
if absf (!slope) < 1. then
|
||||||
if x1 < x2 then
|
if x1 < x2 then
|
||||||
for k = x1 to x2 do
|
for k = x1 to x2 do
|
||||||
let cur_y = ref ((!slope) *. float_of_int (k - x1) +. float_of_int y1) in
|
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 !slope = 0. then cur_y := float_of_int y2 else ();
|
||||||
if mat.(k).(int_of_float (!cur_y)) = neutral || mat.(k).(int_of_float (!cur_y)) = (-2) then
|
if is_rempaceable mat.(k).(int_of_float (!cur_y)) override_arr then
|
||||||
if x2 - k > cutoff then mat.(k).(int_of_float (!cur_y)) <- (-2) else mat.(k).(int_of_float (!cur_y)) <- (-3)
|
if x2 - k > cutoff then mat.(k).(int_of_float (!cur_y)) <- '|' else mat.(k).(int_of_float (!cur_y)) <- 'v'
|
||||||
else
|
else
|
||||||
()
|
()
|
||||||
done
|
done
|
||||||
|
@ -48,8 +64,8 @@ let draw_line_bresenham mat x1 y1 x2 y2 neutral lnc cutoff =
|
||||||
for k = x1 downto x2 do
|
for k = x1 downto x2 do
|
||||||
let cur_y = ref ((!slope) *. float_of_int (k - x1) +. float_of_int y1) in
|
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 !slope = 0. then cur_y := float_of_int y2 else ();
|
||||||
if mat.(k).(int_of_float (!cur_y)) = neutral then
|
if is_rempaceable mat.(k).(int_of_float (!cur_y)) override_arr then
|
||||||
if k - x2 > cutoff then mat.(k).(int_of_float (!cur_y)) <- (-2) else mat.(k).(int_of_float (!cur_y)) <- (-3)
|
if k - x2 > cutoff then mat.(k).(int_of_float (!cur_y)) <- '|' else mat.(k).(int_of_float (!cur_y)) <- '^'
|
||||||
else
|
else
|
||||||
()
|
()
|
||||||
done
|
done
|
||||||
|
@ -61,8 +77,8 @@ let draw_line_bresenham mat x1 y1 x2 y2 neutral lnc cutoff =
|
||||||
cur_x := ((float_of_int l) +. ((!slope) *. (float_of_int x1) -. (float_of_int y1))) /. (!slope)
|
cur_x := ((float_of_int l) +. ((!slope) *. (float_of_int x1) -. (float_of_int y1))) /. (!slope)
|
||||||
else
|
else
|
||||||
();
|
();
|
||||||
if mat.(int_of_float (!cur_x)).(l) = neutral || mat.(int_of_float (!cur_x)).(l) = (-2) then
|
if is_rempaceable mat.(int_of_float (!cur_x)).(l) override_arr then
|
||||||
if y2 - l > cutoff then mat.(int_of_float (!cur_x)).(l) <- (-2) else mat.(int_of_float (!cur_x)).(l) <- (-3)
|
if y2 - l > cutoff then mat.(int_of_float (!cur_x)).(l) <- '-' else mat.(int_of_float (!cur_x)).(l) <- '>'
|
||||||
else
|
else
|
||||||
()
|
()
|
||||||
done
|
done
|
||||||
|
@ -73,75 +89,45 @@ let draw_line_bresenham mat x1 y1 x2 y2 neutral lnc cutoff =
|
||||||
cur_x := ((float_of_int l) +. ((!slope) *. (float_of_int x1) -. (float_of_int y1))) /. (!slope)
|
cur_x := ((float_of_int l) +. ((!slope) *. (float_of_int x1) -. (float_of_int y1))) /. (!slope)
|
||||||
else
|
else
|
||||||
();
|
();
|
||||||
if mat.(int_of_float (!cur_x)).(l) = neutral || mat.(int_of_float (!cur_x)).(l) = (-2) then
|
if is_rempaceable mat.(int_of_float (!cur_x)).(l) override_arr then
|
||||||
if l - y2 > cutoff then mat.(int_of_float (!cur_x)).(l) <- (-2) else mat.(int_of_float (!cur_x)).(l) <- (-3)
|
if l - y2 > cutoff then mat.(int_of_float (!cur_x)).(l) <- '-' else mat.(int_of_float (!cur_x)).(l) <- '<'
|
||||||
else
|
else
|
||||||
()
|
()
|
||||||
done;;
|
done;;
|
||||||
|
|
||||||
let display mat neutral lnc lnf =
|
let display mat =
|
||||||
for i = 0 to (Array.length mat -1) do
|
for i = 0 to (Array.length mat -1) do
|
||||||
for j = 0 to (Array.length mat.(i) -1) do
|
for j = 0 to (Array.length mat.(i) -1) do
|
||||||
if mat.(i).(j) = (-5) then
|
if mat.(i).(j) = '&' then
|
||||||
Printf.printf "#"
|
print_char ' '
|
||||||
else if mat.(i).(j) <> neutral && mat.(i).(j) <> lnc && mat.(i).(j) <> lnf then
|
|
||||||
Printf.printf "%d" mat.(i).(j)
|
|
||||||
else if mat.(i).(j) = lnc then
|
|
||||||
Printf.printf "."
|
|
||||||
else if mat.(i).(j) = lnf then
|
|
||||||
Printf.printf "X"
|
|
||||||
else
|
else
|
||||||
Printf.printf " "
|
print_char mat.(i).(j)
|
||||||
done;
|
done;
|
||||||
print_char '\n'
|
print_char '\n'
|
||||||
done;;
|
done;;
|
||||||
|
|
||||||
let extend mat i j =
|
let extend mat i0 j0 dst =
|
||||||
let ni = Array.length mat in
|
let ni = Array.length mat in
|
||||||
let nj = Array.length mat.(0) in
|
let nj = Array.length mat.(0) in
|
||||||
let s = (-5) in
|
for i = -dst to dst do
|
||||||
if i+1 >= 0 && i+1 < ni then begin
|
for j = -dst to dst do
|
||||||
mat.(i+1).(j) <- s;
|
if i0 - i >= 0 && j0 - j >= 0 && i0 - i < ni && j0 - j < nj then
|
||||||
if j+1 >= 0 && j+1 < nj then
|
if abs i = dst || abs j = dst then
|
||||||
mat.(i+1).(j+1) <- s
|
mat.(i0-i).(j0-j) <- '*'
|
||||||
else
|
else if i <> 0 || j <> 0 then
|
||||||
();
|
mat.(i0-i).(j0-j) <- '&'
|
||||||
if j-1 >= 0 && j-1 < nj then
|
|
||||||
mat.(i+1).(j-1) <- s
|
|
||||||
else
|
else
|
||||||
()
|
()
|
||||||
end
|
|
||||||
else
|
|
||||||
();
|
|
||||||
|
|
||||||
if i-1 >= 0 && i-1 < ni then begin
|
|
||||||
mat.(i-1).(j) <- s;
|
|
||||||
if j+1 >= 0 && j+1 < nj then
|
|
||||||
mat.(i-1).(j+1) <- s
|
|
||||||
else
|
|
||||||
();
|
|
||||||
if j-1 >= 0 && j-1 < nj then
|
|
||||||
mat.(i-1).(j-1) <- s
|
|
||||||
else
|
else
|
||||||
()
|
()
|
||||||
end
|
done
|
||||||
else
|
done ;;
|
||||||
();
|
|
||||||
|
|
||||||
if j+1 >= 0 && j+1 < nj then
|
|
||||||
mat.(i).(j+1) <- s
|
|
||||||
else
|
|
||||||
();
|
|
||||||
|
|
||||||
if j-1 >= 0 && j-1 < nj then
|
|
||||||
mat.(i).(j-1) <- s
|
|
||||||
else
|
|
||||||
() ;;
|
|
||||||
|
|
||||||
let extremely_fancy_graph_printing g size =
|
let extremely_fancy_graph_printing g size =
|
||||||
|
(* creation of the image *)
|
||||||
let px = Array.make (size) [||] in
|
let px = Array.make (size) [||] in
|
||||||
for i = 0 to (size-1) do
|
for i = 0 to (size-1) do
|
||||||
px.(i) <- Array.make (3*size) (-1)
|
px.(i) <- Array.make (3*size) ' '
|
||||||
done;
|
done;
|
||||||
|
|
||||||
let coords = Array.make size (0, 0) in
|
let coords = Array.make size (0, 0) in
|
||||||
|
@ -149,28 +135,30 @@ let extremely_fancy_graph_printing g size =
|
||||||
(* placing the points on the trig circle *)
|
(* placing the points on the trig circle *)
|
||||||
for k = 0 to Array.length g - 1 do
|
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)) in
|
||||||
let i = int_of_float ((float_of_int size) /. 2.) + int_of_float ((float_of_int size) /. 2.05 *. cos theta) 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 = int_of_float ((float_of_int size) /. 2.) + int_of_float ((float_of_int size) /. 2.05 *. sin theta) in
|
let j = ref (int_of_float ((float_of_int size) /. 2.) + int_of_float ((float_of_int size) /. 2. *. sin theta)) in
|
||||||
px.(i).(3*j) <- k;
|
if !i < 0 then i := 0 else ();
|
||||||
extend px i (3*j);
|
if !j < 0 then j := 0 else ();
|
||||||
coords.(k) <- (i, 3*j);
|
if !i >= size then i := size-1 else ();
|
||||||
|
if !j >= size then j := size-1 else ();
|
||||||
|
px.(!i).(3* !j) <- Char.chr (k + 48);
|
||||||
|
extend px !i (3* !j) 3;
|
||||||
|
coords.(k) <- (!i, 3* !j);
|
||||||
done;
|
done;
|
||||||
(*
|
|
||||||
for i = 0 to Array.length g - 2 do
|
(* draw the connections *)
|
||||||
draw_line_bresenham px (fst coords.(i)) (snd coords.(i)) (fst coords.(i+1)) (snd coords.(i+1)) (-1)
|
|
||||||
done;
|
|
||||||
*)
|
|
||||||
for i = 0 to Array.length g -1 do
|
for i = 0 to Array.length g -1 do
|
||||||
for j = 0 to Array.length g.(i) -1 do
|
for j = 0 to Array.length g.(i) -1 do
|
||||||
(*Printf.printf "[%d %d]\n" i g.(i).(j);*)
|
draw_line_bresenham px (fst coords.(i)) (snd coords.(i)) (fst coords.(g.(i).(j))) (snd coords.(g.(i).(j))) 7
|
||||||
draw_line_bresenham px (fst coords.(i)) (snd coords.(i)) (fst coords.(g.(i).(j))) (snd coords.(g.(i).(j))) (-1) (-3) 6
|
|
||||||
done
|
done
|
||||||
done;
|
done;
|
||||||
display px (-1) (-2) (-3) ;;
|
|
||||||
|
(* show the image *)
|
||||||
|
display px ;;
|
||||||
|
|
||||||
|
|
||||||
let gr = [|[|3|]; [|4|]; [|0; 3; 4|]; [|0; 2|]; [|1; 2|]; [|0; 4|]|] ;;
|
let gr = [|[|3; 4|]; [|0; 6; 7|]; [|3; 5|]; [|0; 7|]; [|1; 2|]; [|4|]; [|5; 7|]; [|5|]|] ;;
|
||||||
|
|
||||||
(*print_mat gr ;;*)
|
(*print_mat gr ;;*)
|
||||||
|
|
||||||
extremely_fancy_graph_printing gr 40 ;
|
extremely_fancy_graph_printing gr 44 ;
|
Loading…
Reference in New Issue