Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (* Tracé d'une courbe *)
- #load "graphics.cma";;
- Graphics.open_graph "";;
- Graphics.moveto 30 30;;
- (* Point *)
- type point = {x:float; y:float};;
- let mk_point x1 y1 = {x=x1 ; y=y1};;
- (* Pixel *)
- type pixel = {xi:int; yi:int};;
- let mk_pixel x1 y1 = {xi=x1 ; yi=y1};;
- (* Fenetre *)
- type fenetre = {largeur:int; hauteur:int};;
- let mk_fenetre l h = {largeur=l ; hauteur=h};;
- (* Vérifier que les valeurs soient positives *)
- (* Echelle *)
- type echelle = {xmin:float; xmax:float; ymin:float; ymax:float};;
- let mk_echelle xmini xmaxi ymini ymaxi =
- {xmin=xmini ; xmax=xmaxi ; ymin=ymini ; ymax=ymaxi};;
- (* Vérifier que xmax>xmin et ymax>ymin *)
- (* Fonction coords_vers_ecran *)
- let coords_vers_ecran pt ech fen =
- mk_pixel (int_of_float (float_of_int (fen.largeur) *. (pt.x-.ech.xmin) /. (ech.xmax-.ech.xmin) ) ) (int_of_float (float_of_int (fen.hauteur) *. (pt.y-.ech.ymin) /. (ech.ymax-.ech.ymin) ) )
- ;;
- (* Fonction trace_segment *)
- let trace_segment p1 p2 =
- Graphics.moveto p1.xi p1.yi;
- Graphics.lineto p2.xi p2.yi
- ;;
- (* Fonction trace_lignes *)
- let rec trace_lignes lpix =
- match lpix with
- | [] -> ()
- | _::[] -> ()
- | h1::h2::t -> trace_segment h1 h2 ;
- trace_lignes t
- ;;
- (* Liste de pixels *)
- let pix1 = mk_pixel 30 30;;
- let pix2 = mk_pixel 90 120;;
- let pix3 = mk_pixel 120 180;;
- let pix4 = mk_pixel 90 180;;
- let pix5 = mk_pixel 120 60;;
- let pix6 = mk_pixel 60 90;;
- let pix7 = mk_pixel 30 180;;
- let pix8 = mk_pixel 180 90;;
- let pix9 = mk_pixel 80 160;;
- let pix10 = mk_pixel 50 50;;
- let listpix = pix1::pix2::pix3::pix4::pix5::pix6::pix7::pix8::pix9::pix10::[];;
- (* Fonction f *)
- let f x = x*.x +. 2.*.x -. 3.
- (* Fonction discr *)
- let rec discr f xmin xmax pas =
- if xmin > xmax
- then
- []
- else
- (mk_point (xmin) (f xmin) ) :: discr f (xmin+.pas) xmax pas
- ;;
- (* Fonction discr_nb *)
- let discr_nb f xmin xmax nb =
- discr f xmin xmax ((xmax -. xmin) /. nb)
- ;;
- (* Fonction lcoords_vers_ecran *)
- let rec lcoords_vers_ecran l ech fen =
- match l with
- | [] -> []
- | h::t -> (coords_vers_ecran h ech fen) :: (lcoords_vers_ecran t ech fen)
- ;;
- (* Fonction courbe *)
- let courbe f ech fen nb =
- lcoords_vers_ecran (discr_nb f ech.xmin ech.xmax nb) ech fen
- ;;
- (* Fonction trace_courbe *)
- let trace_courbe f ech fen nb =
- trace_lignes (courbe f ech fen nb)
- ;;
- (* Calcul de la dérivée en un point *)
- let df f x h = (f(x+.h) -. f(x-.h)) /. (2.*. h);;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement