Programs, Uncategorized

Transformations on given polygon [OpenGL]

Here is the menu driven program to do transformations on the give polygon

#include<iostream>
#include<GL/glut.h>
#include<math.h>
using namespace std;
float IMG[3][3],RES[3][3],T[3][3];
int i,j,k;
void displayIMG(float IMG[3][3]);
int axis();
void multiply();

void Init()
{
glClearColor(0.0,0.0,0.0,1.0);
glColor3f(1.0,1.0,0.0);
gluOrtho2D(-400,400,-400,400);
}
//**************************************************************************
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0.0,0.0,0.0,1.0);
glColor3f(0.0,1.0,0.0);
axis();

glClearColor(0.0,0.0,0.0,1.0);
glColor3f(1.0,0.0,0.0);
displayIMG(IMG);

glClearColor(0.0,0.0,0.0,1.0);
glColor3f(0.0,1.0,1.0);
displayIMG(RES);

glFlush();
}
//***********************************************************
void getdata()
{
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
cout<<“Enter vertex”<<i<<“\t”;
cin>>IMG[i][j];
}
IMG[i][j]=1;
}
}
//**********************************************************

void displayIMG(float Img[3][3])
{
glBegin(GL_POLYGON);
for(i=0;i<3;i++)
glVertex2f(Img[i][0],Img[i][1]);
glEnd();
}
//***********************************************************
int axis()
{
glBegin(GL_LINE_STRIP);
glVertex2i(-400,0);
glVertex2i(400,0);
glEnd();

glBegin(GL_LINE_STRIP);
glVertex2i(0,-400);
glVertex2i(0,400);
glEnd();
}

//**************************************************
void scaling()
{
float sx,sy;
cout<<“Enter sx,sy”;
cin>>sx>>sy;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
T[i][j]=0;
T[0][0]=sx;
T[1][1]=sy;
T[2][2]=1;
multiply();
}
}
//**********************************************************
void rotation()
{
float ang;
cout<<“enter angle of root”;
cin>>ang;
ang=ang+3.14;

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
T[i][j]=0;
T[0][0]=T[1][1]=cos(ang);
T[0][1]=-sin(ang);
T[1][0]=sin(ang);
T[2][2]=1;
multiply();

}
}
}
//***********************************************************
void multiply()
{

for(i=0;i<3;i++){
cout<<“\n”;
for(j=0;j<3;j++){
RES[i][j]=0;
for(k=0;k<3;k++){
RES[i][j]+=IMG[i][k]*T[k][j];
}
cout<<” “<<RES[i][j];
}
}
}
//************************************************************
void translation()
{
int tx,ty;
cout<<“Enter tx,ty”;
cin>>tx>>ty;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
T[i][j]=0;
}
}

T[0][0]=T[1][1]=T[2][2]=1;
T[2][0]=tx;
T[2][1]=ty;

multiply();
}

//************************************************************
void reflection()
{
int k;
float shx,shy;
while(1)
{

cout<<“\n——–MENU————“;
cout<<“\n1)Reflecton x axis \n2)Reflection y-axis \n3)Reflection origin \n4)Reflection x=y line:”;
cout<<“\n Enter ur choice:”;
cin>>k;
switch(k)
{
case 1:

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
T[i][j]=0;
}
}
T[0][0]=1;
T[1][1]=-1;
T[2][2]=-1;
multiply();
break;
case 2:

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
T[i][j]=0;
}
}
T[0][0]=-1;
T[1][1]=1;
T[2][2]=-1;
multiply();
break;

case 3:

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
T[i][j]=0;
}
}
T[0][0]=-1;
T[1][1]=-1;
T[2][2]=-1;
multiply();
break;

case 4:

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
T[i][j]=0;
}
}
T[0][0]=0;
T[1][1]=0;
T[2][2]=-1;
multiply();
break;
}
}
}
//************************************************************
void shearing()
{
float shx,shy;
int ch;
cout<<“\n——–MENU——–“;
cout<<“\1)x-shear \n2)y-shear”;
cout<<“\n Enter ur choice:”;
cin>>ch;
switch(ch)
{
case 1:
cout<<“\n Enter the shx “;
cin>>shx;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
T[i][j]=0;
}
}
T[0][0]=1;
T[1][1]=1;
T[2][2]=1;
T[1][0]=shx;
multiply();
break;
case 2:
cout<<“\n Enter the shy”;
cin>>shy;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
T[i][j]=0;
}
}
T[0][0]=1;
T[1][1]=1;
T[2][2]=1;
T[0][1]=shy;
multiply();
break;
}
}
//**********************************************************
void transform()
{
int ch;
cout<<“\n——MENU——“;
cout<<“\n1.Translation \n2.scaling \n3.Rotation\n 4.Reflection\n 5.shearing\n6.exit”;
cout<<“\n Enter Your Choice=”;
cin>>ch;
switch(ch)
{
case 1:translation();
break;
case 2:scaling();
break;
case 3:rotation();
break;
case 4:reflection();
break;
case 5:shearing();
break;

}
}
//*********************************************************
int main(int argc,char **argv)
{

getdata();
transform();
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowPosition(100,100);
glutInitWindowSize(800,800);
glutCreateWindow(“2D”);
glutDisplayFunc(display);
Init();
glutMainLoop();
return 0;
}

//********************************************************

Leave a comment