Read Me:
For run this code you must install GLUT in your system
Installation Guide : GLUT Install
Source Code: implemented using C++ and GLUT
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include<windows.h>
#include<iostream>
#define PI 3.14159265
using namespace std;
int x1,y1,x2,y2;
int option,m,n;
double sx,sy,x[100],y[100],x_t[100],y_t[100];
int Round(double xx)
{
return (floor(xx+0.5));
}
void drowAxis()
{
glBegin(GL_POINTS);
for(int i=-440; i<=480; i++)
{
glVertex2d(0,i);
}
for(int i=-520; i<=640; i++)
{
glVertex2d(i,0);
}
glEnd();
glFlush();
}
void init()
{
glClearColor(1.0, 1.0, 1.0,0);
glColor3f(0.0,0.0,0.0);
gluOrtho2D(-520,640,-440,480);
}
void line_draw(int x1, int y1,int x2,int y2)
{
glBegin(GL_LINES);
glVertex2i(x1,y1);
glVertex2i(x2,y2);
glEnd();
glFlush();
}
void shape()
{
if(option!=5)
{
printf("Choose the shape you want -- \n");
printf(" Choose 1 for triangle\n");
printf(" Choose 2 for square\n");
while(true)
{
scanf("%d",&n);
if(n==1 || n==2)
{
break;
}
printf("choose 1 or 2\n");
}
}
if(n==1)
{
m=3;
printf("Enter the co-ordinate of the triangle\n");
for(int i=0; i<3; i++)
{
scanf("%lf %lf",&x[i],&y[i]);
}
}
else
{
m=4;
printf("Enter the origin of square\n");
int temp1,temp2;
scanf("%d %d",&temp1,&temp2);
x[0]=temp1;
y[0]=temp2;
x[1]=temp1;
y[1]=temp2+50;
x[2]=temp1+50;
y[2]=temp2+50;
x[3]=temp1+50;
y[3]=temp2;
}
}
void translation()
{
printf("Enter the translation factor tx and ty\n");
int tx,ty;
scanf("%d %d",&tx,&ty);
for(int i=0; i<m; i++)
{
x_t[i] = tx + x[i];
y_t[i] = ty + y[i];/// translation
}
}
void rotation()
{
printf("Choose any option of Rotation\n");
printf(" 1- Rotation about the Origin\n");
printf(" 2- Rotation about an Arbitrary Point\n");
int choosed,pointx=0,pointy=0;
double angle;
while(true)
{
scanf("%d",&choosed);
if(choosed==1 || choosed == 2)
{
break;
}
printf("Enter a valid choose\n");
}
if(choosed==2)
{
printf("Enter the arbitrary point\n");
scanf("%d %d",&pointx, &pointy);
}
printf("Enter the Angle for Rotation\n");
scanf("%lf",&angle);
angle = (PI / 180)*angle;
double tempx[100],tempy[100];
for(int i=0; i<m; i++)
{
tempx[i] = x[i]-pointx;
tempy[i] = y[i]-pointy;
}
for(int i=0; i<m; i++)
{
x_t[i] = tempx[i]* cos(angle) - tempy[i] * sin(angle);
y_t[i] = tempx[i]* sin(angle) + tempy[i] * cos(angle);
}
for(int i=0; i<m; i++)
{
x_t[i] = x_t[i]+pointx;
y_t[i] = y_t[i]+pointy;
}
}
void mirror_reflection()
{
printf("Choose any option of Reflection\n");
printf(" 1- Reflection about the x axis\n");
printf(" 2- Reflection about the y axis\n");
printf(" 3- Reflection about a Line\n");
int choosed;
while(true)
{
scanf("%d",&choosed);
if(choosed==1 || choosed == 2|| choosed==3)
{
break;
}
printf("Enter a valid choose\n");
}
if(choosed==1)
{
printf("Reflection about the x axis\n");
for(int i=0; i<m; i++)
{
x_t[i] = x[i];
y_t[i] = y[i]*-1;
}
}
else if(choosed==2)
{
printf("Reflection about the y axis\n");
for(int i=0; i<m; i++)
{
x_t[i] = x[i]*-1;
y_t[i] = y[i];
}
}
else
{
printf("Reflection about a Line\n");
scanf("Enter the start and end points of the line\n");
scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
if(x2==x1)
{
for(int i=0; i<m; i++)
{
x_t[i] = -1*x[i]+x2+x2;
y_t[i] = y[i];
}
}
else if(y2==y1)
{
for(int i=0; i<m; i++)
{
x_t[i] = x[i];
y_t[i] = -1*y[i]+y2+y2;
}
}
else
{
double m_ = (y2-y1)/(x2-x1);
double b = y1-m*x1;
for(int i=0; i<m; i++)
{
x_t[i] = ((1-(m_*m_))/(m_*m_+1))*x[i] + (2*m_*y[i])/(m_*m_+1) - (2*b*m_)/(m_*m_+1);
y_t[i] = (2*m_*x[i])/(m_*m_+1) + ((m_*m_-1)*y[i])/(m_*m_+1) + (2*b)/(m_*m_+1);
}
}
}
}
void scaling()
{
printf("Choose any option of Scaling\n");
printf(" 1- Scaling about the Origin\n");
printf(" 2- Scaling about a arbitrary point\n");
int choosed,pointx=0,pointy=0;
while(true)
{
scanf("%d",&choosed);
if(choosed==1 || choosed == 2)
{
break;
}
printf("Enter a valid choose\n");
}
if(choosed==2)
{
printf("Enter the arbitrary point\n");
scanf("%d %d",&pointx, &pointy);
}
printf("Enter the Scaling factor sx and sy\n");
double sx,sy;
scanf("%lf %lf",&sx,&sy);
for(int i=0; i<m; i++)
{
x_t[i]= sx * x[i] - sx * pointx + pointx;
y_t[i]= sy * y[i] - sy * pointy + pointx;
}
}
void shearing()
{
printf("Enter the Scaling factor sx and sy\n");
double sh,sk;
double tempx[100],tempy[100];
scanf("%lf %lf",&sh,&sk);
for(int i=0; i<m; i++)
{
tempx[i] = x[i]-x[0];
tempy[i] = y[i]-y[0];
}
for(int i=0; i<m; i++)
{
x_t[i] = tempx[i] + sh * tempy[i];
y_t[i] = tempy[i] + sk * tempx[i];/// translation
}
for(int i=0; i<m; i++)
{
x_t[i] = x_t[i]+x[0];
y_t[i] = y_t[i]+y[0];
}
}
void funct()
{
glClear(GL_COLOR_BUFFER_BIT);
drowAxis();
line_draw(x1,y1,x2,y2);
glColor3f(1.0,0.0,0.0);
for(int i=0; i<m-1; i++)
{
line_draw(round(x[i]), round(y[i]), round(x[i+1]), round(y[i+1]));
}
line_draw(round(x[0]), round(y[0]), round(x[m-1]), round(y[m-1]));
glColor3f(0.0,0.0,1.0);
for(int i=0; i<m-1; i++)
{
line_draw(round(x_t[i]), round(y_t[i]), round(x_t[i+1]), round(y_t[i+1]));
}
line_draw(round(x_t[0]), round(y_t[0]), round(x_t[m-1]), round(y_t[m-1]));
}
int main(int argc,char ** argv)
{
/***
Tarequzzaman Khan
CSE'12
2012331514
*/
printf(" @@@ 2D Transformation @@@\n");
printf("Choose The value for transformation\n");
printf(" 1 - for Translation\n");
printf(" 2 - for Rotation\n");
printf(" 3 - for Reflection\n");
printf(" 4 - for Scaling\n");
printf(" 5 - for Shearing\n");
while(true)
{
scanf("%d",&option);
if(option<0 || option>5)
{
printf("Enter a value between 1 to 5\n");
continue;
}
break;
}
if(option == 1)
{
shape();
translation();
}
else if(option ==2)
{
shape();
rotation();
}
else if(option==3)
{
shape();
mirror_reflection();
}
else if(option==4)
{
shape();
scaling();
}
else if(option==5)
{
shape();
shearing();
}
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(0,0);
glutInitWindowSize(640,480);
glutCreateWindow("Translation");
init();
glutDisplayFunc(funct);
glutMainLoop();
}