Praktikum kemarin mempelajari beberapa cara untuk memanipulasi obyek grafis dan system koordinat. Ada dua cara untuk melakukan transformasi yaitu transformasi obyek dan transformasi koordinat.
Jenis - jenis Transformasi :
- Translation
- Scaling
- Rotation
- Searing
//-----------------------------------------------------//
#include (vcl.h)
#include (math.h)
#pragma hdrstop
#include "Unit1.h"
//-----------------------------------------------------//
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
struct Elemen { float x,y ;} ;
Elemen Objek[6];
//-----------------------------------------------------//
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//-----------------------------------------------------//
void __fastcall TForm1::FormShow(TObject *Sender)
{ int i;
Image1->Canvas->Rectangle(0,0,Image1->Width,Image1->Height);
Image1->Canvas->MoveTo(Objek[5].x,Objek[5].y);
for (i=1;i<=5;i++){ Image1->Canvas->LineTo(Objek[i].x,Objek[i].y);};
}
//-----------------------------------------------------//
void __fastcall TForm1::FormActivate(TObject *Sender)
{
Objek[1].x = 100; Objek[1].y = 50;
Objek[2].x = 50; Objek[2].y = 100;
Objek[3].x = 100; Objek[3].y = 100;
Objek[4].x = 100; Objek[4].y = 150;
Objek[5].x = 150; Objek[5].y = 100;
FormShow(Sender);
}
//-----------------------------------------------------//
void __fastcall TForm1::ButtonKiriClick(TObject *Sender)
{ int i;
for (i=1;i<=5;i++){ Objek[i].x-=5;};
FormShow(Sender);
}
//-----------------------------------------------------//
void __fastcall TForm1::ButtonAtasClick(TObject *Sender)
{
int i;
for (i=1;i<=5;i++){ Objek[i].y-=5;};
FormShow(Sender);
}
//-----------------------------------------------------//
void __fastcall TForm1::ButtonKananClick(TObject *Sender)
{ int i;
for (i=1;i<=5;i++){ Objek[i].x+=5;};
FormShow(Sender);
}
//-----------------------------------------------------//
void __fastcall TForm1::ButtonBawahClick(TObject *Sender)
{
int i;
for (i=1;i<=5;i++){ Objek[i].y+=5;};
FormShow(Sender);
}
//-----------------------------------------------------//
void __fastcall TForm1::Button1Click(TObject *Sender)
{ Elemen TempObjek[6];
int i; float Sdt;
for (i=1;i<=5;i++)
{
Objek[i].x=Objek[i].x-Image1->Width / 2;
Objek[i].y=Objek[i].y-Image1->Height / 2;
Sdt=10*M_PI/180;
TempObjek[i].x=(Objek[i].x*cos(Sdt)-Objek[i].y*sin(Sdt)); TempObjek[i].y=(Objek[i].x*sin(Sdt)+Objek[i].y*cos(Sdt));
Objek[i]=TempObjek[i];
Objek[i].x=Objek[i].x+Image1->Width / 2;
Objek[i].y=Objek[i].y+Image1->Height / 2;
}
FormShow(Sender);
}
//-----------------------------------------------------//
void __fastcall TForm1::Button2Click(TObject *Sender)
{ Elemen TempObjek[6];
int i,n,m; float Sdt;
for (i=1;i<=5;i++)
{
n = Objek[3].x;
m = Objek[3].y;
Objek[i].x=Objek[i].x-n;
Objek[i].y=Objek[i].y-m;
Sdt=10*M_PI/180;
TempObjek[i].x=(Objek[i].x*cos(Sdt)-Objek[i].y*sin(Sdt)); TempObjek[i].y=(Objek[i].x*sin(Sdt)+Objek[i].y*cos(Sdt));
Objek[i]=TempObjek[i];
Objek[i].x=Objek[i].x+n;
Objek[i].y=Objek[i].y+m;
}
FormShow(Sender);
}
//-----------------------------------------------------//
Setelah mengetikkan script tersebut, outputnya sebagai berikut :