Wednesday, 10 December 2014

Program for performing Two Dimensional Transformations : Translation , Scaling , Rotation , Reflection , Shear by using a homogeneous Matrix representation ,use of a function for matrix multiplication is desirable , so as to perform composite transformation.

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>

float ptmatr[3][9]={{100,120,120,160,160,60,60,100,100},{20,20,100,100,120,120,100,100,20},{1,1,1,1,1,1,1,1,1}};
void trans();
void scal();
void rotn();
void shear();
void refln();
void matrixmul(float a[3][3], float b[3][9]);
void displayorgobj();
void dda(int x1,int y1,int x2,int y2);


void main()
 {
int op,flag=1,a,gm,gd=DETECT;
initgraph(&gd,&gm,"..\\bgi");
clrscr();
do
{
printf("\t\n Operation :-");
printf("\t\n1. Display original object.\n");
printf("\t\n2. Perform Translation.\n");
printf("\t\n3. Perform Scaling.\n");
printf("\t\n4. Perform  Rotation.\n");
printf("\t\n5. Sharing the Object.\n");
printf("\t\n6. Perform Reflection.\n");
printf("\t\n7. Exit\n");
printf("\t\n Choose the operation to be performed :-\t");

scanf("%d",&op);
switch(op)
{
case 1:  printf("\t\n Displaying original object ::-\t\n");
displayorgobj();
break;
case 2:  printf("\t\n TRANSLATION ::-\t\n");
trans();
displayorgobj();
break;
case 3:  printf("\t\n SCALING ::-\t\n");
scal();
displayorgobj();
break;
case 4:  printf("\t\n ROTATION ::-\t\n");
rotn();
displayorgobj();
break;
case 5:  printf("\t\n SHEARING ::-\t\n");
shear();
displayorgobj();
break;
case 6:  printf("\t\n REFLECTION ::-\t\n");
refln();
displayorgobj();
break;
case 7: flag=0;
break;
default: printf("\n\nPlease select proper option !!!!!!!!!!!!!!!\n");
}
getch();
clrscr();
}
while(flag==1);
}

void matrixmul(float a[3][3], float b[3][9])
 {
int i,j,k;
float c[3][9], sum=0;
int p=0,q=0;
for(i=0; i<3; i++)
{
for(j=0; j<9; j++)
{
for(k=0; k<3; k++)
{
sum+=a[i][k]*b[k][j];
}
c[i][j]=sum;
sum=0;
}
   }
for (p=0; p<8; p++)
dda(c[q][p], c[q+1][p], c[q][p+1], c[q+1][p+1]);
dda(c[0][7], c[1][7], c[0][0], c[1][0]);
}

void trans()
 {
float tx, ty;
float txmatrix[3][3]={{1,0,0}, {0,1,0}, {0,0,1}};
printf("\n\t Enter translation parameter tx & ty :- \n");
scanf("%f %f",&tx, &ty);
txmatrix[0][2]=tx;
txmatrix[1][2]=ty;
matrixmul(txmatrix, ptmatr);
multiply(ptmatr,mat);
 }

void scal()
 {
float sx, sy, scalmatrix[3][3]={{0,0,0}, {0,0,0}, {0,0,1}};
printf("\n\tEnter scaling parameter sx & sy :- \n");
scanf("%f %f",&sx, &sy);
scalmatrix[0][0]=sx;
scalmatrix[1][1]=sy;
matrixmul(scalmatrix, ptmatr);
multiply(ptmatr,mat);
 }

void rotn()
 {
float deg,rotnmatrix[3][3]={{0,0,0}, {0,0,0}, {0,0,1}};
printf("\n\tEnter value of angle in degree :-\t");
scanf("%f",&deg);
rotnmatrix[0][0]=cos(deg*(3.142857/180));
rotnmatrix[0][1]=-sin(deg*(3.142857/180));
rotnmatrix[1][0]=sin(deg*(3.142857/180));
rotnmatrix[1][1]=cos(deg*(3.142857/180));
matrixmul(rotnmatrix, ptmatr);
 }

void refln()
{
float flag, reflnmatrix[3][3]={0,0,0,0,0,0,0,0,1};
printf("\n\tEnter '0' for reflection on X axis or \n\t'1' for reflection on Y axis or \n\t'2' for reflection about origin :-\t");
scanf("%f",&flag);
if(flag==0)
{
reflnmatrix[0][0]=1;
reflnmatrix[1][1]=-1;
}
else

{
if(flag==1)
{
reflnmatrix[0][0]=-1;
reflnmatrix[1][1]=1;
}
else
{
reflnmatrix[0][0]=-1;
reflnmatrix[1][1]=-1;
}
}
matrixmul(reflnmatrix, ptmatr);
}

void shear()
 {
float shx, shy, shearmatrix[3][3]={{1,0,0}, {0,1,0}, {0,0,1}};
printf("Enter shearing parameters shx& shy: ");
scanf("%f%f",&shx,&shy);
shearmatrix[0][1]=shx;
shearmatrix[1][0]=shy;
matrixmul(shearmatrix, ptmatr);
 }

void dda(int x1,int y1,int x2,int y2)
{
int dx,dy,steps,k,xmax,ymax,a,b,c,d;
float x,y,xincr,yincr;
xmax=getmaxx()/2;
ymax=getmaxy()/2;
a=xmax+x1;
b=ymax-y1;
c=xmax+x2;
d=ymax-y2;
line(xmax,0,xmax,getmaxy());
line(getmaxx(),ymax,0,ymax);
dx=c-a;
dy=d-b;
if(abs(dx)>abs(dy))
steps=abs(dx);
else
steps=abs(dy);
xincr=(float)dx/steps;
yincr=(float)dy/steps;
x=a;
y=b;
putpixel(floor(x),floor(y),15);
for(k=1;k<=steps;k++)
{
x+=xincr;
y+=yincr;
putpixel(floor(x),floor(y),15);
}
 }
void displayorgobj()
{
int i;
int arr[2][9]={{100,120,120,160,160,60,60,100,100},{20,20,100,100,120,120,100,100,20}};
for(i=0; i<8; i++)
    {
dda(arr[0][i],arr[1][i],arr[0][1+i],arr[1][1+i]);
    }
 }
Output :-












No comments:

Post a Comment