(*********************) (* Correction du TP5 *) (*********************) (**** Question 1 ****) #open "graphics";; open_graph "800x600+20+20";; for i=0 to 255 do for j=0 to 255 do set_color (rgb i 0 j); plot i j; done; done;; (**** Question 2 ****) let sirp n = clear_graph(); set_color black; plot 10 550; for i=1 to n do for j=0 to i do let (x,y) = (j+10,550-i) in if point_color x (y+1) <> point_color (x-1) (y+1) then plot x y; done; done;; sirp 512;; (**** Question 3 ****) let coord (x0,x1) width x = int_of_float (0.5 +. (float_of_int (width-1)) *. (x-.x0) /. (x1-.x0));; coord (-1.0,1.0) 800 (-1.0);; coord (-1.0,1.0) 800 1.0;; coord (-1.0,1.0) 800 0.5;; let draw_axes (xmin,ymin,xmax,ymax) = let mto x y = moveto (coord (xmin,xmax) (size_x()) x) (coord (ymin,ymax) (size_y()) y) and lto x y = lineto (coord (xmin,xmax) (size_x()) x) (coord (ymin,ymax) (size_y()) y) in set_color black; mto 0.0 ymin; lto 0.0 ymax; mto xmin 0.0; lto xmax 0.0;; draw_axes (-2.0,-1.0,1.0,2.0);; let step (xmin,xmax) n i = xmin +. (xmax -. xmin) *. (float_of_int i) /. (float_of_int n);; step (-1.0,1.0) 200 50;; let trace (xmin,ymin,xmax,ymax) f n = draw_axes (xmin,ymin,xmax,ymax); let mto x y = moveto (coord (xmin,xmax) (size_x()) x) (coord (ymin,ymax) (size_y()) y) and lto x y = lineto (coord (xmin,xmax) (size_x()) x) (coord (ymin,ymax) (size_y()) y) in mto xmin (f xmin); for i=1 to n do let x = step (xmin,xmax) n i in lto x (f x); done;; clear_graph();; trace (-6.0,-1.5,6.0,1.5) sin 500;; (**** Question 4 ****) let joli () = let continue = ref true in while !continue do let st = wait_next_event [Mouse_motion; Key_pressed] in if st.keypressed then continue:=false; fill_circle st.mouse_x st.mouse_y 5; done;; let joli2 () = let x = ref 0 and y = ref 0 and continue = ref true in while !continue do let st = wait_next_event [Mouse_motion; Key_pressed] in if st.keypressed then continue:=false; set_color (rgb (128 + st.mouse_x - !x) 0 (128 + st.mouse_y - !y)); fill_circle st.mouse_x st.mouse_y 5; x := st.mouse_x; y := st.mouse_y; done;; (**** Question 5 ****) type arbre = | vide | nd of int*arbre*arbre;; let rec search x = function | vide -> false | nd(y,g,d) -> if x=y then true else if x nd(x,vide,vide) | nd(y,g,d) -> if x () | nd(x,g,d) -> affiche g; print_int x; print_string ","; affiche d;; let rec arbre_of_list = function | [] -> vide | t::q -> insert t (arbre_of_list q);; let trie_list l = affiche (arbre_of_list l);; trie_list [2;5;3;2;5;3;2;1;0;7];; (**** Question 6 ****) let draw_node x y i = set_color white; fill_circle x y 16; set_color black; moveto (x-8) (y-12); draw_string (string_of_int i); draw_circle x y 16;; let dessine_arbre= let rec aux x y l = function | vide -> () | nd(i,g,d) -> set_color black; moveto (x-l) (y-40); lineto x y; lineto (x+l) (y-40); draw_node x y i; aux (x-l) (y-20) (l/2) g; aux (x+l) (y-20) (l/2) d; in aux 400 550 200;; clear_graph();; dessine_arbre (arbre_of_list [1;4;2;3;6;1;7;8]);; (**** Question 7 ****) let dead = green;; let baby = blue;; let age x = if x=dead || x=white then white else if x >= 240*65536 then magenta else x + (rgb 16 0 0);; let xmin = ref 0;; let ymin = ref 0;; let xmax = ref 0;; let ymax = ref 0;; let cellule x y = let c = point_color (5*x) (5*y) in c <> white && c <> baby;; let point x y c = set_color c; fill_rect (5*x) (5*y) 5 5;; let pass1 () = for x = !xmin-1 to !xmax+1 do for y = !ymin-1 to !ymax+1 do let nb_voisins = ref 0 in if cellule (x-1) (y-1) then incr nb_voisins; if cellule (x+1) (y-1) then incr nb_voisins; if cellule (x-1) (y+1) then incr nb_voisins; if cellule (x+1) (y+1) then incr nb_voisins; if cellule (x) (y+1) then incr nb_voisins; if cellule (x+1) (y) then incr nb_voisins; if cellule (x-1) (y) then incr nb_voisins; if cellule (x) (y-1) then incr nb_voisins; if !nb_voisins = 3 && not(cellule x y) then ( point x y baby; if x < !xmin then xmin:=x; if y < !ymin then ymin:=y; if x > !xmax then xmax:=x; if y > !ymax then ymax:=y; ); if (!nb_voisins < 2 || !nb_voisins > 3) && cellule x y then ( point x y dead; ); done; done;; let pass2 () = for x = !xmin-1 to !xmax+1 do for y = !ymin-1 to !ymax+1 do point x y (age (point_color (5*x) (5*y))); done; done;; let init() = clear_graph(); xmin := 100000; ymin := 100000; xmax := 0; ymax := 0; let continue = ref true in while !continue do let st = wait_next_event [Button_down; Key_pressed] in if st.keypressed then continue:=false else ( let (x,y) = (st.mouse_x/5,st.mouse_y/5) and c = point_color st.mouse_x st.mouse_y in if c=baby then point x y white else point x y baby; if x > !xmax then xmax:= x; if y > !ymax then ymax:= y; if x < !xmin then xmin:= x; if y < !ymin then ymin:= y; ) done;; let main() = init(); let cont = ref true and pass = ref true in while !cont do if !pass then pass2() else pass1(); pass := not (!pass); let st = wait_next_event [Button_down; Key_pressed] in if st.keypressed then cont:=false; done;; let rapide() = clear_graph(); for i=1 to 300 do point (30+random__int 30) (30+random__int 30) baby done; xmin := 30; ymin := 30; xmax := 60; ymax := 60; while not key_pressed() do pass2(); pass1(); done;; rapide();;