Ask Question

Name:
Title:
Your Question:

Answer Question

Name:
Your Answer:
User Submitted Source Code!


Description:
  SASI
Language: C/C++
Code:
//2D Transformation
#include<GL/glut.h>
#include<GL/gl.h>
#include<GL/glu.h>
#include<math.h>
#include<stdlib.h>
#include<stdio.h>
int coordinates[3][2],choice,angle,rotate[3][2];
float xtransformation,ytransformation;
inline int Round(float value)
{
return (value+0.5);
}
void init(void)
{
glClearColor(1,1,1,0);
glColor3f(0,0,0);
glPointSize(3.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-100.0,100.0,-100.0,100.0,-1.0,1.0);
}
void ReadInput()
{
int counter;
for(counter=0;counter<3;counter++)
{
printf("Enter the Coordinates of the vertex %d:",counter+1);
scanf("%i %i",&coordinates[counter][0],&coordinates[counter][1]);
}}
void setPixel( int xcoordinate,int ycoordinate)
{
glBegin(GL_POINTS);
glVertex2i(xcoordinate,ycoordinate);
glEnd();
}
void DDA(int xstart,int xend,int ystart,int yend)
{
int dx=xend-xstart;
int dy=yend-ystart;
int steps,counter;
float xincrement,yincrement,xcoordinate,ycoordinate;
xcoordinate=xstart;
ycoordinate=ystart;
if(abs(dx)>abs(dy))
steps=abs(dx);
else
steps=abs(dy);
xincrement=dx/(float)steps;
yincrement=dy/(float)steps;
setPixel(Round(xcoordinate),Round(ycoordinate));
for(counter=0;counter<steps;counter++)
{
xcoordinate=xcoordinate+xincrement;
ycoordinate=ycoordinate+yincrement;
setPixel(Round(xcoordinate),Round(ycoordinate));
}}
void Draw_Triangle()
{
int counter;
for(counter=0;counter<3;counter++)
{
if(counter==2)
DDA(coordinates[counter][0],coordinates[0][0],coordinates[counter][1],coordinates[0][1]);
else
DDA(coordinates[counter][0],coordinates[counter+1][0],coordinates[counter][1],coordinates[counter+1][1]);
}}
void Translation()
{
int counter;
glClear(GL_COLOR_BUFFER_BIT);
Draw_Triangle();
for(counter=0;counter<3;counter++)
{
coordinates[counter][0]+=xtransformation;
coordinates[counter][1]+=ytransformation;
}
Draw_Triangle();
glFlush();
}
void Scaling()
{
int counter;
glClear(GL_COLOR_BUFFER_BIT);
Draw_Triangle();
for(counter=0;counter<3;counter++)
{
coordinates[counter][0]=coordinates[counter][0]*xtransformation;
coordinates[counter][1]=coordinates[counter][1]*ytransformation;
}
Draw_Triangle();
glFlush();
}


void Rotation()
{
int counter;
float theta=(angle*3.141)/180;
glClear(GL_COLOR_BUFFER_BIT);
Draw_Triangle();
for(counter=0;counter<3;counter++)
{
     rotate[counter][0]=coordinates[counter][0];
     rotate[counter][1]=coordinates[counter][1];
}
for(counter=0;counter<3;counter++)
{
coordinates[counter][0]=xtransformation+((rotate[counter][0]-xtransformation)*cos(theta))-((rotate[counter][1]-ytransformation)*sin(theta));

coordinates[counter][1]=ytransformation+((rotate[counter][0]-xtransformation)*sin(theta))+((rotate[counter][1]-ytransformation)*cos(theta));
}
Draw_Triangle();
glFlush();
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
glutCreateWindow("2D Transformation");
init();
ReadInput();
printf("ttTRANSFORMATION MENUnn1.Translationn2.Scalingn3.RotationnnEnter ur Choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("nEnter the translation matrix: ");
     scanf("%f%f",&xtransformation,&ytransformation);
     glutDisplayFunc(Translation);
     break;

case 2:printf("nEnter the scaling matrix: ");
     scanf("%f%f",&xtransformation,&ytransformation);
     glutDisplayFunc(Scaling);
     break;

case 3:printf("nEnter the pivot point: ");
     scanf("%f%f",&xtransformation,&ytransformation);
     printf("nEnter the angle of rotation ");
     scanf("%d",&angle);
     glutDisplayFunc(Rotation);
     break;
default :printf("nPlease enter only the choices provided");
}
glutMainLoop();
return 0;
}

          
Comments: