- open "graphics";;
(****** LES DONNEES ******)
let nb_pts = 20;;
let les_pts = make_vect nb_pts (0, 0);;
let tab = let t = make_vect nb_pts 0 in
for i = 0 to nb_pts -1 do t.(i)<-i ;done; t;;
(****** LES FONCTIONS ******)
let rond (a, b) = fill_circle a b 2;;
let pointe_en (a, b) = moveto a b;;
let va_en (a, b) = lineto a b;;
let trace_poly tab =
for i = 0 to nb_pts - 2 do
pointe_en les_pts.(tab.(i));
va_en les_pts.(tab.(i + 1));
rond les_pts.(tab.(i));
done;
va_en les_pts.(tab.(0));rond les_pts.(tab.(0));;
let suite_de_points a =
let attend =
wait_next_event [Button_down] in
let ma = attend.mouse_x in let mb = attend.mouse_y in
(ma, mb);;
let init_points_souris nb =
for i = 0 to (nb - 1) do
let coord = suite_de_points 0 in
les_pts.(i) <- coord;
rond coord;
moveto (fst coord) (snd coord); draw_string (string_of_int i);
done;;
let diff (a, b) (c, d) = (a - c, b - d);;
let det (a, b) (c, d) = a *. d -. c *. b;;
let point_reel (a,b) = (float_of_int a, float_of_int b) ;;
let intersection a b c d =
let v1= point_reel (diff b a)
and v2 = point_reel (diff c d)
and v3 = point_reel (diff c a) in
let d = det v1 v2
and dt = det v3 v2
and du = det v1 v3 in
let u = du/.d
and t = dt/.d in
(t<=1.)&&(t>=0.)&&(u<=1.)&&(u>=0.);;
let echange t i j =
let e = t.(i) in
t.(i) <- t.(j);
t.(j) <- e;;
let inverse t i j =
for k = 0 to (j-i)/2 do
echange t (i+k) (j-k) ;
done;
;;
let simplifie_premiere_arete tab =
let n = vect_length tab in
let ok = ref true in
while !ok do
ok := false;
let i = ref 2 in
while (!i) <= (n - 2) && (not !ok )do
if intersection les_pts.(tab.(0)) les_pts.(tab.(1)) les_pts.(tab.(!i)) les_pts.(tab.(!i + 1));
then (ok := true; inverse tab 1 !i);
incr i;
done;done;;
let permute t =
let n =vect_length t - 1 in
let a0=t.(0) in
for i=1 to n do
t.(i-1) <- t.(i); done;
t.(n) <- a0;t ;;
let decroise tab = let n = vect_length tab in
for i = 0 to n-1 do
simplifie_premiere_arete tab;
permute tab;
done;;
(********** LES ACTIONS *************)
open_graph "";
init_points_souris nb_pts;
trace_poly tab ;
wait_next_event [Key_pressed];
decroise tab;
clear_graph ();
trace_poly (tab);;
On fait des trucs géniaux avec Caml 