This commit is contained in:
Alexandre 2024-05-19 12:37:54 +02:00
parent f4307650bb
commit f88a54968e
4 changed files with 60 additions and 16 deletions

BIN
a.out

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1,3 +1,5 @@
(*open Spectrum*)
let rec print_mat m = let rec print_mat m =
for i = 0 to (Array.length m)-1 do for i = 0 to (Array.length m)-1 do
print_char ' '; print_char ' ';
@ -42,7 +44,7 @@ let is_rempaceable c arr =
with with
| ReturnBool b -> b ;; | ReturnBool b -> b ;;
let draw_line_bresenham mat x1 y1 x2 y2 cutoff = let draw_line_bresenham mat cls origin x1 y1 x2 y2 cutoff =
let slope = ref 0. in let slope = ref 0. in
let override_arr = [|' '; '.'; '|'; '-'|] in let override_arr = [|' '; '.'; '|'; '-'|] in
if x2 <> x1 || y2 <> y1 then if x2 <> x1 || y2 <> y1 then
@ -55,8 +57,16 @@ let draw_line_bresenham mat x1 y1 x2 y2 cutoff =
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 is_rempaceable mat.(k).(int_of_float (!cur_y)) override_arr then if is_rempaceable mat.(k).(int_of_float (!cur_y)) override_arr then begin
if x2 - k > cutoff then mat.(k).(int_of_float (!cur_y)) <- '|' else mat.(k).(int_of_float (!cur_y)) <- 'v' 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 else
() ()
done done
@ -64,8 +74,16 @@ let draw_line_bresenham mat x1 y1 x2 y2 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 is_rempaceable mat.(k).(int_of_float (!cur_y)) override_arr then if is_rempaceable mat.(k).(int_of_float (!cur_y)) override_arr then begin
if k - x2 > cutoff then mat.(k).(int_of_float (!cur_y)) <- '|' else mat.(k).(int_of_float (!cur_y)) <- '^' 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 else
() ()
done done
@ -77,8 +95,16 @@ let draw_line_bresenham mat x1 y1 x2 y2 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 is_rempaceable mat.(int_of_float (!cur_x)).(l) override_arr then if is_rempaceable mat.(int_of_float (!cur_x)).(l) override_arr then begin
if y2 - l > cutoff then mat.(int_of_float (!cur_x)).(l) <- '-' else mat.(int_of_float (!cur_x)).(l) <- '>' 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 else
() ()
done done
@ -89,15 +115,25 @@ let draw_line_bresenham mat x1 y1 x2 y2 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 is_rempaceable mat.(int_of_float (!cur_x)).(l) override_arr then if is_rempaceable mat.(int_of_float (!cur_x)).(l) override_arr then begin
if l - y2 > cutoff then mat.(int_of_float (!cur_x)).(l) <- '-' else mat.(int_of_float (!cur_x)).(l) <- '<' 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 else
() ()
done;; done;;
let display mat = let display mat cls =
let colors = [|"\027[30m"; "\027[31m"; "\027[32m"; "\027[33m"; "\027[34m"; "\027[35m"; "\027[36m"; "\027[37m"|] in
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
print_string colors.(cls.(i).(j) mod (Array.length colors));
if mat.(i).(j) = '&' then if mat.(i).(j) = '&' then
print_char ' ' print_char ' '
else else
@ -106,18 +142,20 @@ let display mat =
print_char '\n' print_char '\n'
done;; done;;
let extend mat i0 j0 dst = let extend mat cls 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
for i = -dst to dst do for i = -dst to dst do
for j = -dst to dst do for j = -dst to dst do
if i0 - i >= 0 && j0 - j >= 0 && i0 - i < ni && j0 - j < nj then 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 if abs i = dst || abs j = dst then
mat.(i0-i).(j0-j) <- '*' mat.(i0-i).(j0-j) <- '*'
else if i <> 0 || j <> 0 then else if i <> 0 || j <> 0 then
mat.(i0-i).(j0-j) <- '&' mat.(i0-i).(j0-j) <- '&'
else else
() ()
end
else else
() ()
done done
@ -130,6 +168,12 @@ let extremely_fancy_graph_printing g size =
px.(i) <- Array.make (3*size) ' ' px.(i) <- Array.make (3*size) ' '
done; done;
(* color matrix *)
let cls = Array.make (size) [||] in
for i = 0 to (size-1) do
cls.(i) <- Array.make (3*size) 0
done;
let coords = Array.make size (0, 0) in let coords = Array.make size (0, 0) in
(* placing the points on the trig circle *) (* placing the points on the trig circle *)
@ -142,22 +186,22 @@ let extremely_fancy_graph_printing g size =
if !i >= size then i := size-1 else (); if !i >= size then i := size-1 else ();
if !j >= size then j := size-1 else (); if !j >= size then j := size-1 else ();
px.(!i).(3* !j) <- Char.chr (k + 48); px.(!i).(3* !j) <- Char.chr (k + 48);
extend px !i (3* !j) 3; extend px cls !i (3* !j) 3;
coords.(k) <- (!i, 3* !j); coords.(k) <- (!i, 3* !j);
done; done;
(* draw the connections *) (* draw the connections *)
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
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 cls i (fst coords.(i)) (snd coords.(i)) (fst coords.(g.(i).(j))) (snd coords.(g.(i).(j))) 7
done done
done; done;
(* show the image *) (* show the image *)
display px ;; display px cls ;;
let gr = [|[|3; 4|]; [|0; 6; 7|]; [|3; 5|]; [|0; 7|]; [|1; 2|]; [|4|]; [|5; 7|]; [|5|]|] ;; let gr = [|[|1; 2|]; [|2; 3|]; [|0; 1; 3|]; [|0; 1|]|] ;;
(*print_mat gr ;;*) (*print_mat gr ;;*)