TRANSFORMASI V.2

uses graph,crt;
type tmatriks = array[1..3,1..3] of real;
tvektor  = array[1..3] of real;
ttitik   = array[1..4] of real;
var gd,gm,i,j,counter,n     : integer;
XVmin,XVmax,
YVmin,YVmax,
XWmin,XWmax,
YWmin,YWmax   : integer;
ms            : tmatriks;
vek,vekhasil  : tvektor;
Xw,Yw,Xv,Yv   : ttitik;
s             : string[7];

procedure BuatKotak(x1,y1,x2,y2 : integer; warnakotak,warnabatas : word);
begin
setcolor(warnabatas);   {menentukan warna batas }
rectangle(x1,y1,x2,y2);   { membuat kotak }
setfillstyle(solidfill,warnakotak);   { menentukan warna kotak }
floodfill((x1+x2) div 2,(y1+y2) div 2,warnabatas);   { proses pewarnaan kotak }
end;

procedure MatriksSebarang(var Ms : TMatriks; sx1,sx2,sx3,sy1,sy2,sy3 : real);
begin
{ matriks transformasi }
Ms[1,1] := sx1;Ms[1,2] := sy1;Ms[1,3] := 0;
Ms[2,1] := sx2;Ms[2,2] := sy2;Ms[2,3] := 0;
Ms[3,1] := sx3;Ms[3,2] := sy3;Ms[3,3] := 1;
end;

Procedure InisialisasiViewport;
begin
{ transformasi window ke viewport }
XVmin := 1;XVmax := 479;   { titik-titik batas viewport }
YVmin := 43;YVmax := 479;

XWmin := -10;XWmax := 10;  { titik-titik batas window }
YWmin := -10;YWmax := 10;
end;

procedure GambarPoligon(var Xv,Yv : TTitik; X1,Y1,X2,Y2,X3,Y3,X4,Y4 : real;
warna : word);
var i : integer;
begin
InisialisasiViewport;

{ titik-titik poligon }
Xw[1] := x1;Xw[2] := x2;Xw[3] := x3;Xw[4] := x4;
Yw[1] := y1;Yw[2] := y2;Yw[3] := y3;Yw[4] := y4;

{ warna poligon }
setcolor(warna);

{ rumus mwnentukan letak titik-titik x dan y di viewport }
for i := 1 to 4 do begin
Xv[i] := XVmin + (Xw[i]-XWmin)/(XWmax-XWmin)*(XVmax-XVmin);
Yv[i] := YVmax – (Yw[i]-YWmin)/(YWmax-YWmin)*(YVmax-YVmin);
end;

{ menggambar garis-garis poligon }
for i := 1 to 4 do begin
if i = 4 then line(round(Xv[i]),round(Yv[i]),round(Xv[i-3]),round(Yv[i-3]))
else line(round(Xv[i]),round(Yv[i]),round(Xv[i+1]),round(Yv[i+1]));
end;
end;

procedure KerjakanTransformasi;
var c : char;
begin
repeat
c := readkey;
until c = #27;
end;

begin { program utama }
{inisialisasi grafik }
gd := detect;
initgraph(gd,gm,’c:\tpascal7\bgi’);

{ judul }
BuatKotak(1,1,639,40,brown,white);
settextjustify(centertext,centertext);
settextstyle(triplexfont,horizdir,3);
outtextxy(320,15,’T R A N S F O R M A S I   S E B A R A N G’);

BuatKotak(1,43,479,479,blue,white);   { window tempat penggambaran obyek transformasi }

{ window pendefinisian poligon }
BuatKotak(482,43,639,65,magenta,white);
settextjustify(centertext,centertext);
settextstyle(defaultfont,horizdir,1);
outtextxy((482+639) div 2,55,’Poligon ‘);

BuatKotak(482,65,639,85,cyan,white);
settextjustify(lefttext,centertext);
settextstyle(defaultfont,horizdir,1);
outtextxy(485,75,’Segi :   (maks 10)’);

BuatKotak(482,85,639,227,cyan,white);
settextjustify(lefttext,centertext);
settextstyle(defaultfont,horizdir,1);
outtextxy(485,95,’Titiký Poligon : ‘);

{ window tempat modifikasi matriks transformasi }
BuatKotak(482,230,639,250,magenta,white);
settextjustify(lefttext,centertext);
settextstyle(defaultfont,horizdir,1);
outtextxy(485,240,’Matriks : ‘);

BuatKotak(482,250,639,347,cyan,white);
settextjustify(centertext,centertext);
settextstyle(defaultfont,horizdir,1);
outtextxy(320,15,’ ‘);  { duruuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuung}

{ window keterangan tombol kendali }
BuatKotak(482,350,639,370,darkgray,white);
settextjustify(lefttext,centertext);
settextstyle(defaultfont,horizdir,1);
outtextxy(485,360,’Tombol Kendali : ‘);

s := chr(27) +’,’+chr(26)+’,’+chr(24)+’,’+chr(25);
BuatKotak(482,370,639,479,lightgray,white);
settextjustify(lefttext,centertext);
settextstyle(defaultfont,horizdir,1);
outtextxy(485,380,”+s+’ : geser ‘);
outtextxy(565,395,’layar’);
outtextxy(485,415,’Tab     : pindah ‘);
outtextxy(565,430,’window’ );
outtextxy(485,449,’Enter   : eksekusi’);
outtextxy(485,469,’Esc     : keluar’);

{ membuat sumbu x dan y }
for i := 50 to 475 do putpixel((1+479) div 2,i,lightgray);
for i := 8 to 475 do putpixel(i,(43+479) div 2,lightgray);

matriksSebarang(Ms,-1,0,0,0,-1,0);

GambarPoligon(Xv,Yv,1,1,3,1,3,3,1,3,red);

{ perkalian matriks untuk transformasi poligon }
for n := 1 to 4 do begin
for i := 1 to 3 do begin
vekhasil[i] := 0;
counter := 1;
for j := 1 to 3 do begin
if counter = 1 then vek[j] := Xw[n] else
if counter = 2 then vek[j] := Yw[n] else
If counter = 3 then vek[j] := 1;
vekhasil[i] := vekhasil[i] + vek[j] * Ms[j,i];
inc(counter);
end;
end;
Xw[n] := vekhasil[1];
Yw[n] := vekhasil[2];
end;

gambarPoligon(xv,yv,Xw[1],Yw[1],Xw[2],Yw[2],Xw[3],Yw[3],Xw[4],Yw[4],yellow);

KerjakanTransformasi;
closegraph;
end.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: