admin Administrators
Tổng số bài gửi : 39 Points : 147 Reputation : 0 Join date : 04/09/2010 Age : 32 Đến từ : nam định
| | code ve hinh tam giac | |
program Thuat_toan_BresenhamLine; uses graph,crt; var gd,gm:integer; {---------------------------------------------------------------------------} procedure Doitoado(var a,b,c,d:integer); var temp:integer; begin temp:=a;a:=c;c:=temp; temp:=b;b:=d;d:=temp; end; {-------------------------------------------------------------------------- Thu tuc ve voi truong hop duong thang song song voi cac truc toa do} procedure Vesongsong(x1,y1,x2,y2:integer); var x,y:integer; begin if x1=x2 then begin if y1>y2 then doitoado(x1,y1,x2,y2); y:=y1; x:=x1; repeat putpixel(x,y,white); y:=y+1; until y = y2; end; if y1 = y2 then begin if x1>x2 then doitoado(x1,y1,x2,y2); y:=y1; x:=x1; repeat putpixel(x,y,white); x:=x+1; until x = x2;
end; end; {---------------------------------------------------------------------------} procedure Bresenham_line(x1,y1,x2,y2:integer); var x,y,dx,dy,const1,const2,p:integer; begin if (x1=x2) or (y1=y2) then begin vesongsong(x1,y1,x2,y2); exit; end;
dx:=abs(x2-x1);dy:=abs(y2-y1); if dx>dy then begin if x1>x2 then Doitoado(x1,y1,x2,y2); p:=2*dy-dx;const1:=2*dy;const2:=2*(dy-dx); x:=x1;y:=y1; if y2>y1 then begin repeat putpixel(x,y,white); x:=x+1; if p<0 then p:=p+const1 else begin y:=y+1; p:=p+const2; end; until x = x2; end else begin repeat putpixel(x,y,white); x:=x+1; if p<0 then p:=p+const1 else begin y:=y-1; p:=p+const2; end; until x=x2; end; end else begin if y1>y2 then Doitoado(x1,y1,x2,y2); p:=2*dx-dy;const1:=2*dx;const2:=2*(dx-dy); x:=x1;y:=y1; if x2>x1 then begin repeat putpixel(x,y,white); y:=y+1; if p<0 then p:=p+const1 else begin x:=x+1; p:=p+const2; end; until y=y2; end else begin repeat putpixel(x,y,white); y:=y+1; if p<0 then p:=p+const1 else begin x:=x-1; p:=p+const2; end; until y=y2; end; end; end;
{--------------------------------------------------------------------------} BEGIN gd:=0; initgraph(gd,gm,'c:\bp\bgi'); Bresenham_line(100,0,100,400); Bresenham_line(100,100,500,100); Bresenham_line(100,0,500,100); Bresenham_line(100,400,500,100); Bresenham_line(0,0,100,400); Bresenham_line(0,400,100,0); readln; closegraph; END.
| |
|