Fractionner mon code en procédures et fonctions : quelles différences entre les deux ?
Alors voilà, j'ai un souci maintenant que mon code est tapé (:ccool: merci à vous) : je dois le fractionner en procédures et fonctions...
Kesako ??
J'ai lu que les procédures ne renvoyaient rien et que les fonctions renvoyaient quelque chose.
Par exemple, dans mon code, à un moment je centre et je redimensionne la taille de la molécule en fonction de la taille du canevas.
Voici le code :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| begin
mox:=0;
moy:=0;
dxmin:=PaintBox1.Width;
dymin:=PaintBox1.Height;
dxmax:=-1*PaintBox1.Width;
dymax:=-1*PaintBox1.Height;
alphax:=0;
alphay:=0;
For i:=1 to MyFPMol.p_NX do begin
PAt:=MyFPMol.AtmSet[i];
dx:=PAt^.P[0]*SclF; dy:=PAt^.P[1]*SclF;
writeln('dx :'+FloatToStr(dx));
writeln('dy :'+FloatToStr(dy));
if dx < dxmin then dxmin:=dx;
if dx > dxmax then dxmax:=dx;
if dy < dymin then dymin:=dy;
if dy > dymax then dymax:=dy;
if i = MyFPMol.p_NX then
begin
xg := (dxmax+dxmin)/2;
yg := (dymax+dymin)/2;
writeln('dxmin :'+FloatToStr(dxmin));
writeln('dymin :'+FloatToStr(dymin));
writeln('dxmax :'+FloatToStr(dxmax));
writeln('dymax :'+FloatToStr(dymax));
writeln('xg :'+FloatToStr(xg));
writeln('xy :'+FloatToStr(yg));
end;
end;
For i:=1 to MyFPMol.p_NX do begin
PAt:=MyFPMol.AtmSet[i];
dx:=PAt^.P[0]*SclF; dy:=PAt^.P[1]*SclF;
dx:=dx-xg;
dy:=dy-yg;
writeln('dxcent :'+FloatToStr(dx));
writeln('dycent :'+FloatToStr(dy));
alphax:=PaintBox1.Width/(dxmax-dxmin);
alphay:=PaintBox1.Height/(dymax-dymin);
if (alphax < alphay) then
begin
dx:=alphax * dx;
dy:=alphax * dy;
writeln('dxalphax :'+FloatToStr(dx));
writeln('dyalphax :'+FloatToStr(dy));
end
else if (alphax > alphay) then
begin
dx:=alphay * dx;
dy:=alphay * dy;
writeln('dxalphay :'+FloatToStr(dx));
writeln('dyalphay :'+FloatToStr(dy));
end;
dx:=dx+PaintBox1.Width/2;
dy:=dy+PaintBox1.Height/2;
writeln('dxfin :'+FloatToStr(dx));
writeln('dyfin :'+FloatToStr(dy));
ix:=round(dx); iy:=round(dy);
end; |
Je sais que je pourrais l'épurer et le rendre plus propre mais la question n'est pas là.
Je pense laisser la boucle d'entrée et donc j'aurai cela
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| if dx < dxmin then dxmin:=dx;
if dx > dxmax then dxmax:=dx;
if dy < dymin then dymin:=dy;
if dy > dymax then dymax:=dy;
if i = MyFPMol.p_NX then
begin
xg := (dxmax+dxmin)/2;
yg := (dymax+dymin)/2;
writeln('dxmin :'+FloatToStr(dxmin));
writeln('dymin :'+FloatToStr(dymin));
writeln('dxmax :'+FloatToStr(dxmax));
writeln('dymax :'+FloatToStr(dymax));
writeln('xg :'+FloatToStr(xg));
writeln('xy :'+FloatToStr(yg));
end;
end;
For i:=1 to MyFPMol.p_NX do begin
PAt:=MyFPMol.AtmSet[i];
dx:=PAt^.P[0]*SclF; dy:=PAt^.P[1]*SclF;
dx:=dx-xg;
dy:=dy-yg;
writeln('dxcent :'+FloatToStr(dx));
writeln('dycent :'+FloatToStr(dy));
alphax:=PaintBox1.Width/(dxmax-dxmin);
alphay:=PaintBox1.Height/(dymax-dymin);
if (alphax < alphay) then
begin
dx:=alphax * dx;
dy:=alphax * dy;
writeln('dxalphax :'+FloatToStr(dx));
writeln('dyalphax :'+FloatToStr(dy));
end
else if (alphax > alphay) then
begin
dx:=alphay * dx;
dy:=alphay * dy;
writeln('dxalphay :'+FloatToStr(dx));
writeln('dyalphay :'+FloatToStr(dy));
end;
dx:=dx+PaintBox1.Width/2;
dy:=dy+PaintBox1.Height/2;
writeln('dxfin :'+FloatToStr(dx));
writeln('dyfin :'+FloatToStr(dy));
ix:=round(dx); iy:=round(dy); |
J'aurais voulu transformer tout cela en une fonction ou procédure; ainsi, j'aurai juste eu à faire par exemple centrage(dx,dy) à taper.
Merci