Random and full graph generators
This commit is contained in:
parent
8ed1b5a320
commit
648b62ea4b
BIN
graphs.cmi
BIN
graphs.cmi
Binary file not shown.
BIN
graphs.cmo
BIN
graphs.cmo
Binary file not shown.
63
graphs.ml
63
graphs.ml
|
@ -63,22 +63,27 @@ let improved_pretty_printing g wd ht r =
|
|||
coords.(k) <- (!i, !j)
|
||||
done ;
|
||||
|
||||
set_line_width 4 ;
|
||||
set_color black ;
|
||||
for k = 0 to n-1 do
|
||||
set_color colors.(k) ;
|
||||
for l = 0 to (Array.length g.(k))-1 do
|
||||
draw_poly_line [|coords.(k); coords.(g.(k).(l))|];
|
||||
if g.(k).(l) <> (-1) then begin
|
||||
draw_poly_line [|coords.(k); coords.(g.(k).(l))|]
|
||||
end
|
||||
done
|
||||
done;
|
||||
|
||||
set_line_width 22 ;
|
||||
set_line_width 12 ;
|
||||
for k = 0 to n-1 do
|
||||
set_color colors.(k) ;
|
||||
for l = 0 to (Array.length g.(k))-1 do
|
||||
let slope = Float.atan2 (float_of_int (snd coords.(g.(k).(l)) - snd coords.(k))) (float_of_int (fst coords.(g.(k).(l)) - fst coords.(k))) in
|
||||
if g.(k).(l) <> (-1) then begin
|
||||
let slope = Float.atan2 (float_of_int (snd coords.(g.(k).(l)) - snd coords.(k))) (float_of_int (fst coords.(g.(k).(l)) - fst coords.(k))) in
|
||||
|
||||
let nexi = int_of_float (float_of_int (fst coords.(k)) +. (float_of_int r) *. 1.5 *. cos slope) in
|
||||
let nexj = int_of_float (float_of_int (snd coords.(k)) +. (float_of_int r) *. 1.5 *. sin slope) in
|
||||
draw_poly_line [|coords.(k); (nexi, nexj)|]
|
||||
let nexi = int_of_float (float_of_int (fst coords.(k)) +. (float_of_int r) *. 1.75 *. cos slope) in
|
||||
let nexj = int_of_float (float_of_int (snd coords.(k)) +. (float_of_int r) *. 1.75 *. sin slope) in
|
||||
draw_poly_line [|coords.(k); (nexi, nexj)|]
|
||||
end
|
||||
done
|
||||
done;
|
||||
|
||||
|
@ -96,11 +101,51 @@ let improved_pretty_printing g wd ht r =
|
|||
|
||||
(* ----------------------- Tests --------------------------- *)
|
||||
|
||||
Random.self_init ;;
|
||||
Random.self_init ;;
|
||||
|
||||
open_graph " 1200x800" ;;
|
||||
set_window_title "Graphs" ;;
|
||||
|
||||
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 generate_full_graph k =
|
||||
let res = Array.make k [||] in
|
||||
for i = 0 to k-1 do
|
||||
res.(i) <- Array.make (k-1) 0
|
||||
done;
|
||||
|
||||
improved_pretty_printing gr 1200 800 50 ;;
|
||||
for x = 0 to k-1 do
|
||||
for y = 0 to k-1 do
|
||||
if x < y then
|
||||
res.(x).(y-1) <- y
|
||||
else if x > y then
|
||||
res.(x).(y) <- y
|
||||
done
|
||||
done;
|
||||
res ;;
|
||||
|
||||
let generate_random_graph k freq =
|
||||
let res = Array.make k [||] in
|
||||
for i = 0 to k-1 do
|
||||
res.(i) <- Array.make (k-1) (-1)
|
||||
done;
|
||||
|
||||
for x = 0 to k-1 do
|
||||
for y = 0 to k-1 do
|
||||
if (Random.int 100) < freq then
|
||||
if x < y then
|
||||
res.(x).(y-1) <- y
|
||||
else if x > y then
|
||||
res.(x).(y) <- y
|
||||
done
|
||||
done;
|
||||
res ;;
|
||||
|
||||
let gr = [|[|3; 5; 7|]; [|0|]; [|1; 7; 8|]; [|2; 6; 9; 10|]; [|0; 1; 3|]; [|6; 7|]; [|0; 1; 2|]; [|8|]; [|0; 7; 6|]; [|10; 11|]; [|3; 5; 7|]; [|0; 9|]|] ;;
|
||||
let fulg = generate_full_graph 16 ;;
|
||||
let rang = generate_random_graph 9 50 ;;
|
||||
|
||||
(*improved_pretty_printing gr 1200 800 50*) ;;
|
||||
(*improved_pretty_printing fulg 1200 800 25 ;;*)
|
||||
improved_pretty_printing rang 1200 800 45 ;;
|
||||
|
||||
close_graph () ;;
|
Loading…
Reference in New Issue