Vers Bibliothéque Graphique (Présentation)
#open "graphics";;
open_graph "highest";;
clear_graph();;
type p_ecran = {X:int;Y:int};;
type p_repere = {x:float;y:float};;
type repere = {O:p_ecran;I:int;J:int};;
(* conversion point repére/ecran *)
let rep_to_ecr pt_rep rep ={X=(rep.O.X)+( int_of_float((pt_rep.x)*.float_of_int(rep.I)) );
Y=(rep.O.Y)+( int_of_float((pt_rep.y)*.float_of_int(rep.J)) )};;
let move_ecr pt_ecr = moveto (pt_ecr.X) (pt_ecr.Y) ;;
let line_ecr pt_ecr = lineto (pt_ecr.X) (pt_ecr.Y) ;;
let segment_ecr pt_ecr1 pt_ecr2 =moveto (pt_ecr1.X) (pt_ecr1.Y);
lineto (pt_ecr2.X) (pt_ecr2.Y);;
let point_ecr pt_ecr = segment_ecr {X=((pt_ecr.X)-2) ; Y=pt_ecr.Y} {X=((pt_ecr.X)+2) ; Y=(pt_ecr.Y)} ;
segment_ecr {X=(pt_ecr.X) ; Y=((pt_ecr.Y)+2)} {X=(pt_ecr.X) ; Y=((pt_ecr.Y)-2)} ;;
let move_rep pt_rep rep = move_ecr (rep_to_ecr pt_rep rep) ;;
let line_rep pt_rep rep = line_ecr (rep_to_ecr pt_rep rep) ;;
let point_rep pt_rep rep = point_ecr (rep_to_ecr pt_rep rep) ;;
let segment_rep pt_rep1 pt_rep2 rep =
move_rep pt_rep1 rep;
line_rep pt_rep2 rep;;
let fenetre xmin xmax ymin ymax=
let II=int_of_float((float_of_int(size_x()))/.(xmax-.xmin))
and JJ=int_of_float((float_of_int(size_y()))/.(ymax-.ymin)) in
{O={X=int_of_float((-.xmin)*.float_of_int(II));Y=int_of_float((-.ymin)*.float_of_int(JJ))} ; I=II ; J=JJ};;
let vision centre largeur=
let (a,b)=centre and hauteur=(largeur*.(float_of_int (size_y()))/.(float_of_int (size_x()))) in
fenetre (a-.(largeur/.2.)) (a+.(largeur/.2.)) (b-.(hauteur/.2.)) (b+.(hauteur/.2.));;
let axes rep =moveto 0 (rep.O.Y);
lineto (size_x()) (rep.O.Y);
moveto (rep.O.X) 0;
lineto (rep.O.X) (size_y());;
let trace_rep rep n couleur=
set_color couleur;
moveto 0 (rep.O.Y);
lineto (size_x()) (rep.O.Y);
moveto (rep.O.X) 0;
lineto (rep.O.X) (size_y());
let XM=int_of_float( float_of_int( abs(size_x()-(rep.O.X)) ) /. float_of_int(rep.I) )
and Xm=int_of_float( float_of_int( abs(rep.O.X) ) /. float_of_int(rep.I) )
and YM=int_of_float( float_of_int( abs(size_y()-(rep.O.Y)) ) /. float_of_int(rep.J) )
and Ym=int_of_float( float_of_int( abs(rep.O.Y) ) /. float_of_int(rep.J) ) in
let sX= float_of_int(rep.I)/.float_of_int(n)
and sY= float_of_int(rep.J)/.float_of_int(n) in
move_ecr { X=((rep.O.X)+(rep.I)) ; Y=(rep.O.Y) } ;
line_ecr { X=(((rep.O.X)+(rep.I))-7); Y=((rep.O.Y)+7) };
move_ecr { X=((rep.O.X)+(rep.I)) ; Y=(rep.O.Y) } ;
line_ecr { X=(((rep.O.X)+(rep.I))-7); Y=((rep.O.Y)-7) };
move_ecr { X=(rep.O.X) ; Y=(rep.O.Y)+(rep.J) } ;
line_ecr { X=((rep.O.X)-7); Y=(rep.O.Y)+(rep.J)-7 };
move_ecr { X=(rep.O.X) ; Y=(rep.O.Y)+(rep.J) } ;
line_ecr { X=((rep.O.X)+7); Y=(rep.O.Y)+(rep.J)-7 };
for i=0 to (XM+1) do
move_ecr { X= ((rep.O.X)+i*(rep.I)) ; Y= ((rep.O.Y)+5) };
line_ecr { X= ((rep.O.X)+i*(rep.I)) ; Y= ((rep.O.Y)-5) };
for j=1 to (n) do
move_ecr { X= ((rep.O.X)+i*(rep.I)) + int_of_float(float_of_int(j)*.sX) ; Y=((rep.O.Y)+2) };
line_ecr { X= ((rep.O.X)+i*(rep.I)) + int_of_float(float_of_int(j)*.sX) ; Y=((rep.O.Y)-2) };
done;
done;
for i=0 downto (-Xm-1) do
move_ecr { X= ((rep.O.X)+i*(rep.I)) ; Y= ((rep.O.Y)+5) };
line_ecr { X= ((rep.O.X)+i*(rep.I)) ; Y= ((rep.O.Y)-5) };
for j=(-1) downto (-n) do
move_ecr { X= ((rep.O.X)+i*(rep.I)) + int_of_float(float_of_int(j)*.sX) ; Y=((rep.O.Y)+2) };
line_ecr { X= ((rep.O.X)+i*(rep.I)) + int_of_float(float_of_int(j)*.sX) ; Y=((rep.O.Y)-2) }
done;
done;
for i=0 to (YM+1) do
move_ecr { X= ((rep.O.X)+5) ; Y= ((rep.O.Y)+i*(rep.J)) };
line_ecr { X= ((rep.O.X)-5) ; Y= ((rep.O.Y)+i*(rep.J)) };
for j=1 to (n) do
move_ecr { X=((rep.O.X)+2) ; Y= ((rep.O.Y)+i*(rep.J)) + int_of_float(float_of_int(j)*.sY) };
line_ecr { X=((rep.O.X)-2) ; Y= ((rep.O.Y)+i*(rep.J)) + int_of_float(float_of_int(j)*.sY) };
done;
done;
for i=0 downto (-Ym-1) do
move_ecr { X=((rep.O.X)+5) ; Y=((rep.O.Y)+i*(rep.J)) };
line_ecr { X=((rep.O.X)-5) ; Y=((rep.O.Y)+i*(rep.J)) };
for j=(-1) downto (-n) do
move_ecr { X=((rep.O.X)+2) ; Y= ((rep.O.Y)+i*(rep.J)) + int_of_float(float_of_int(j)*.sY) };
line_ecr { X=((rep.O.X)-2) ; Y= ((rep.O.Y)+i*(rep.J)) + int_of_float(float_of_int(j)*.sY) };
done;
done;
set_color black;;
let trace_fun f xmin xmax n rep=
let h=(xmax-.xmin)/.(float_of_int n) in
move_rep { x=xmin ; y= (f xmin) } rep;
for i=1 to n-1 do
line_rep { x=xmin+.((float_of_int i)*.h) ; y= f (xmin+.((float_of_int i)*.h)) } rep;
move_rep { x=xmin+.((float_of_int i)*.h) ; y=f (xmin+.((float_of_int i)*.h))} rep;
done;;
let nuage_pts pliste rep =
let pliste2 = ref (pliste) in
while (!pliste2)<>[] do
point_rep (hd !pliste2) rep;
pliste2 := tl (!pliste2);
done;;
let polygone pliste rep =
let pliste2 = ref (pliste) in
move_rep (hd !pliste2) rep;
pliste2 := (tl !pliste2);
while (!pliste2)<>[] do
line_rep (hd !pliste2) rep;
pliste2 := tl (!pliste2);
done;set_color black;;
let l_pts_fun f xmin xmax n =
let liste=ref([]) and h=(xmax-.xmin)/.(float_of_int n) in
for i=n-1 downto 1 do
liste:={ x=xmin+.((float_of_int i)*.h) ; y= f (xmin+.((float_of_int i)*.h)) }:: (!liste);
done;
(!liste);;
let l_pts_para (f,g) tmin tmax n=
let liste=ref([]) and h=((tmax-.tmin)/.(float_of_int(n))) in
for i=n-1 downto 1 do
liste:={x=(f(tmin+.((float_of_int i)*.h)));y=(g(tmin+.((float_of_int i)*.h)))}::(!liste);
done;
(!liste);;
let minx pliste =
let pliste2 = ref (pliste) and mx =ref(((hd (pliste)).x)) in
while (!pliste2)<>[] do
if ((hd (!pliste2)).x) <= (!mx) then mx:=((hd (!pliste2)).x);
pliste2 := tl (!pliste2);
done;
(!mx);;
let miny pliste =
let pliste2 = ref (pliste) and my =ref(((hd (pliste)).y)) in
while (!pliste2)<>[] do
if ((hd (!pliste2)).y) <= (!my) then my:=((hd (!pliste2)).y);
pliste2 := tl (!pliste2);
done;
(!my);;
let maxx pliste =
let pliste2 = ref (pliste) and Mx =ref(((hd (pliste)).x)) in
while (!pliste2)<>[] do
if ((hd (!pliste2)).x) >= (!Mx) then Mx:=((hd (!pliste2)).x);
pliste2 := tl (!pliste2);
done;
(!Mx);;
let maxy pliste =
let pliste2 = ref (pliste) and My =ref(((hd (pliste)).y)) in
while (!pliste2)<>[] do
if ((hd (!pliste2)).y) >= (!My) then My:=((hd (!pliste2)).y);
pliste2 := tl (!pliste2);
done;
(!My);;
let plot_nuage pliste=
let xmin=(minx pliste)
and xMax=(maxx pliste)
and ymin=(miny pliste)
and yMax=(maxy pliste) in
let xm=xmin-.(0.1)*.(xMax-.xmin)
and xM=xMax+.(0.1)*.(xMax-.xmin)
and ym=ymin-.(0.1)*.(yMax-.ymin)
and yM=yMax+.(0.1)*.(yMax-.ymin) in
let (rx,ry)=
if (yM-.ym)>(100.*.(xM-.xm)) then
((0.),(( (float_of_int(size_y())) *. (xM-.xm) /.(float_of_int(size_x())) )+.(ym-.yM) )/.(2.))
else
if (xM-.xm)>(100.*.(yM-.ym)) then
((( (float_of_int(size_x())) *. (yM-.ym) /.(float_of_int(size_y())) )+.(xm-.xM) )/.(2.),0.)
else
if ((float_of_int(size_x())) *. (yM-.ym) /.(float_of_int(size_y())))>=xM-.xm
then ((( (float_of_int(size_x())) *. (yM-.ym) /.(float_of_int(size_y())) )+.(xm-.xM) )/.(2.),0.)
else ((0.),(( (float_of_int(size_y())) *. (xM-.xm) /.(float_of_int(size_x())) )+.(ym-.yM) )/.(2.)); in
let rep=fenetre (xm-.rx) (xM+.rx) (ym-.ry) (yM+.ry) in
trace_rep rep 10 black;
nuage_pts pliste rep;;
let plot_polygone pliste=
let xmin=(minx pliste)
and xMax=(maxx pliste)
and ymin=(miny pliste)
and yMax=(maxy pliste) in
let xm=xmin-.(0.1)*.(xMax-.xmin)
and xM=xMax+.(0.1)*.(xMax-.xmin)
and ym=ymin-.(0.1)*.(yMax-.ymin)
and yM=yMax+.(0.1)*.(yMax-.ymin) in
let (rx,ry)=
if (yM-.ym)>(100.*.(xM-.xm)) then
((0.),(( (float_of_int(size_y())) *. (xM-.xm) /.(float_of_int(size_x())) )+.(ym-.yM) )/.(2.))
else
if (xM-.xm)>(100.*.(yM-.ym)) then
((( (float_of_int(size_x())) *. (yM-.ym) /.(float_of_int(size_y())) )+.(xm-.xM) )/.(2.),0.)
else
if ((float_of_int(size_x())) *. (yM-.ym) /.(float_of_int(size_y())))>=xM-.xm
then ((( (float_of_int(size_x())) *. (yM-.ym) /.(float_of_int(size_y())) )+.(xm-.xM) )/.(2.),0.)
else ((0.),(( (float_of_int(size_y())) *. (xM-.xm) /.(float_of_int(size_x())) )+.(ym-.yM) )/.(2.)); in
let rep=fenetre (xm-.rx) (xM+.rx) (ym-.ry) (yM+.ry) in
trace_rep rep 10 black;
polygone pliste rep;;
let plot_fun f xm xM n=
let pliste= (l_pts_fun f xm xM n) in
plot_polygone pliste;;
let plot_para (f,g) tmin tmax n=
let pliste= (l_pts_para (f,g) tmin tmax n) in
plot_polygone pliste;;
let plot_polaire f amin amax n=
let pliste= (l_pts_para ((fun x->cos(x)*.f(x)),(fun x->sin(x)*.f(x))) amin amax n) in
plot_polygone pliste;;
let convert_vect t = let n=vect_length t and r=ref[ ] in
for i=(n-1) downto 0 do
let (m,n)=t.(i) in r :=[{x=m ;y=n}]@( !r)
done ;
( !r);;
let rec convert_list l = match l with
|[ ] -> [ ]
|[(a,b)] -> [{x=a ;y=b}]
|r::ll -> (convert_list [r])@(convert_list ll);;
let rec projection l a b = match l with
|[ ] -> []
|[(t)] -> [{x=t.(a-1);y=t.(b-1)}]
|r::ll -> (projection [r] a b)@(projection ll a b);;