Bài toán loại 2 D : {Chuyen tu cua so thuc sang cs nhin} procedure wtvp:point_2d;var q:point_2d;{chuyen sang cs nhin}begin q.x:=p.x*cgx; q.y:=p.y*cgy; end; {Chuyen tu cs nhin sang man
Trang 1Bài toán loại 2 D :
{Chuyen tu cua so thuc sang cs nhin}
procedure wtv(p:point_2d;var q:point_2d);{chuyen sang cs nhin}begin
q.x:=p.x*cgx;
q.y:=p.y*cgy;
end;
{Chuyen tu cs nhin sang man hinh}
procedure vts(p:point_2d; var q:point_2d);{chuyen sang man hinh}begin
Trang 2{ if xmin>0 then xmin:=0;
if ymin>0 then ymin:=0;
if xmax<0 then xmax:=0;
if ymax<0 then ymax:=0;}
Trang 3vts(p2,p2);
lineto(round(p2.x),round(p2.y)); x:=x+b;
procedure mt3333(a,b:mt2;var c:mt2);var i,j,k:integer;
begin
for i:=1 to 3 do
for j:=1 to 3 do
c[i,j]:=0;
Trang 4mt1333(mtb,mttt,mtkqb);
mt1333(mtc,mttt,mtkqc);
kqa.x :=mtkqa[1,1];kqa.y:=mtkqa[1,2]; kqb.x :=mtkqb[1,1];kqb.y:=mtkqb[1,2]; kqc.x :=mtkqc[1,1];kqc.y:=mtkqc[1,2];}{ write('Nhap Lx =');readln(lx);
write('Nhap Ly =');readln(ly);
mta[1,1]:=a.x;mta[1,2]:=a.y;mta[1,3]:=1; mtb[1,1]:=b.x;mtb[1,2]:=b.y;mtb[1,3]:=1; mtc[1,1]:=c.x;mtc[1,2]:=c.y;mtc[1,3]:=1; mttl[1,1]:=lx;mttl[2,1]:=0;mttl[3,1]:=0; mttl[1,2]:=0;mttl[2,2]:=ly;mttl[3,2]:=0; mttl[1,3]:=0;mttl[2,3]:=0;mttl[3,3]:=1;
mt1333(mta,mttl,mtkqa);
mt1333(mtb,mttl,mtkqb);
mt1333(mtc,mttl,mtkqc);
Trang 6Function tiento(x:real):boolean; forward;
Procedure Status(fx:string;a,b,x:real); forward;
if ghA=ghB then if ghA=0 then ghB:=1 else ghA:=-ghB;
if ghA>ghB then begin ghA:=ghA+ghB; ghB:=ghA-ghB; ghA:=ghA-ghB;end;
Trang 7i:=0;while (not f(ghA+(ghB-ghA)*(i/Ndoan)))and(i<=Ndoan)do
i:=i+1;Fmin:=y[1];fmax:=fmin;
for i:=0 to Ndoan do
if f(ghA+(ghB-ghA)*(i/Ndoan))then
for j:=1 to NN do begin if fmin>y[j] then fmin:=y[j];
if fmax<y[j] then fmax:=y[j];end;
outtextxy(Xmin+dx-round(ghA*tX)+3,Ymax-dy+round(fmin*tY)-8,'0');
end;
dx,Ymax);
dy+round(fmin*tY)+2,'B');
end else start:=false;
End;
End else Message('Canh bao ','Ham nay khong xac dinh trong khoang [A,B]' );
Trang 8Settextjustify(lefttext,1);settextstyle(Ttype,Tdir,Tsize);setcolor(Tcolor);
outtextxy(Xmin+3,Ymin+H_button div 2-1,' Ham so:');
outtextxy(Xmin+305,Ymin+H_button div 2-1,' Gioi han:');
outtextxy(Xmin+470,Ymin+H_button div 2-1,' N§:X Y');
Trang 10else begin tiento:=false;i:=1;end;
Trang 12Procedure ChieuPC(P:point3d;var Pa:Point2d);
Procedure ChieuSS(P:point3d;var Pa:Point2d);
Function Cohen3D(var p1,p2:point3D):Boolean;
Function PerClipLine(p1,p2:Point3d;var q1,q2:Point2d):boolean;
Procedure ClipPoly(np:integer;P:array of point3d;var nd:integer;var DG:array of pointtype);
Implementation
var
XWmin,YWmin,XWmax,YWmax:real; {Window trong he toa do thuc 2D} XVmin,YVmin,XVmax,YVmax:Integer;{Viewport trong thiet bi hien thi} Zfront,Zback,ClipX,clipY:real; {khung nhin lap the cho phoi canh}
Trang 13if x1<x2 then begin XWmin:=x1;XWmax:=x2;end
else begin XWmin:=x2;XWmax:=x1;end;
if y1<y2 then begin YWmin:=y1;YWmax:=y2;end
else begin YWmin:=y2;YWmax:=y1;end;
if x1<x2 then begin XVmin:=x1;XVmax:=x2;end
else begin XVmin:=x2;XVmax:=x1;end;
Trang 14if y1<y2 then begin YVmin:=y1;YVmax:=y2;end
else begin YVmin:=y2;YVmax:=y1;end;
var i,j:integer;
Begin
for i:=1 to 3 do for j:=1 to 3 do
if i<>j then mt[i,j]:=0 else mt[i,j]:=1;
for i:=1 to 4 do for j:=1 to 4 do
if i<>j then mt[i,j]:=0 else mt[i,j]:=1;
End;
Trang 15{ -}
Procedure Tinhtien3d(x,y,z:real;var MT:MT3d);Begin
{ -}
Procedure NhanMT3d(a,b:MT3d;var MT:MT3d); var i,j,k:byte;
Trang 17if P.z<Zfront then ma:=ma+1;
{ if p.z>Zback then ma:=ma+2;}
if P.y> P.z*ClipY then ma:=ma+4;
if P.y<-P.z*ClipY then ma:=ma+8;
if P.x> P.z*ClipX then ma:=ma+16;
if P.x<-P.z*ClipX then ma:=ma+32;
if (ma1 or ma2)=0 then
begin kq:=true;cohen3d:=true;if dao then begin p0:=p1;p1:=p2;p2:=p0;end;exit;end;
if (ma1 and ma2)<>0 then begin kq:=true;cohen3d:=false;exit;end;
Trang 19then begin giao(p[i+1],p[i],0,1,clipY,0,G);nq:=nq+1;Q[nq]:=G;end;
Trang 21{ if xmin>0 then xmin:=0;
if ymin>0 then ymin:=0;
if xmax<0 then xmax:=0;
if ymax<0 then ymax:=0;}
Trang 22for i:=0 to nx do for j:=0 to ny do
begin if (i mod nx<>0)and(j mod ny<>0)then
begin rx:=random(kc)-(kc div 2);ry:=random(kc)-(kc div 2);end else begin rx:=0;ry:=0;end;
Trang 23if tt<>0 then begin setcolor(14);setfillstyle(1,3);end
else begin setcolor(0);setfillstyle(1,0);end;
case (huong mod 5) of
1:begin mt[1,3]:=delta; dg[tt].x:=dg[tt].x+delta;end;
Trang 24ktmt2d(mt); mt[1,1]:=c;mt[2,1]:=s;mt[1,2]:=-s;mt[2,2]:=c; for j:=1 to 4 do biendoi2d(dg[tt].P[j],mt);
outtextxy(x0+(h div 2),y0-(v div 2),'HANH PHUC'); delay(2000);readln;
for i:=1 to nd do
for j:=1 to random(10) do
begin ve(0);tinhtien(i,random(i)+1);tinhtien(i,random(i)+2);quay(i,random(1)+1);ve(i);delay(100); end;
outtextxy(x0+(h div 2),y0-(v div 2),'Hanh phuc');
setcolor(15+blink);settextstyle(0,0,1);settextjustify(1,0); outtextxy(x0+(h div 2),y0,'Tiep tuc cai nhau(c/k)?:');End;
Trang 25#77:tinhtien(cur,1);
#71:if cur>1 then cur:=cur-1 else cur:=nd;
#79:if cur<nd then cur:=cur+1 else cur:=1;
Trang 26begin name:=ten;Phantu:=pt;Shortcut:=sc;next:=nil;Down:=nil; end;
if start=nil then start:=me else
{tim toi vi tri menu neu khong co thi tao noname}
while (Pmệnext<>nil)and(i<vt[0]) do
begin Pme:=Pmệnext;i:=i+1;end;
if i<vt[0] then nhapME(1,'Nonamé,sc2);
{tim toi Item can noi neu 0 co thi ghep Mi ngay duoi}
if Pmệdown=nil then Pmệdown:=mi else
Begin Pmi:=Pmệdown;
Repeat i:=1;
while (Pmi^.down<>nil)and(i<vt[j]) do
Trang 27While st[i]=' 'do begin delete(st,i,1);i:=i-1;end;
if j=0 then begin nhapME(0,st,sc2) end
else begin delete(st,1,j);NhapMI(st,tt,sc1,sc2,ma,vt);end;
Trang 28for i:=1 to l do
if st[i]='&'then begin moveto(getX,GetY+1);outtext('_');
moveto(getX-textwidth('_'),getY-1);i:=i-1;delete(st,i,1);end else outtext(st[i]);
While (mi<>nil)and(i<vt[j])do begin mi:=mi^.down;i:=i+1;end;
y0:=0;for i:=2 to deep do if vt[i]<>0 then y0:=y0+vt[i-1]-1;
Trang 29while vt[i]<>0 do i:=i+1;i:=i-1;
GetItem(vt,Smi,mi,x0,y0,le,pt);if tt=0 then tt:=mi^.att else tt:=2;
i:=1;
While (Pmi<>nil)do
Begin
if Pmi^.att=1 then setcolor(Tcolor)else setcolor(Hicolor);
outtextxy(x0+5,y0+i*H_button-H_button div 2-1,Pmi^.name);
Trang 30{ Khoi phuc lai nen ben duoi menu -}
begin Restore(i);for j:=1 to i do vt[j]:=cur[j];
for i:=i to deep do if cur[i]<>0 then begin vt[i]:=cur[i];show1menu(vt);end; end;
Begin showmenu(cursor); mamenu:=0;
hh:=deep+1; repeat hh:=hh-1 until cursor[hh]<>0;
GetItem(cursor,Pitem,Item,x0,y0,le,pt);pt:=pt-1;PTe:=1;
Pme:=start;
While Pmệnext<>nil do begin Pme:=Pmệnext;PTe:=PTe+1;end;
Repeat if keypressed then
Begin ch:=readkey;
case ch of
Trang 31for i:=2 to deep do cursor[i]:=0;cursor[1]:=1;
if cursor[0]>1 then cursor[0]:=cursor[0]-1 else cursor[0]:=PTe; GetItem(cursor,Pitem,Item,x0,y0,le,pt);pt:=pt-1;hh:=1;
for i:=2 to deep do cursor[i]:=0;cursor[1]:=1;
if cursor[0]<PTe then cursor[0]:=cursor[0]+1 else cursor[0]:=1; GetItem(cursor,Pitem,Item,x0,y0,le,pt);pt:=pt-1;
Trang 32begin ch:=readkey;if ch=#0 then
for i:=0 to deep do oldcur[i]:=0;
for i:=1 to deep do begin Po[i]:=nil;bk[i].x:=0;end;
Trang 33outtextxy(Xm+3,ym+vitri*H_button-H_button div 2-1,st);M_on;
End;
{ -}
Function MouseinME:byte;
Trang 34If MLP and(MouseinMI<>0)then begin
M_off;goimi(Me[Mcur]^.cursor,tt,stt);reszone;savezone(200,150,440,330); window(200,150,440,330,0,stt);menuflag:=false;M_on;delay(1000);end;
Trang 36aqs.y:=-a.x*hs2 - a.y*hs1*hs3 + a.z*hs4;
aqs.z:=-a.x*hs4*hs1 - a.y*hs3*hs2 - a.z*hs3 + R;
Trang 38dinh[7].x:=-50; dinh[7].y:=-30; dinh[7].z:=60; dinh[8].x:=50; dinh[8].y:=-30; dinh[8].z:=60;
dinh[9].x:=60; dinh[9].y:=0; dinh[9].z:=80;
dinh[10].x:=-60; dinh[10].y:=0; dinh[10].z:=80;
dinh[11].x:=60; dinh[11].y:=40; dinh[11].z:=60; dinh[12].x:=-60; dinh[12].y:=40; dinh[12].z:=60; dinh[13].x:=-60; dinh[13].y:=-40; dinh[13].z:=60; dinh[14].x:=60; dinh[14].y:=-40; dinh[14].z:=60; dinh[15].x:=10; dinh[15].y:=30; dinh[15].z:=0; dinh[16].x:=-10; dinh[16].y:=30; dinh[16].z:=0; dinh[17].x:=-10; dinh[17].y:=30; dinh[17].z:=40; dinh[18].x:=10; dinh[18].y:=30; dinh[18].z:=40;
dinh[19].x:=30; dinh[19].y:=30; dinh[19].z:=15; dinh[20].x:=20; dinh[20].y:=30; dinh[20].z:=15; dinh[21].x:=20; dinh[21].y:=30; dinh[21].z:=40; dinh[22].x:=30; dinh[22].y:=30; dinh[22].z:=40;
dinh[23].x:=-20; dinh[23].y:=30; dinh[23].z:=15; dinh[24].x:=-30; dinh[24].y:=30; dinh[24].z:=15; dinh[25].x:=-30; dinh[25].y:=30; dinh[25].z:=40; dinh[26].x:=-20; dinh[26].y:=30; dinh[26].z:=40; dinh[27].x:=-25; dinh[27].y:=30; dinh[27].z:=15; dinh[28].x:=-25; dinh[28].y:=30; dinh[28].z:=40; dinh[29].x:=-30; dinh[29].y:=30; dinh[29].z:=27; dinh[30].x:=-20; dinh[30].y:=30; dinh[30].z:=27;
dinh[31].x:=25; dinh[31].y:=30; dinh[31].z:=15; dinh[32].x:=25; dinh[32].y:=30; dinh[32].z:=40; dinh[33].x:=20; dinh[33].y:=30; dinh[33].z:=27; dinh[34].x:=30; dinh[34].y:=30; dinh[34].z:=27; c[1].d1:=1; c[1].d2:=2;
Trang 39c[9].d1:=8; c[9].d2:=5; c[10].d1:=4; c[10].d2:=8; c[11].d1:=2; c[11].d2:=6; c[12].d1:=3; c[12].d2:=7; c[13].d1:=9; c[13].d2:=14; c[14].d1:=9; c[14].d2:=11; c[15].d1:=9; c[15].d2:=10; c[16].d1:=10; c[16].d2:=13; c[17].d1:=10; c[17].d2:=12; c[18].d1:=11; c[18].d2:=12; c[19].d1:=12; c[19].d2:=13; c[20].d1:=13; c[20].d2:=14; c[21].d1:=14; c[21].d2:=11;
c[22].d1:=15; c[22].d2:=18; c[23].d1:=17; c[23].d2:=18; c[24].d1:=17; c[24].d2:=16; c[25].d1:=19; c[25].d2:=20; c[26].d1:=20; c[26].d2:=21; c[27].d1:=21; c[27].d2:=22; c[28].d1:=22; c[28].d2:=19;
c[29].d1:=23; c[29].d2:=24; c[30].d1:=24; c[30].d2:=25; c[31].d1:=25; c[31].d2:=26; c[32].d1:=26; c[32].d2:=23; c[33].d1:=27; c[33].d2:=28; c[34].d1:=29; c[34].d2:=30; c[35].d1:=31; c[35].d2:=32; c[36].d1:=33; c[36].d2:=34; for i:=1 to 36 do
Begin
diem(dinh[c[i].d1]); veden(dinh[c[i].d2]); end;
end;
Trang 41Procedure PolyZone(tt,color,n:byte;Mp:array of pointtype);
Procedure Savezone(x1,y1,x2,y2:integer;var x,y:integer;var Po:pointer);Procedure Reszone(x,y:integer;P:Pointer);
Procedure SaveBack(x1,y1,x2,y2:integer);
Procedure ResBack;
Procedure ButtonText(x,y,le:integer;tt:byte;st:string);
Procedure AddButton(x,y,le:integer;tt:byte;name:string;key:byte);Procedure DrawallButton;
Procedure Readstr(x,y,le:integer;tt:byte;var xau:string);
Procedure Readval(x,y,le:integer;tt:byte;var so:real);
Procedure ReadInt(x,y,le:integer;tt:byte;var so:Integer);
Procedure Readfilename(t:byte;var xau:string);
Trang 42SetlineStyle(Oline.linestyle,Oline.pattern,Oline.thickness);
SettextStyle(Otext.font,Otext.Direction,Otext.charsize);
Settextjustify(Otext.horiz,Otext.vert);
Trang 43setcolor(black);line(x2,y2,x2,y1);line(x2,y2,x1,y2);
x1:=x1-1;y1:=y1-1;x2:=x2+1;y2:=y2+1;end;
3:begin x1:=x1+x2;x2:=x1-x2;x1:=x1-x2;y1:=y1+y2;y2:=y1-y2;y1:=y1-y2;end; 4:begin setcolor(0);line(x1,y1,x2,y1);line(x1,y1,x1,y2);
if tt=0 then setcolor(Black)else setcolor(color-8)
else if tt=1 then setcolor(Black)else setcolor(color-8);
Trang 44Procedure Savezone(x1,y1,x2,y2:integer;var x,y:integer;var Po:pointer);Begin M_Off; X:=x1;Y:=y1;
Trang 46Function MIBT(x,y,le:integer;tt:byte;st:string):boolean;
var xm,ym:integer;
Begin MIBT:=false;
if MLP and M_IN(x,y+(tt div 10),x+le,y+H_Button-(tt div 10)) then
begin ButtonText(x,y,le,tt-(tt mod 10)+1,st);repeat until MLR;
if M_IN(x,y+(tt div 10),x+le,y+H_Button-(tt div 10)) then begin
Trang 47While (P<>nil)and(P^.key<>key) do P:=P^.next;
if P^.key=key then ButtonText(P^.x,P^.y,P^.le,P^.tt,P^.name);
Trang 48if S_back=P then begin S_back:=nil; C_back:=nil;end else
While C_back^.next<>P do C_back:=C_back^.next;
Trang 49setfillstyle(1,15);bar(x-112,y-12,x-98,y-8);setcolor(Tcolor); i:=35;
while st[i]<>' 'do i:=i-1;st1:=copy(st,1,i-1);delete(st,1,i); outtextxy(x-80,y-10,st1); outtextxy(x-80,y-10+H_button,st); SetownInfo;Buttontext(X-25,Y+25,50,0,'OK');
repeat until keypressed;
Trang 52Begin M_In:=false; getviewsettings(vp);
GMP(x,y);x:=x-vp.x1;y:=y-vp.y1;
if (x>=x1)and(x<=x2)and(y>=y1)and(y<=y2)then M_In:=true;End;
Trang 55for i:= 0 to k do san[i]:=san[i+1];
if tt<>0 then setfillstyle(1,2)else setfillstyle(9,0);fillpoly(k,san);
for i:=1 to nl do
begin if tt<>0 then setcolor(L[i].color)else setcolor(0);
if PerClipLine(P[L[i].s],P[L[i].e],q1,q2) then doan(q1,q2);
P[5].x:=vt.x+0.4;P[5].y:=vt.y-0.2;P[5].z:=vt.z+0.9;
P[6].x:=vt.x+0.3;P[6].y:=vt.y+0.2;P[6].z:=vt.z;
P[7].x:=vt.x-0.3;P[7].y:=vt.y-0.2;P[7].z:=vt.z;
if PerClipLine(P[1],P[3],q1,q2) then doan(q1,q2);
if PerClipLine(P[4],P[2],q1,q2) then doan(q1,q2);
Trang 56if PerClipLine(P[5],P[2],q1,q2) then doan(q1,q2);
if PerClipLine(P[6],P[3],q1,q2) then doan(q1,q2);
if PerClipLine(P[7],P[3],q1,q2) then doan(q1,q2);
#71:begin VPC(0);if g2>0 then g2:=g2-5 else g2:=360;VPC(1);end;
#79:begin VPC(0);if g2<360 then g2:=g2+5 else g2:=0;VPC(1);end;
#83:begin VPC(0);if g1<360 then g1:=g1+5 else g1:=0;VPC(1);end;
#81:begin VPC(0);if g1>0 then g1:=g1-5 else g1:=360;VPC(1);end;
Trang 57if x1>x2 then begin i:=x1;x1:=x2;x2:=i;end;
if y1>y2 then begin i:=y1;y1:=y2;y2:=i;end;
Trang 58if x2<x1 then begin a:=x2;x2:=x1;x1:=a;a:=y2;y2:=y1;y1:=a;end;
if y2<y1 then i:=-1 else i:=1;
A:=2*abs(y2-y1);B:=A-2*abs(x2-x1);P:=2*abs(y2-y1)-abs(x2-x1); x:=x1;y:=y1;if j<0 then vediem(y,x,color)else vediem(x,y,color); while x<x2 do
begin
if P<0 then begin x:=x+1;P:=P+A;end
else begin x:=x+1;y:=y+i;P:=P+B;end;
if j<0 then vediem(y,x,color) else vediem(x,y,color);
Trang 59program Thuat_toan_BresenhamCircle;
uses graph;
var gd,gm:integer;
Thu tuc ve tam diem tam (x1,y1)}
Trang 60uses graph;
var gd,gm:integer;
Thu tuc ve tam diem tam (x1,y1)}
Trang 61program Thuat_toan_RoundCircle;
uses graph;
var gd,gm:integer;
{ -}{Thu tuc ve duong tron}
gd:=0;
initgraph(gd,gm,'c:\bp\bgi');
Trang 62{ -procedure cuaso(var xmin,ymin,xmax,ymax:integer);
{ -procedure toado(var a1,b1,a2,b2:real);
Trang 63Thu tuc ve doan thang trong truong hop song song voi canh cua so}procedure vesongsong(x1,y1,x2,y2:real);
if y1 <ymin then y1:=ymin;
if y2>ymax then y2:= ymax;
if x1 <xmin then x1:=xmin;
if x2>xmax then x2:= xmax;
{ -procedure kod(x,y:real;var b:code);
Trang 64if (y>ymax) then b[4]:=1;
end;
Thu tuc clipping}
if b[2]=1 then
begin
y1:=y1+(xmax-x1)*(y2-y1)/(x2-x1);x1:=xmax; end;
Trang 65if b[3]=1 then
begin
x1:=x1+(ymin-y1)*(x2-x1)/(y2-y1);y1:=ymin; end;
if b[4]=1 then
begin
x1:=x1+(ymax-y1)*(x2-x1)/(y2-y1);y1:=ymax; end;
{ -procedure Dongdohoa;
begin
readln;
closegraph;
Trang 66Thu tuc nhap vao cua so cat}
{ -procedure cuaso(var xmin,ymin,xmax,ymax:integer);
{ -procedure toado(var a1,b1,a2,b2:real);
if y1 <ymin then y1:=ymin;
if y2>ymax then y2:= ymax;
Trang 67end;
if (x1<xmax) and (x2>xmin) then
begin
if x1 <xmin then x1:=xmin;
if x2>xmax then x2:= xmax;
end;
Chuong trinh chinh}