#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
struct TPoint
{ float x;
float y;};
struct TMatrix
{
float m[3][3];
};
void DrawLine(TPoint P1,TPoint P2)
{
float x1,y1,x2,y2;
x1 = 250+P1.x ;
y1 =250-P1.y;
x2 = 250+P2.x ;
y2 =250-P2.y;
line(x1,y1,x2,y2);
}
TPoint SetPoint(float x,float y)
{
TPoint p;
p.x = x;
p.y= y;
return p;
}
float d2r(float sd )
{
return sd/180*3.1415926535897932384626433832795;
}
TMatrix MatRotasi(float sd)
{
TMatrix M;
M.m[0][0]=cos(sd);M.m[0][1]=-sin(sd);M.m[0][2]=0;
M.m[1][0]=sin(sd);M.m[1][1]=cos(sd);M.m[1][2]=0;
M.m[2][0]=0;M.m[2][1]=0;M.m[2][2]=1;
return M;
}
TMatrix MatSkala(float Sx,float Sy)
{
TMatrix M;
M.m[0][0]=Sx;M.m[0][1]=0;M.m[0][2]=0;
M.m[1][0]=0;M.m[1][1]=Sy;M.m[1][2]=0;
M.m[2][0]=0;M.m[2][1]=0;M.m[2][2]=1;
return M;
}
TMatrix MatTranslasi(float Dx,float Dy)
{
TMatrix M;
M.m[0][0]=1;M.m[0][1]=0;M.m[0][2]=Dx;
M.m[1][0]=0;M.m[1][1]=1;M.m[1][2]=Dy;
M.m[2][0]=0;M.m[2][1]=0;M.m[2][2]=1;
return M;
}
TMatrix MatKali(TMatrix A, TMatrix B)
{
TMatrix C;
int i,j,k;
for (i=0;i<=2;i++)
{
for (j=0;j<=2;j++)
{
C.m[i][j]=0;
for (k=0;k<=2;k++)
{
C.m[i][j]=C.m[i][j]+A.m[i][k]*B.m[k][j];
}
}
}
return C;
}
TPoint Transformasi(TMatrix M, TPoint Pi)
{
TPoint Po;
Po.x=M.m[0][0]*Pi.x+M.m[0][1]*Pi.y+M.m[0][2];
Po.y=M.m[1][0]*Pi.x+M.m[1][1]*Pi.y+M.m[1][2];
return Po;
}
void Kotak(TMatrix m)
{
int i;
TPoint p[5],p1,p2;
p[0]=SetPoint(-100,-100);
p[1]=SetPoint(100,-100);
p[2]=SetPoint(100,100);
p[3]=SetPoint(-100,100);
p[4]=SetPoint(-100,-100);
for (i = 0;i<4 i="" p=""> {
p1 = Transformasi(m,p[i]);
p2 = Transformasi(m,p[i+1]);
DrawLine(p1,p2);
}
}
float sd;
int main()
{
TMatrix m;
initwindow(500,500);
do
{
cleardevice();
m=MatSkala(1,1);
m=MatKali(MatRotasi(d2r(sd)),MatSkala(1,1));
sd =sd+2;
Kotak(m);
delay(100);
}while(!kbhit());
return 0;
}
4>
No comments:
Post a Comment