Đồ họa máy tính - Chương 2: Công cụ đồ họa của Turbo Pascal
VAR
gd,gm:integer;
canh,buoc,direct,x,y,d:integer;
ch:char;
Procedurehop(x,y:integer);
begin
rectangle(x-canh,y-canh,x+canh,y+canh);delay(d);
rectangle(x-canh,y-canh,x+canh,y+canh);
end;
BEGIN
gd:=detect; initgraph(gd,gm,'c:\tp\bgi');
x:=100;y:=100; setwritemode(xorput);
d:=50; direct:=1; buoc:=2; canh:=10;
Repeat
repeat
hop(x,y);
if(0<x+direct*buoc) and (x+direct*buoc<GetmaxX) then
x:=x+direct*buoc elsedirect:=direct*(-1);
untilKeypressed;
ch:=readkey;
ifch=#0 then
begin
ch:=readkey;
case ch of
#72: if y>10 then y:=y-10;
#80: if y<getmaxY-10 then y:=y+10;
#75: if direct=1 then
begin if buoc>1 then dec(buoc); { hãm phanh }
end
else { direct=-1} inc(buoc); { tăng tốc }
#77: if direct=-1 then
begin if buoc>1 then dec(buoc); { hãm phanh }
end
else inc(buoc); { tăng tốc }
end;
end;
Untilch=#27;
closegraph;
END.
ạo các tham số của cửa sổ và khung nhìn *)
procedure khoitao_cuaso;
begin
xo := 20; yo := 40; w := 600; h := 400;
xtl := 50; ytl := 50; {chọn trước tỉ lệ ngang và dọc, cho bằng nhau}
x2 := w/xtl/2; x1 := -x2; {căn cửa sổ để tâm là gốc toạ độ}
y2 := h/ytl/2; y1 := -y2;
end;
{Các hàm chuyển toạ độ thực thành toạ độ màn hình}
function tox(x : real): integer;
begin
tox := xo + round((x-x1)*xtl);
end;
function toy(y : real): integer;
begin
toy := yo + round((y2-y)*ytl);
end;
{Thủ tục vẽ đoạn thẳng trong mặt phẳng thực 2D}
procedure Line2D(x1,y1,x2,y2: real);
begin
Line(tox(x1),toy(y1),tox(x2),toy(y2));
end;
{Thủ tục vẽ trục toạ độ}
Đồ họa máy tính - Khoa CNTT - ĐHSPHN
31
procedure vetruc;
var i : integer;
x : string;
begin
Line2D(x1,0,x2,0); {vẽ trục x}
Line2D(x2-0.2,+0.1,x2,0); {vẽ hình mũi tên trên trục x}
Line2D(x2-0.2,-0.1,x2,0);
Line2D(0,y1,0,y2); {vẽ trục y}
Line2D(+0.1,y2-0.2,0,y2); {vẽ hình mũi tên trên trục y}
Line2D(-0.1,y2-0.2,0,y2);
SetColor(lightgreen);
SetTextJustify(1,1);
for i := round(x1) + 1 to round(x2) - 1 do {vạch và ghi toạ độ trên trục x}
if i 0 then begin
str(i,x); {vạch i nguyên, chuyển số thành chữ}
outtextxy(tox(i),toy(-0.4),x); {để vẽ chữ lên màn hình}
Line2D(i,-0.1,i,0.1); {vẽ 1 đoạn thẳng làm vạch}
end;
for i := round(y1) + 1 to round(y2) - 1 do
if i 0 then begin
str(i,x);
outtextxy(tox(-0.4),toy(i),x);
Line2D(-0.1,i,0.1,i);
end;
end;
{Vẽ đường cong hàm số y = sinx, x = x1 -> x2}
procedure duongsin;
var x,y,a,b,dx : real;
i,n : integer;
begin
setcolor(yellow);
a := x1; b := x2; n := 100; dx := (b-a)/n;
x := a; y := sin(x);
moveto(tox(x),toy(y)); {dùng 2 hàm toX, toY để chuyển toạ độ thực thành toạ độ màn
hình}
for i := 1 to n do begin
x := x + dx; y := sin(x);
lineto(tox(x),toy(y));
end;
end;
{Vẽ đường cong tham số y = cos3t, y = sin5t, t= 0.. 2pi}
procedure thamso;
var t,x,y,a,b,dt : real;
i,n : integer;
begin
setcolor(lightred);
a := 0; b := 2*pi; n := 100; dt := (b-a)/n;
Đồ họa máy tính - Khoa CNTT - ĐHSPHN
32
t := a; x := cos(3*t) ; y := sin(5*t);
moveto(tox(x),toy(y));
for i := 1 to n do begin
t := t + dt; x := cos(3*t) ; y := sin(5*t);
lineto(tox(x),toy(y));
end;
end;
{Vẽ đường cong toạ độ cực r = 3cos3p, p= 0 .. 2pi}
procedure tdcuc;
var r,p,x,y,a,b,dp : real;
i,n : integer;
begin
setcolor(13);
a := 0; b := 2*pi; n := 100; dp := (b-a)/n;
p := a; r := 3*cos(3*p);
x := r*cos(p) ; y := r*sin(p); {công thức đổi toạ độ cực sang toạ độ Đề các 2D}
moveto(tox(x),toy(y));
for i := 1 to n do begin
p := p + dp; r := 3*cos(3*p); x := r*cos(p) ; y := r*sin(p);
lineto(tox(x),toy(y));
end;
end;
BEGIN
gd := 0; initgraph(gd,gm,'C:\TP\BGI');
khoitao_cuaso;
vetruc;
duongsin;
thamso;
tdcuc;
readkey;
END.
Ví dụ: MINH HOẠ CÁC PHÉP BIẾN ĐỔI 2D
uses crt,graph;
var gd, gm, xo, yo, w, h, n : integer;
x1,y1,x2,y2, xtl, ytl : real;
x,y : array[1..4] of real;
procedure khoitao_cuaso;
begin
gd := 0; initgraph(gd,gm,'C:\TP\BGI');
xo := 20; yo := 40; w := 600; h := 400;
xtl := 30; ytl := 30;
x2 := w/xtl/2; x1 := -x2;
y2 := h/ytl/2; y1 := -y2;
end;
Đồ họa máy tính - Khoa CNTT - ĐHSPHN
33
function tox(x : real): integer;
begin
tox := xo + round((x-x1)*xtl);
end;
function toy(y : real): integer;
begin
toy := yo + round((y2-y)*ytl);
end;
procedure Line2D(x1,y1,x2,y2: real);
begin
Line(tox(x1),toy(y1),tox(x2),toy(y2));
end;
procedure vetruc;
var i : integer;
x : string;
begin
Line2D(x1,0,x2,0);
Line2D(x2-0.2,+0.1,x2,0);
Line2D(x2-0.2,-0.1,x2,0);
Line2D(0,y1,0,y2);
Line2D(+0.1,y2-0.2,0,y2);
Line2D(-0.1,y2-0.2,0,y2);
SetColor(lightgreen);
SetTextJustify(1,1);
for i := round(x1) + 1 to round(x2) - 1 do
if i 0 then begin
str(i,x);
outtextxy(tox(i),toy(-0.4),x);
Line2D(i,-0.1,i,0.1);
end;
for i := round(y1) + 1 to round(y2) - 1 do
if i 0 then begin
str(i,x);
outtextxy(tox(-0.4),toy(i),x);
Line2D(-0.1,i,0.1,i);
end;
end;
(* Khởi tạo toạ độ các đỉnh hình chữ nhật*)
procedure khoitao_hcn;
begin
n := 4;
x[1] := 1; y[1] := 1;
x[2] := 1; y[2] := 3;
x[3] := 5; y[3] := 3;
x[4] := 5; y[4] := 1;
Đồ họa máy tính - Khoa CNTT - ĐHSPHN
34
end;
(*Thủ tục vẽ hình chữ nhật*)
procedure VeHCN(c : integer);
begin
SetColor(c);
MoveTo(tox(x[1]), toy(y[1]));
LineTo(tox(x[2]), toy(y[2]));
LineTo(tox(x[3]), toy(y[3]));
LineTo(tox(x[4]), toy(y[4]));
LineTo(tox(x[1]), toy(y[1]));
end;
(* Tịnh tiến HCN theo vector (a,b) *)
procedure tinhtien(a,b : real);
var i : integer;
begin
for i := 1 to n do begin
x[i] := x[i] + a;
y[i] := y[i] + b;
end;
end;
(* Tịnh tiến HCN theo tỉ lệ (Sx,Sy) *)
procedure codan(Sx,Sy : real);
var i : integer;
begin
for i := 1 to n do begin
x[i] := Sx*x[i];
y[i] := Sy*y[i];
end;
end;
(* Tịnh tiến HCN theo góc a, đo bằng radian *)
procedure Quay(a : real);
var i : integer;
cosa,sina,tx,ty : real;
begin
cosa := cos(a); sina := sin(a);
for i := 1 to n do begin
tx := x[i] * cosa - y[i] * sina;
ty := x[i] * sina + y[i] * cosa;
x[i] := tx; y[i] := ty;
end;
end;
BEGIN
khoitao_cuaso;
khoitao_hcn;
vetruc;
VeHCN(14);
readkey;
tinhtien(-3,-2); {tịnh tiến HCN để tâm HCN là gốc toạ độ}
Đồ họa máy tính - Khoa CNTT - ĐHSPHN
35
VeHCN(13);
readkey;
codan(1.2,2.4); {co dãn để HCN trở thành hình vuông :D}
VeHCN(12);
readkey;
quay(pi/4); {Quay HCN góc pi/4}
VeHCN(11);
readkey;
END.
Ví dụ: Vẽ các đường sin quay quanh gốc tọa độ
uses crt,graph;
var gd, gm, xo, yo, w, h : integer;
x1,y1,x2,y2, xtl, ytl : real;
(* CÁC THỦ TỤC HỆ ĐỒ HOẠ 2D *)
procedure khoitao_cuaso;
begin
xo := 20; yo := 40; w := 600; h := 400;
xtl := 30; ytl := 30;
x2 := w/xtl/2; x1 := -x2;
y2 := h/ytl/2; y1 := -y2;
end;
function tox(x : real): integer;
begin
tox := xo + round((x-x1)*xtl);
end;
function toy(y : real): integer;
begin
toy := yo + round((y2-y)*ytl);
end;
procedure Line2D(x1,y1,x2,y2: real);
begin
Line(tox(x1),toy(y1),tox(x2),toy(y2));
end;
procedure vetruc;
var i : integer;
x : string;
begin
SetColor(14);
Line2D(x1,0,x2,0);
Line2D(x2-0.2,+0.1,x2,0);
Line2D(x2-0.2,-0.1,x2,0);
Line2D(0,y1,0,y2);
Line2D(+0.1,y2-0.2,0,y2);
Line2D(-0.1,y2-0.2,0,y2);
SetColor(lightgreen);
SetTextJustify(1,1);
for i := round(x1) + 1 to round(x2) - 1 do
Đồ họa máy tính - Khoa CNTT - ĐHSPHN
36
if i 0 then begin
str(i,x);
outtextxy(tox(i),toy(-0.4),x);
Line2D(i,-0.1,i,0.1);
end;
for i := round(y1) + 1 to round(y2) - 1 do
if i 0 then begin
str(i,x);
outtextxy(tox(-0.4),toy(i),x);
Line2D(-0.1,i,0.1,i);
end;
end;
{Vẽ đường sin y = sinx, x = -pi -> pi, quay 1 góc}
procedure quay(x,y,a:real; var tx,ty : real);
begin
tx := x * cos(a) - y * sin(a);
ty := x * sin(a) + y * cos(a);
end;
procedure duongsin(goc : real);
var x,y,a,b,dx,tx,ty : real;
i,n : integer;
begin
setcolor(15);
a := -2*pi; b := 2*pi; n := 50; dx := (b-a)/n;
x := a; y := sin(x); quay(x,y,goc,tx,ty);
moveto(tox(tx),toy(ty));
for i := 1 to n do begin
x := x + dx; y := sin(x); quay(x,y,goc,tx,ty);
lineto(tox(tx),toy(ty));
end;
end;
BEGIN
gd := 0; initgraph(gd,gm,'C:\TP\BGI');
khoitao_cuaso;
vetruc;
duongsin(0);
duongsin(pi/4);
duongsin(pi/2);
duongsin(3*pi/4);
readkey;
END.
Ví dụ: Vẽ bầu trời sao
Uses crt,graph;
Var
gd,gm:integer;
Đồ họa máy tính - Khoa CNTT - ĐHSPHN
37
s:array[1..100] of pointType;
n,i,j:integer;
procedure star(a,b:integer); {một ngôi sao lóe sáng}
var i:integer;
begin
for i:=2 to 10 do
begin
setcolor(random(GetMaxColor));
circle(a,b,i); delay(100);
setcolor(0); circle(a,b,i);
end;
end;
Begin
gd:=detect;
initgraph(gd,gm,'c:\tp\bgi');
setcolor(Yellow); n:=100;
Randomize;
for i:=1 to n do {n ngôi sao lần lượt hiện lên}
begin
delay(50);
s[i].x:=random(GetmaxX);
s[i].y:=random(GetmaxY);
putpixel(s[i].x,s[i].y,yellow);
end;
i:=0;j:=0;
{các ngôi sao lần lượt lóe sáng}
repeat
i:=i mod n +1; j:=j mod 10 +1;
if j 10 then putpixel(s[i].x,s[i].y,0)
else star(s[i].x,s[i].y);
delay(50);
s[i].x:=random(GetmaxX);
s[i].y:=random(GetmaxY);
putpixel(s[i].x,s[i].y,yellow);
until keypressed;
closeGraph;
End.
Ví dụ: Vẽ hộp chuyển động theo sự điều khiển của người dùng
{ Hộp vuông chuyển động theo phương ngang, chạm vào 2 biên và nảy ngược lại.
Nếu người dùng bấm phím mũi tên cùng chiều với chiều chuyển động của hộp thì hộp tăng
tốc độ, ngược lại sẽ giảm tốc độ.
Bấm phím mũi tên ÈÇ : quỹ đạo hộp sẽ được hạ xuống / nâng lên }
Uses crt,graph;
Đồ họa máy tính - Khoa CNTT - ĐHSPHN
38
VAR
gd,gm:integer;
canh,buoc,direct,x,y,d:integer;
ch:char;
Procedure hop(x,y:integer);
begin
rectangle(x-canh,y-canh,x+canh,y+canh);delay(d);
rectangle(x-canh,y-canh,x+canh,y+canh);
end;
BEGIN
gd:=detect; initgraph(gd,gm,'c:\tp\bgi');
x:=100;y:=100; setwritemode(xorput);
d:=50; direct:=1; buoc:=2; canh:=10;
Repeat
repeat
hop(x,y);
if (0<x+direct*buoc) and (x+direct*buoc<GetmaxX) then
x:=x+direct*buoc
else direct:=direct*(-1);
until Keypressed;
ch:=readkey;
if ch=#0 then
begin
ch:=readkey;
case ch of
#72: if y>10 then y:=y-10;
#80: if y<getmaxY-10 then y:=y+10;
#75: if direct=1 then
begin if buoc>1 then dec(buoc); { hãm phanh }
end
else { direct=-1} inc(buoc); { tăng tốc }
#77: if direct=-1 then
begin if buoc>1 then dec(buoc); { hãm phanh }
end
else inc(buoc); { tăng tốc }
end;
end;
Until ch=#27;
closegraph;
END.
File đính kèm:
Đồ họa máy tính - Chương 2_Công cụ đồ họa của Turbo Pascal.pdf

