unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ComCtrls, Buttons, ExtCtrls,Math; type TForm1 = class(TForm) Image1: TImage; Edit1: TEdit; Label1: TLabel; Point: TSpeedButton; Ligne: TSpeedButton; PLigne: TSpeedButton; Polygone: TSpeedButton; Button1: TButton; Memo1: TMemo; BitBtn1: TBitBtn; BitBtn2: TBitBtn; Timer1: TTimer; BitBtn3: TBitBtn; Button2: TButton; Button3: TButton; Memo2: TMemo; Button4: TButton; BitBtn4: TBitBtn; LabeledEdit1: TLabeledEdit; LabeledEdit2: TLabeledEdit; BitBtn5: TBitBtn; BitBtn6: TBitBtn; SpeedButton1: TSpeedButton; SpeedButton2: TSpeedButton; SpeedButton3: TSpeedButton; Memo3: TMemo; Label2: TLabel; ListBox1: TListBox; Button5: TButton; Button6: TButton; procedure Edit1Change(Sender: TObject); procedure Image1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); procedure Image1DblClick(Sender: TObject); procedure PointClick(Sender: TObject); procedure LigneClick(Sender: TObject); procedure PLigneClick(Sender: TObject); procedure PolygoneClick(Sender: TObject); procedure FormCreate(Sender: TObject); procedure Button1Click(Sender: TObject); procedure BitBtn1Click(Sender: TObject); procedure BitBtn2Click(Sender: TObject); procedure BitBtn3Click(Sender: TObject); procedure Timer1Timer(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure Button4Click(Sender: TObject); procedure BitBtn4Click(Sender: TObject); procedure BitBtn5Click(Sender: TObject); procedure BitBtn6Click(Sender: TObject); procedure SpeedButton1Click(Sender: TObject); procedure SpeedButton2Click(Sender: TObject); procedure SpeedButton3Click(Sender: TObject); procedure Image1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); private { Déclarations privées } public { Déclarations publiques } end; Type TPoint=Record X,Y:integer; end; Type Objet=Record Nature : String; ListePoint:array of TPoint; end; var Form1: TForm1; ListeObjets:array of Objet; LS,LSO,LG:Objet; NouvelObjet:String; Sweep:integer; clique:integer; function DistPoint(P1,P2:tpoint):real; function DistSegment(P,P1,P2:tPoint;M:tmemo):real; // function Gausse(V:integer):real; implementation {function Gausse(V:integer):real; begin Gausse:=(1/(sgma*sqrt(2*pi))*exp(-sqr(V)/(2*sqr(sgma))); end;} function DistPoint(P1,P2:tpoint):real; begin DistPoint:=sqrt(sqr(P2.X-P1.X)+sqr(P2.Y-P1.Y)); end; function DistSegment(P,P1,P2:tPoint;M:tmemo):Real; Var Angle:double; begin DistSegment:=abs(((P1.Y-P2.Y)*P.X+ (P2.X-P1.X)*P.Y+ (P1.X*P2.Y-P2.X*P1.Y))/ sqrt(sqr(P2.X-P1.X)+sqr(P2.Y-P1.Y))); if DistPoint(P1,P)*DistPoint(P,P2)=0 then DistSegment:=0 else begin Angle:=(((P1.X-P.X)*(P1.X-P2.X))+((P1.Y-P.Y)*(P1.Y-P2.Y)))/(DistPoint(P1,P)*DistPoint(P1,P2)); //L'angle 1 //M.Lines.Add(FloatToStr(Angle)); //M.Lines.SaveToFile('d:\bbbb'); //If ABS(angle)=1 then angle:=0.99*Angle; if angle>1 then angle:=1; if angle<-1 then angle:=-1; Angle:=radtodeg(arccos(Angle)); if Angle>90 then DistSegment:=DistPoint(P1,P); Angle:=round((((P2.X-P.X)*(P2.X-P1.X))+((P2.Y-P.Y)*(P2.Y-P1.Y)))/(DistPoint(P2,P)*DistPoint(P2,P1))); //L'angle 2 //if abs(angle)>1 then showmessage(floattostr(angle))else if angle>1 then angle:=1; if angle<-1 then angle:=-1; Angle:=radtodeg(arccos(Angle)); if Angle>90 then DistSegment:=DistPoint(P2,P); end; end; {$R *.dfm} procedure TForm1.Edit1Change(Sender: TObject); var B:TColor; begin B:=Image1.Canvas.Brush.Color; Image1.Canvas.Brush.Color:=86186240; Sweep:=strtoint(Edit1.Text); Image1.Canvas.MoveTo(0,Sweep); Image1.Canvas.LineTo(Image1.Width,Sweep); end; procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); var i,YY: integer; begin if Point.Flat then begin Point.Flat:=false; SetLength(ListeObjets,length(ListeObjets)+1); ListeObjets[length(ListeObjets)-1].Nature:='P'; SetLength(ListeObjets[length(ListeObjets)-1].ListePoint,1); ListeObjets[length(ListeObjets)-1].ListePoint[0].X:=X; ListeObjets[length(ListeObjets)-1].ListePoint[0].Y:=Y; Image1.Canvas.Ellipse(X,Y,X+4,Y+4); end; if Ligne.Flat then begin inc(clique); if clique=1 then begin SetLength(ListeObjets,length(ListeObjets)+1); ListeObjets[length(ListeObjets)-1].Nature:='L'; Image1.Canvas.MoveTo(X,Y); end; SetLength(ListeObjets[length(ListeObjets)-1].ListePoint,clique); ListeObjets[length(ListeObjets)-1].ListePoint[clique-1].X:=X; ListeObjets[length(ListeObjets)-1].ListePoint[clique-1].Y:=Y; if clique=2 then begin Ligne.Flat:=false;clique:=0;Image1.Canvas.LineTo(X,Y);end; end; if PLigne.Flat then begin inc(clique); if clique=1 then begin SetLength(ListeObjets,length(ListeObjets)+1); ListeObjets[length(ListeObjets)-1].Nature:='PL'; Image1.Canvas.MoveTo(X,Y); end; SetLength(ListeObjets[length(ListeObjets)-1].ListePoint,clique); ListeObjets[length(ListeObjets)-1].ListePoint[clique-1].X:=X; ListeObjets[length(ListeObjets)-1].ListePoint[clique-1].Y:=Y; Image1.Canvas.LineTo(X,Y); end; if Polygone.Flat then begin inc(clique); if clique=1 then begin SetLength(ListeObjets,length(ListeObjets)+1); ListeObjets[length(ListeObjets)-1].Nature:='PG'; Image1.Canvas.MoveTo(X,Y); end; SetLength(ListeObjets[length(ListeObjets)-1].ListePoint,clique); ListeObjets[length(ListeObjets)-1].ListePoint[clique-1].X:=X; ListeObjets[length(ListeObjets)-1].ListePoint[clique-1].Y:=Y; Image1.Canvas.LineTo(X,Y); end; end; procedure TForm1.Image1DblClick(Sender: TObject); var i,YY: integer; begin if PLigne.Flat then begin PLigne.Flat:=false; clique:=0; end; if Polygone.Flat then begin Polygone.Flat:=false; Image1.Canvas.LineTo(ListeObjets[length(ListeObjets)-1].ListePoint[0].X,ListeObjets[length(ListeObjets)-1].ListePoint[0].Y); clique:=0; end; // changement de plan for i:=0 to Length(ListeObjets[length(ListeObjets)-1].ListePoint)-1 do begin YY:=ListeObjets[length(ListeObjets)-1].ListePoint[i].Y; ListeObjets[length(ListeObjets)-1].ListePoint[i].Y:=500-YY; end; end; procedure TForm1.PointClick(Sender: TObject); begin Point.Flat:=true; end; procedure TForm1.LigneClick(Sender: TObject); begin Ligne.Flat:=true; end; procedure TForm1.PLigneClick(Sender: TObject); begin PLigne.Flat:=true; end; procedure TForm1.PolygoneClick(Sender: TObject); begin Polygone.Flat:=true; end; procedure TForm1.FormCreate(Sender: TObject); begin Image1.Canvas.Create; clique:=0; end; procedure TForm1.Button1Click(Sender: TObject); var i:integer; begin for i:=0 to length(ListeObjets)-1 do Memo1.Lines.Add(ListeObjets[i].nature); end; procedure TForm1.BitBtn1Click(Sender: TObject); var Taille,i,j,k,w:integer; DistSweep,Dist:real; Pix:tPoint; begin for i:= 1 to Image1.Width do //X for j:= 1 to Sweep-1 do //Y , la veritable formule est < sweep mais comme c'est des pix de ce fait discontinue c'est exactement sweep-1, de toute façon c pas comme ça pour le continu for k:= 0 to Length(ListeObjets)-1 do begin DistSweep:=ABS(Sweep-J); Pix.X:=i; Pix.Y:=j; Dist:=-1; if ListeObjets[k].Nature='P' then Dist:=Round(DistPoint(ListeObjets[k].ListePoint[0],Pix)); if ListeObjets[k].Nature='L' then Dist:=Round(DistSegment(Pix,ListeObjets[k].ListePoint[0],ListeObjets[k].ListePoint[1],memo2)); if DistSweep=Dist then Image1.Canvas.Ellipse(I,J,I+2,J+2); if ListeObjets[k].Nature='PL'then for w:=0 to length(ListeObjets[k].ListePoint)-2 do begin Dist:=round(DistSegment(Pix,ListeObjets[k].ListePoint[w],ListeObjets[k].ListePoint[w+1],memo2)); if DistSweep=Dist then Image1.Canvas.Ellipse(I,J,I+2,J+2); end; if ListeObjets[k].Nature='PG'then for w:=0 to length(ListeObjets[k].ListePoint)-1 do begin Dist:=round(DistSegment(Pix,ListeObjets[k].ListePoint[w],ListeObjets[k].ListePoint[(w+1)mod length(ListeObjets[k].ListePoint)],memo2)); if DistSweep=Dist then Image1.Canvas.Ellipse(I,J,I+2,J+2); end; end; end; procedure TForm1.BitBtn2Click(Sender: TObject); var H,W:integer; begin H:=Image1.Height; W:=Image1.Width; Image1.Canvas.Brush.Color:=clWhite; Image1.Canvas.Rectangle(0,0,W,H); setlength(ListeObjets,0); end; procedure TForm1.BitBtn3Click(Sender: TObject); begin Timer1.Enabled:=not Timer1.Enabled; end; procedure TForm1.Timer1Timer(Sender: TObject); var H,W,I:integer; begin inc(Sweep); Edit1.Text:=inttostr(Sweep); H:=Image1.Height; W:=Image1.Width; Image1.Canvas.Brush.Color:=clWhite; Image1.Canvas.Rectangle(0,0,W,H); Image1.Canvas.MoveTo(0,Sweep); Image1.Canvas.LineTo(Image1.Width,Sweep); for i:=0 to length(ListeObjets)-1do if ListeObjets[i].Nature='P' then Image1.Canvas.Ellipse(ListeObjets[i].ListePoint[0].X,ListeObjets[i].ListePoint[0].Y,ListeObjets[i].ListePoint[0].X+3,ListeObjets[i].ListePoint[0].Y+3) else if ListeObjets[i].Nature='L' then begin Image1.Canvas.MoveTo(ListeObjets[i].ListePoint[0].X,ListeObjets[i].ListePoint[0].Y); Image1.Canvas.LineTo(ListeObjets[i].ListePoint[1].X,ListeObjets[i].ListePoint[1].Y);end else if ListeObjets[i].Nature='PL' then for w:=0 to length(ListeObjets[i].ListePoint)-2 do begin Image1.Canvas.MoveTo(ListeObjets[i].ListePoint[w].X,ListeObjets[i].ListePoint[w].Y); Image1.Canvas.LineTo(ListeObjets[i].ListePoint[w+1].X,ListeObjets[i].ListePoint[w+1].Y); end else if ListeObjets[i].Nature='PG' then for w:=0 to length(ListeObjets[i].ListePoint)-1 do begin Image1.Canvas.MoveTo(ListeObjets[i].ListePoint[w].X,ListeObjets[i].ListePoint[w].Y); Image1.Canvas.LineTo(ListeObjets[i].ListePoint[(w+1)mod length(ListeObjets[i].ListePoint)].X,ListeObjets[i].ListePoint[(w+1)mod length(ListeObjets[i].ListePoint)].Y); end; BitBtn1.Click; end; procedure TForm1.Button2Click(Sender: TObject); var PP,PPP,PPPP:tpoint; begin PPP.X:=0; PPP.Y:=0; PPPP.X:=0; PPPP.Y:=10; pp.X:=5; pp.Y:=20; ShowMessage(floattostr(DistSegment(pp,ppp,pppp,memo2))); end; procedure TForm1.Button3Click(Sender: TObject); var PA,PP,PB:TPoint; Dist:Real; begin PP.X:=10; PP.Y:=10; PA.X:=10; PA.Y:=0; PB.X:=0; PB.Y:=10; Dist:=(((PP.X-PA.X)*(PP.X-PB.X))+((PP.Y-PA.Y)*(PP.Y-PB.Y)))/(DistPoint(PP,PA)*DistPoint(PP,PB)); showmessage(floattostr(radtodeg(arccos(Dist)))); end; procedure TForm1.Button4Click(Sender: TObject); begin showmessage(floattostr(radtodeg(arccos(-1)))); //radtodeg(arccos(Angle)); end; procedure TForm1.BitBtn4Click(Sender: TObject); var L,LSO:objet; i,si,oi,j,sgma,pas,kint:integer; k,seg,x,y:real; begin sgma:=strtoint(LabeledEdit1.text); pas:=strtoint(LabeledEdit2.text); L:=ListeObjets[0]; //showmessage(inttostr(length(L.ListePoint))); si:=-1; for i:=0 to length(L.ListePoint)-2 do begin inc(si); SetLength(LS.ListePoint,si+1); LS.ListePoint[si]:=L.ListePoint[i]; K:=sqrt(sqr(L.ListePoint[i].x-L.ListePoint[i+1].x)+sqr(L.ListePoint[i].y-L.ListePoint[i+1].y))/pas; kint:=trunc(k); for J:=1 to kint do begin x:=(1-(j/k))*L.ListePoint[i].x+(j/k)*L.ListePoint[i+1].x; y:=(1-(j/k))*L.ListePoint[i].y+(j/k)*L.ListePoint[i+1].y; inc(si); SetLength(LS.ListePoint,si+1); LS.ListePoint[si].X:=trunc(x); LS.ListePoint[si].Y:=trunc(y); end; end; inc(si); SetLength(LS.ListePoint,si+1); LS.ListePoint[si]:=L.ListePoint[length(L.ListePoint)-1]; showmessage(inttostr(length(LS.ListePoint))); end; procedure TForm1.BitBtn5Click(Sender: TObject); var i:integer; begin for i:=0 to length(LS.ListePoint)-1 do Image1.Canvas.Ellipse(LS.ListePoint[i].X,500-LS.ListePoint[i].Y,LS.ListePoint[i].X+5,500-LS.ListePoint[i].Y+5); end; procedure TForm1.BitBtn6Click(Sender: TObject); var i,oi,sgma:integer; x,y:integer; begin //*************PROLONGATION coté debut sgma:=StrToInt(LabeledEdit1.Text); SetLength(LSO.ListePoint,0); oi:=-1; for i:=1 to 4*sgma do //le point 0 est le point de symetrie begin x:=(-LS.ListePoint[i].X)+(2*LS.ListePoint[0].X); y:=(-LS.ListePoint[i].Y)+(2*LS.ListePoint[0].Y); inc(oi); SetLength(LSO.ListePoint,oi+1); LSO.ListePoint[oi].X:=trunc(x); LSO.ListePoint[oi].Y:=trunc(y); end; //***************Ajout ligne segmentée for i:=0 to length(LS.ListePoint)-1 do //le point 0 est le point de symetrie begin x:=LS.ListePoint[i].X; y:=LS.ListePoint[i].Y; inc(oi); SetLength(LSO.ListePoint,oi+1); LSO.ListePoint[oi].X:=trunc(x); LSO.ListePoint[oi].Y:=trunc(y); end; //*************PROLONGATION coté fin for i:=1 to 4*sgma do //le point 0 est le point de symetrie begin x:=(-LS.ListePoint[length(LS.ListePoint)-1-i].X)+(2*LS.ListePoint[length(LS.ListePoint)-1].X); y:=(-LS.ListePoint[length(LS.ListePoint)-1-i].Y)+(2*LS.ListePoint[length(LS.ListePoint)-1].Y); inc(oi); SetLength(LSO.ListePoint,oi+1); LSO.ListePoint[oi].X:=trunc(x); LSO.ListePoint[oi].Y:=trunc(y); end; showmessage(inttostr(Length(LSO.ListePoint))); end; procedure TForm1.SpeedButton1Click(Sender: TObject); var sgma,i,k,ig:integer; G,x,y:real;s:string; begin sgma:=strtoint(LabeledEdit1.text); ig:=-1; setlength(LG.ListePoint,0); for i:=4*sgma to length(LSO.ListePoint)-(4*sgma)-1 do begin x:=0;y:=0; s:=inttostr(i); for k:=i-(4*sgma) to i+(4*sgma) do begin G:=exp(-sqr(K-i)/(2*sqr(sgma)))/(sgma*sqrt(2*pi)); s:=s+' '+inttostr(k);x:=x+(LSO.ListePoint[K].X*G); y:=y+(LSO.ListePoint[K].Y*G); //showmessage('G='+floattostr(G)+' x(k)='+inttostr(LSO.ListePoint[K].X)+' y(k)='+inttostr(LSO.ListePoint[K].Y)+' x='+floattostr(x)+' y='+floattostr(y)); end; inc(IG); ListBox1.Items.Add(s); setlength(LG.ListePoint,IG+1); LG.ListePoint[ig].X:=trunc(x);//LG.ListePoint[ig].X:=trunc(x/((8*sgma)+1)); LG.ListePoint[ig].Y:=trunc(y);//LG.ListePoint[ig].Y:=trunc(y/((8*sgma)+1)); //showmessage(floattostr(x)+' '+floattostr(y)); end; showmessage(inttostr(length(LG.ListePoint))); end; procedure TForm1.SpeedButton2Click(Sender: TObject); var i:integer; begin for i:=0 to length(LSO.ListePoint)-1 do Image1.Canvas.Ellipse(LSO.ListePoint[i].X,500-LSO.ListePoint[i].Y,LSO.ListePoint[i].X+3,500-LSO.ListePoint[i].Y+3); end; procedure TForm1.SpeedButton3Click(Sender: TObject); var i:integer; begin for i:=0 to length(LG.ListePoint)-1 do Image1.Canvas.Ellipse(LG.ListePoint[i].X,500-LG.ListePoint[i].Y,LG.ListePoint[i].X+3,500-LG.ListePoint[i].Y+3); for i:=0 to length(LG.ListePoint)-1 do ListBox1.Items.Add('i = '+inttostr(i)+' x = '+inttostr(LG.ListePoint[i].X)+' y = '+inttostr(LG.ListePoint[i].Y)); Image1.Canvas.Ellipse(LG.ListePoint[i].X,500-LG.ListePoint[i].Y,LG.ListePoint[i].X+3,500-LG.ListePoint[i].Y+3); end; procedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); begin Label2.Caption:='X = '+inttostr(x)+' Y = '+inttostr(y); end; end.