#load "graphics.cma";; Graphics.open_graph "";; Graphics.set_color 0;; (* type point *) type pt = float * float;; let (creerPt : float->float->pt) = function x -> function y -> (x,y);; let (absPt : pt->float) = function (x,y) -> x;; let (ordPt : pt->float) = function (x,y) -> y;; (* type carré *) type carre = pt * float;; let (creerCarre : pt->float->carre) = function p -> function c -> (p,c);; let (origCarre : carre->pt) = function (p,c) -> p;; let (coteCarre : carre->float) = function (p,c) -> c;; (* arrondi : pour passer d'un flottant au plus proche entier *) let round x = if x > 0. then int_of_float (x +. 0.5) else int_of_float (x -. 0.5);; (* dessine un carré *) let (traceCarre : carre->unit) = function car -> let ocar = origCarre car and c = coteCarre car in let ax = round (absPt ocar) and ay = round (ordPt ocar) in Graphics.moveto ax ay ; Graphics.lineto (ax + round c) ay ; Graphics.lineto (ax + round c) (ay + round c) ; Graphics.lineto ax (ay + round c) ; Graphics.lineto ax ay;; (* renvoie les quatres sommets (de type point) d'un carré *) let (somCarre : carre->pt*pt*pt*pt) = function car -> let ocar = origCarre car and co = coteCarre car in let ox = absPt ocar and oy = ordPt ocar in let a = creerPt ox oy in let b = creerPt (ox +. co) oy in let c = creerPt (ox +. co) (oy +. co) in let d = creerPt ox (oy +. co) in (a,b,c,d);; (* dessine un quadrilatère donné par ses 4 sommets *) let (dessCarre : pt*pt*pt*pt->unit) = function (a,b,c,d) -> Graphics.moveto (round (absPt a)) (round (ordPt a)) ; Graphics.lineto (round (absPt b)) (round (ordPt b)) ; Graphics.lineto (round (absPt c)) (round (ordPt c)) ; Graphics.lineto (round (absPt d)) (round (ordPt d)) ; Graphics.lineto (round (absPt a)) (round (ordPt a));; (* calcul le milieu de deux points *) let (milieu : pt->pt->pt) = function a -> function b -> creerPt ((absPt a +. absPt b)/.2.) ((ordPt a +. ordPt b)/.2.);; (* à vous de jouer... *)