TRANSFORMASI V.3

uses graph,crt;
type tmatriks = array[1..3,1..3] of real;
tvektor  = array[1..3] of real;
ttitik   = array[1..4] of real;
const hidup = true;
mati = false;
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];
lebar,tinggi : byte;


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 Kursor(x,y : word;status : boolean);
begin
If status then setfillstyle(solidfill,white)
else setfillstyle(emptyfill,white);
bar(x,y-2,x+lebar-1,y+tinggi);
end;

function Greadln(x,y : word) : string;
var c,cl : char;
posisi,panjang : integer;
kata : string;
begin
kursor(x,y,hidup);
posisi := 1;       { posisi kursor awal }
panjang := 0;      { panjang untai/kata awal }
kata := ”;        { untai/kata awal }
{ membaca tombol sampai tombol enter ditekan }
repeat
c := readkey;
case c of
#00 : begin           { extended key ditekan }
cl := readkey;
if cl = #75 then begin
kursor(x,y,mati);
kursor(x-lebar,y,hidup);
setcolor(getbkcolor);
outtextxy(x-lebar,y,kata[posisi-1]);
if posisi < panjang+1 then begin
setcolor(white);
outtextxy(x,y,kata[posisi]);
end;
dec(x,lebar);
dec(posisi);
end
else if cl = #77 then begin
if (posisi < panjang+1) then begin
kursor(x,y,mati);
setcolor(white);
outtextxy(x,y,kata[posisi]);
kursor(x+lebar,y,hidup);
setcolor(getbkcolor);
if posisi < panjang then outtextxy(x+lebar,y,kata[posisi+1]);
inc(x,lebar);
inc(posisi);
end;
end;
end;
#08 : begin
kursor(x,y,mati);
setcolor(getbkcolor);
outtextxy(x-lebar,y,kata[panjang]);
setcolor(white);
kursor(x-lebar,y,hidup);
dec(x,lebar);
dec(panjang);
dec(posisi);
end;
else begin
kursor(x,y,mati);
outtextxy(x,y,c);
kursor(x+lebar,y,hidup);
inc(x,lebar);
inc(panjang);
inc(posisi);
kata := kata + c;
end;
end;
until c = #13;
end;

procedure GWriteln(x,y : word;numkar : real;lebar,desimal : byte);
var s : string;
begin
str(numkar:lebar:desimal,s);
outtextxy(x,y,s);
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,code : integer;
kata : array[1..10] of string;
begin
InisialisasiViewport;

setwritemode(xorput);
settextstyle(defaultfont,horizdir,1);
settextjustify(lefttext,toptext);
lebar := textwidth(‘H’);
tinggi := textheight(‘H’);

kata[1] := Greadln(100,50);
kata[2] := Greadln(100,100);
kata[3] := Greadln(100,150);
kata[4] := Greadln(100,200);
kata[5] := Greadln(100,250);
kata[6] := Greadln(100,300);
kata[7] := Greadln(100,350);
kata[8] := Greadln(100,400);
val(kata[1],Xw[1],code);
val(kata[2],Yw[1],code);
val(kata[3],Xw[2],code);
val(kata[4],Yw[2],code);
val(kata[5],Xw[3],code);
val(kata[6],Yw[3],code);
val(kata[7],Xw[4],code);
val(kata[8],Yw[4],code);

{ 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;

{ prosedur untuk menghidupmatikan kursor }

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

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

{ 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 : ‘);
{ letakkan titik-titiik poligon disini }
j := 115;
n := 10;
case n of
1..6 :  for i := 1 to n do begin
outtextxy(485,j,'(   ,   )’);
inc(j,20);
end;
7..10 : begin
for i := 1 to 6 do begin
outtextxy(485,j,'(   ,   )’);
inc(j,20);
end;
j := 115;
for i := 1 to n-6 do begin
outtextxy(565,j,'(   ,   )’);
inc(j,20);
end;
end;
end;

{ 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(500,270,’É ‘);  { duruuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuung}
outtextxy(500,280,’f ‘);
outtextxy(500,290,’É ‘);
outtextxy(500,300,’É ‘);
outtextxy(500,310,’É ‘);

{ 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: