CG Computer Graphics Lab

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 45

1.

write an opengl program to draw a circle using mid point circle algorithm

#include<stdio.h>

#include<iostream>

#include<math.h>

#include<GL/glut.h>

using namespace std;

int ptx=250,pty=250,r=50;

void plot(int x,int y)

glBegin(GL_POINTS);

glVertex2i(x+ptx, y+pty);

glEnd();

void MPC()

int x=0;

int y=r;

float d=5/4-r;

plot(x,y);

while(y>x)

if(d<0)

x++;

d+=2*x+1;

else

x++;

y--;

d+=2*(x-y)+1;
}

plot(x,y);

plot(x,-y);

plot(-x,y);

plot(-x,-y);

plot(y,x);

plot(-y,x);

plot(y,-x);

plot(-y,-x);

void disp()

glClear(GL_COLOR_BUFFER_BIT);

MPC();

glFlush();

void init()

glClearColor(1.0,1.0,1.0,0.0);

glColor3f(0.2,0.2,0.2);

glPointSize(2.0);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluOrtho2D(0.0,640.0,0.0,480.0);

int main(int argc,char** argv)

{
glutInit(&argc,argv);

glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);

glutInitWindowSize(640,480);

glutInitWindowPosition(100,100);

glutCreateWindow("AS1");

glutDisplayFunc(disp);

init();

glutMainLoop();

return 0;

2. Write an opengl program to draw a rectangle. Use DDA line drawing algorithm to draw the sides.

#include<stdio.h>

#include<math.h>

#include<iostream>

#include<GL/glut.h>

using namespace std;

const float PI=3.14;

void DDA(int p,int q,int r,int s)

glBegin(GL_POINTS);

double a=(double)p,b=(double)q,c=(double)r,d=(double)s;

double m=(d-b)/(c-a);

double m1=1/m;

if(m<1)

while(a<=c)
{

glVertex2d(a,floor(b));

b=b+m;

a++;

else

while(b<=d)

glVertex2d(floor(a),b);

a=a+m1;

b++;

glEnd();

void init()

glClearColor(1.0,1.0,1.0,0.0);

glColor3f(0.2,0.2,0.2);

glPointSize(2.0);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluOrtho2D(0.0,640.0,0.0,480.0);

}
void disp()

glClear(GL_COLOR_BUFFER_BIT);

DDA(100,100,150,160);

glFlush();

int main(int argc,char** argv)

glutInit(&argc,argv);

glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);

glutInitWindowSize(640,480);

glutInitWindowPosition(100,100);

glutCreateWindow("AS2");

init();

glutDisplayFunc(disp);

glutMainLoop();

return 0;

3. Write an openg program to draw a polygon and perform rotation and translation.

#include<stdio.h>

#include<math.h>

#include<iostream>

#include<GL/glut.h>

using namespace std;

double p1[]={100,100,0};

double p2[]={100,250,0};
double p3[]={250,250,0};

double p4[]={250,100,0};

void init(int x,int y)

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluOrtho2D(-640.0,640.0,-480.0,480.0);

void disp()

glClear(GL_COLOR_BUFFER_BIT);

glPushMatrix();

//glRotatef(30,0,0,1);

glBegin(GL_POLYGON);

glVertex3dv(p1);

glVertex3dv(p2);

glVertex3dv(p3);

glVertex3dv(p4);

glEnd();

glPopMatrix();

glFlush();

glutSwapBuffers();

void trans(double &x,double &y,double a,double b)

x=x+a;

y=y+b;

void rotate(double &a,double &b,double &x,double &y,double r)


{

double p=sin((r*3.14)/180);

double q=cos((r*3.14)/180);

double m=(x-a)*q-(y-b)*p;

double n=(x-a)*p+(y-b)*q;

x=m+a;y=n+b;

void mykey(unsigned char key,int x,int y)

if(key=='t')

trans(p1[0],p1[1],10,10);

trans(p2[0],p2[1],10,10);

trans(p3[0],p3[1],10,10);

trans(p4[0],p4[1],10,10);

if(key=='o')

p1[0]={100};

p2[0]={100};

p3[0]={250};

p4[0]={250};

p1[1]={100};

p2[1]={250};

p3[1]={250};

p4[1]={100};

}
if(key=='r')

rotate(p1[0],p1[1],p1[0],p1[1],30);

rotate(p1[0],p1[1],p2[0],p2[1],30);

rotate(p1[0],p1[1],p3[0],p3[1],30);

rotate(p1[0],p1[1],p4[0],p4[1],30);

int main(int argc,char** argv)

glutInit(&argc,argv);

glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);

glutInitWindowSize(640,480);

glutInitWindowPosition(100,100);

glutCreateWindow("AS2");

glClearColor(1.0,1.0,1.0,0.0);

glColor3f(0.2,0.2,0.2);

glPointSize(2.0);

glutKeyboardFunc(mykey);

glutDisplayFunc(disp);

glutIdleFunc(disp);

glutReshapeFunc(init);

glutMainLoop();

return 0;

}
4. Write an opengl program to draw a rectangle. Use Bresenham’s line drawing algorithm to draw
the sides.

#include<stdio.h>

#include<math.h>

#include<iostream>

#include<GL/glut.h>

using namespace std;

const float PI=3.14;

void init()

glClearColor(1.0,1.0,1.0,0.0);

glColor3f(0.2,0.2,0.2);

glPointSize(2.0);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluOrtho2D(0.0,640.0,0.0,480.0);

void BA(int x,int y,int x2,int y2)

glBegin(GL_POINTS);

int dx, dy,e,i;

int incx,incy,inc1,inc2;

dx=x2-x;

dy=y2-y;

if(dx<0) dx=-dx;

if(dy<0) dy=-dy;
incx=1;

if(x2<x) incx=-1;

incy=1;

if(y2<y) incy=-1;

if(dx>dy)

glVertex2i(x,y);

e=2*dy-dx;

inc1=2*(dy-dx);

inc2=2*dy;

for(i=0;i<dx;i++)

if(e>=0)

y+=incy;

e+=inc1;

else

e+=inc2;

x+=incx;

glVertex2i(x,y);

else

{
glVertex2i(x,y);

e=2*dx-dy;

inc1=2*(dx-dy);

inc2=2*dx;

for(i=0;i<dy;i++)

if(e>=0)

x+=incx;

e+=inc1;

else

e+=inc2;

y+=incy;

glVertex2i(x,y);

glEnd();

void disp()

glClear(GL_COLOR_BUFFER_BIT);

BA(100,100,100,200);

BA(100,200,200,200);

BA(200,200,200,100);
BA(200,100,100,100);

glFlush();

int main(int argc,char** argv)

glutInit(&argc,argv);

glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);

glutInitWindowSize(640,480);

glutInitWindowPosition(100,100);

glutCreateWindow("AS2");

init();

glutDisplayFunc(disp);

glutMainLoop();

return 0;

5. Write an opengl program to draw a line using dda algorithm and perform translation operation

#include<stdio.h>

#include<math.h>

#include<iostream>

#include<GL/glut.h>

using namespace std;

const float PI=3.14;

int a=100;

int b=110;
int c=250;

int d=280;

void DDA(int p,int q,int r,int s)

glBegin(GL_POINTS);

double a=(double)p,b=(double)q,c=(double)r,d=(double)s;

double m=(d-b)/(c-a);

double m1=1/m;

if(m<1)

while(a<=c)

glVertex2d(a,floor(b));

b=b+m;

a++;

else

while(b<=d)

glVertex2d(floor(a),b);

a=a+m1;

b++;

glEnd();

}
void init()

glClearColor(1.0,1.0,1.0,0.0);

glColor3f(0.2,0.2,0.2);

glPointSize(2.0);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluOrtho2D(0.0,640.0,0.0,480.0);

void disp()

glClear(GL_COLOR_BUFFER_BIT);

DDA(a,b,c,d);

glFlush();

void trans(int &x,int &y,int a,int b)

x=x+a;

y=y+b;

void mykey(unsigned char key,int x,int y)

if(key=='t')

trans(a,b,10,10);

trans(c,d,10,10);

}
}

int main(int argc,char** argv)

glutInit(&argc,argv);

glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);

glutInitWindowSize(640,480);

glutInitWindowPosition(100,100);

glutCreateWindow("AS2");

init();

glutKeyboardFunc(mykey);

glutDisplayFunc(disp);

glutIdleFunc(disp);

glutMainLoop();

return 0;

6. Write an opengl program to draw the given figure at the center of the drawing window.

#include <stdio.h>

#include <iostream>

#include <GL/glut.h>

using namespace std;

int w=640,h=480;

int x1=w/2;

int y1=h/2;

int r=30;

void plot(int x, int y,int x1,int y1)


{

glBegin(GL_POINTS);

glVertex2i(x+x1, y+y1);

glEnd();

void myInit (void)

glClearColor(1.0, 1.0, 1.0, 0.0);

glColor3f(0.2f, 0.2f, 0.2f);

glPointSize(4.0);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluOrtho2D(0.0, 640.0, 0.0, 480.0);

void midPointCircleAlgo(int r)

int x = 0;

int y = r;

float decision = 5/4 - r;

plot(x, y,x1,y1);

while (y > x)

if (decision < 0)

x++;

decision += 2*x+1;

else
{

y--;

x++;

decision += 2*(x-y)+1;

plot(x, y,x1,y1);

plot(x, -y,x1,y1);

plot(-x, y,x1,y1);

plot(-x, -y,x1,y1);

plot(y, x,x1,y1);

plot(-y, x,x1,y1);

plot(y, -x,x1,y1);

plot(-y, -x,x1,y1);

void myDisplay(void)

glClear (GL_COLOR_BUFFER_BIT);

glColor3f (0.0, 0.0, 0.0);

glPointSize(1.0);

midPointCircleAlgo(r);

glBegin(GL_LINES);

glVertex2i(x1-r,y1-r);

glVertex2i(x1-r,y1+r);

glVertex2i(x1-r,y1+r);

glVertex2i(x1+r,y1+r);

glVertex2i(x1+r,y1+r);

glVertex2i(x1+r,y1-r);

glVertex2i(x1+r,y1-r);

glVertex2i(x1-r,y1-r);

glEnd();

glFlush ();
}

int main(int argc, char** argv)

glutInit(&argc, argv);

glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);

glutInitWindowSize (w, h);

glutInitWindowPosition (100, 150);

glutCreateWindow ("Line Drawing Alogrithms");

glutDisplayFunc(myDisplay);

myInit ();

glutMainLoop();

return 0;

7. Write an opengl programs to draw a polygon and fill the polygon by a color flood fill

Algorithm.

#include <math.h>

#include <GL/glut.h>

#include<stdio.h>

#include<iostream>

using namespace std;

const float PI=3.14;

struct Point{

int x;

int y;

};

struct Color{

float r;
float g;

float b;

};

Color getPixelColor(int x,int y)

Color color;

glReadPixels(x,y,1,1,GL_RGB,GL_FLOAT,&color);

return color;

void setPixelColor(int x,int y,Color color)

glColor3f(color.r,color.g,color.b);

glBegin(GL_POINTS);

glVertex2i(x,y);

glEnd();

glFlush();

void FloodF(int x,int y,Color oldcolor,Color newcolor)

Color color;

color=getPixelColor(x,y);

if(color.r==oldcolor.r&&color.g==oldcolor.g&&color.b==oldcolor.b)

setPixelColor(x,y,newcolor);

FloodF(x+1,y,oldcolor,newcolor);

FloodF(x,y+1,oldcolor,newcolor);

FloodF(x-1,y,oldcolor,newcolor);
FloodF(x,y-1,oldcolor,newcolor);

return;

void mykey(unsigned char key,int x,int y)

if(key=='f')

Color newcolor={1.0f,0.0f,0.0f};

Color oldcolor={1.0f,1.0f,1.0f};

FloodF(150,150,oldcolor,newcolor);

void DDA(int p,int q,int r,int s)

glBegin(GL_POINTS);

double a=(double)p;

double b=(double)q;

double c=(double)r;

double d=(double)s;

double m=(d-b)/(c-a);

double m1=1/m;

if(m<1)

while(a<=c)

glVertex2d(a,round(b));

b=b+m;

a++;
}

else

while(b<=d)

glVertex2d(round(a),b);

a=a+m1;

b++;

glEnd();

void drawrect(){

DDA(100,100,100,200);

DDA(100,200,200,200);

DDA(200,100,200,200);

DDA(100,100,200,100);

void init() {

glClearColor(1.0, 1.0, 1.0, 0.0);

glColor3f(0.0, 0.0, 0.0);

glPointSize(1.0);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();
gluOrtho2D(0, 300, 0, 300);

void disp()

glClear(GL_COLOR_BUFFER_BIT);

drawrect();

glFlush();

int main(int argc,char** argv)

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);

glutInitWindowSize(300, 300);

glutInitWindowPosition(200, 200);

glutCreateWindow("Open GL");

init();

glutDisplayFunc(disp);

glutKeyboardFunc(mykey);

glutIdleFunc(disp);

glutMainLoop();

return 0;

8. Write an opengl program to clip lines using Liang Barsky algorithm.

#include <math.h>
#include <GL/glut.h>

#include<stdio.h>

#include<iostream>

using namespace std;

const float PI=3.14;

struct Point{

int x;

int y;

};

struct Color{

float r;

float g;

float b;

};

Color getPixelColor(int x,int y)

Color color;

glReadPixels(x,y,1,1,GL_RGB,GL_FLOAT,&color);

return color;

void setPixelColor(int x,int y,Color color)

glColor3f(color.r,color.g,color.b);

glBegin(GL_POINTS);

glVertex2i(x,y);

glEnd();

glFlush();
}

void BoundaryF(int x, int y, Color fillColor, Color boundaryColor) {

Color currentColor = getPixelColor(x, y);

if(currentColor.r != boundaryColor.r && currentColor.g != boundaryColor.g && currentColor.b !=


boundaryColor.b) {

setPixelColor(x, y, fillColor);

BoundaryF(x+1, y, fillColor, boundaryColor);

BoundaryF(x-1, y, fillColor, boundaryColor);

BoundaryF(x, y+1, fillColor, boundaryColor);

BoundaryF(x, y-1, fillColor, boundaryColor);

return;

void mykey(unsigned char key,int x,int y)

if(key=='f')

Color fillColor = {1.0f, 0.0f, 0.0f};

Color boundaryColor = {0.0f, 0.0f, 0.0f};

BoundaryF(150,150,fillColor,boundaryColor);

void DDA(int p,int q,int r,int s)

glBegin(GL_POINTS);

double a=(double)p;

double b=(double)q;

double c=(double)r;
double d=(double)s;

double m=(d-b)/(c-a);

double m1=1/m;

if(m<1)

while(a<=c)

glVertex2d(a,round(b));

b=b+m;

a++;

else

while(b<=d)

glVertex2d(round(a),b);

a=a+m1;

b++;

glEnd();

void drawrect(){

DDA(100,100,100,200);

DDA(100,200,200,200);

DDA(200,100,200,200);

DDA(100,100,200,100);
}

void init() {

glClearColor(1.0, 1.0, 1.0, 0.0);

glColor3f(0.0, 0.0, 0.0);

glPointSize(1.0);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluOrtho2D(0, 300, 0, 300);

void disp()

glClear(GL_COLOR_BUFFER_BIT);

drawrect();

glFlush();

int main(int argc,char** argv)

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);

glutInitWindowSize(300, 300);

glutInitWindowPosition(200, 200);

glutCreateWindow("Open GL");

init();

glutDisplayFunc(disp);

glutKeyboardFunc(mykey);
glutIdleFunc(disp);

glutMainLoop();

return 0;}

9. Write a program to clip lines using Liang – Barsky algorithm

#include<stdio.h>

#include<iostream>

#include<GL/glut.h>

using namespace std;

double u1=0,u2=1;

double x1=30,x2=30,y3=-80,y2=15;

double xmin=-50,ymin=-50,xmax=50,ymax=50;

double p[4],q[4];

void init()

glClearColor(1.0,1.0,1.0,0.0);

glColor3f(0.0,0.0,0.0);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluOrtho2D(-300.0,300.0,-300.0,300.0);

void clip(double x1,double y1,double x2,double y2)

double dx=x2-x1,dy=y2-y1;

int i;
double t;

p[0]=-dx;q[0]=x1-xmin;

p[1]=dx;q[1]=xmax-x1;

p[2]=-dy;q[2]=y1-ymin;

p[3]=dy;q[3]=ymax-y1;

for(i=0;i<4;i++)

if(p[i]==0&&q[i]<0)

return;

if(p[i]<0)

t=(q[i])/(p[i]);

if(t>u1&&t<u2)

u1=t;

cout<<"u1::"<<u1<<endl;

else if(p[i]>0)

t=(q[i])/(p[i]);

if(t>u1&&t<u2)

u2=t;

cout<<"u2::"<<u2<<endl;

cout<<"u1::"<<u1<<endl;

cout<<"u2::"<<u2<<endl;

if(u1<u2)

x1=x1+u1*(x2-x1);

y1=y1+u1*(y2-y1);

x2=x1+u2*(x2-x1);
y2=y1+u2*(y2-y1);

cout<<x1<<endl<<x2<<endl<<y1<<endl<<y2<<endl;

glBegin(GL_LINES);

glVertex2d(x1,y1);

glVertex2d(x2,y2);

glEnd();

glFlush();

void disp()

glClear(GL_COLOR_BUFFER_BIT);

glColor3f(0.0,0.0,0.0);

glBegin(GL_LINES);

glVertex2d(xmin,ymin);

glVertex2d(xmin,ymax);

glVertex2d(xmin,ymax);

glVertex2d(xmax,ymax);

glVertex2d(xmax,ymax);

glVertex2d(xmax,ymin);

glVertex2d(xmax,ymin);

glVertex2d(xmin,ymin);

glEnd();

glBegin(GL_LINES);

glVertex2d(x1,y3);

glVertex2d(x2,y2);
glEnd();

glFlush();

void mykey(unsigned char key,int x,int y)

glClear(GL_COLOR_BUFFER_BIT);

if(key=='c')

glColor3f(0.0,0.0,0.0);

glBegin(GL_LINES);

glVertex2d(xmin,ymin);

glVertex2d(xmin,ymax);

glVertex2d(xmin,ymax);

glVertex2d(xmax,ymax);

glVertex2d(xmax,ymax);

glVertex2d(xmax,ymin);

glVertex2d(xmax,ymin);

glVertex2d(xmin,ymin);

glEnd();

clip(x1,y3,x2,y2);

glFlush();

int main(int argc,char** argv)

{
glutInit(&argc,argv);

glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);

glutInitWindowSize(300,300);

glutInitWindowPosition(100,100);

glutCreateWindow("LB");

init();

glutKeyboardFunc(mykey);

glutDisplayFunc(disp);

glutMainLoop();

return 0;

10. Write an program to create (without using built in function) a square and implement shear
algorithm along x axis and y axis.

#include<stdio.h>

#include<math.h>

#include<iostream>

#include<GL/glut.h>

using namespace std;

double p1[]={100,100,0};

double p2[]={100,250,0};

double p3[]={250,250,0};

double p4[]={250,100,0};

void init(int x,int y)

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluOrtho2D(0,640.0,0,480.0);

}
void disp()

glClear(GL_COLOR_BUFFER_BIT);

glBegin(GL_POLYGON);

glVertex3dv(p1);

glVertex3dv(p2);

glVertex3dv(p3);

glVertex3dv(p4);

glEnd();

glFlush();

glutSwapBuffers();

void shearx(double &x,double &y,double s)

x=x+s*y;

void sheary(double &x,double &y,double s)

y=y+s*x;

void mykey(unsigned char key,int x,int y)

if(key=='x')

shearx(p2[0],p2[1],1.2);

shearx(p3[0],p3[1],1.2);
}

if(key=='y')

sheary(p1[0],p1[1],1.2);

sheary(p2[0],p2[1],1.2);

if(key=='o')

p1[0]={100};

p2[0]={100};

p3[0]={250};

p4[0]={250};

p1[1]={100};

p2[1]={250};

p3[1]={250};

p4[1]={100};

int main(int argc,char** argv)

glutInit(&argc,argv);

glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);

glutInitWindowSize(640,480);

glutInitWindowPosition(100,100);

glutCreateWindow("AS2");

glClearColor(1.0,1.0,1.0,0.0);

glColor3f(0.2,0.2,0.2);

glPointSize(2.0);

glutKeyboardFunc(mykey);
glutDisplayFunc(disp);

glutIdleFunc(disp);

glutReshapeFunc(init);

glutMainLoop();

return 0;

11. Wap in C/C++ using Opengl to implement Cohen Sutherland line clipping algorithm

#include<GL/glut.h>

#include<math.h>

#include<stdio.h>

#include<iostream>

void display();

using namespace std;

float xmin=-100;

float ymin=-100;

float xmax=100;

float ymax=100;

float xd1,yd1,xd2,yd2;

void init(void)

glClearColor(0.0,0,0,0);

glMatrixMode(GL_PROJECTION);

gluOrtho2D(-300,300,-300,300);
}

int code(float x,float y)

int c=0;

if(y>ymax)c=8;

if(y<ymin)c=4;

if(x>xmax)c=c|2;

if(x<xmin)c=c|1;

return c;

void cohen_Line(float x1,float y1,float x2,float y2)

int c1=code(x1,y1);

int c2=code(x2,y2);

float m=(y2-y1)/(x2-x1);

while((c1|c2)>0)

if((c1 & c2)>0)

exit(0);

float xi=x1;float yi=y1;

int c=c1;

if(c==0)

c=c2;

xi=x2;

yi=y2;

float x,y;
if((c & 8)>0)

y=ymax;

x=xi+ 1.0/m*(ymax-yi);

else

if((c & 4)>0)

y=ymin;

x=xi+1.0/m*(ymin-yi);

else

if((c & 2)>0)

x=xmax;

y=yi+m*(xmax-xi);

else

if((c & 1)>0)

x=xmin;

y=yi+m*(xmin-xi);

if(c==c1)

xd1=x;

yd1=y;

c1=code(xd1,yd1);

if(c==c2)

xd2=x;
yd2=y;

c2=code(xd2,yd2);

display();

void mykey(unsigned char key,int x,int y)

if(key=='c'||key=='a')

{ cout<<"Clipped"<<endl;

cohen_Line(xd1,yd1,xd2,yd2);

glFlush();

void display()

glClear(GL_COLOR_BUFFER_BIT);

glColor3f(0.0,1.0,0.0);

glBegin(GL_LINE_LOOP);

glVertex2i(xmin,ymin);

glVertex2i(xmin,ymax);

glVertex2i(xmax,ymax);

glVertex2i(xmax,ymin);

glEnd();

glColor3f(1.0,0.0,0.0);

glBegin(GL_LINES);

glVertex2i(xd1,yd1);

glVertex2i(xd2,yd2);

glEnd();
glFlush();

int main(int argc,char** argv)

printf("Enter line co-ordinates:");

cin>>xd1>>yd1>>xd2>>yd2;

glutInit(&argc,argv);

glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);

glutInitWindowSize(600,600);

glutInitWindowPosition(0,0);

glutCreateWindow("Clipping");

glutDisplayFunc(display);

glutKeyboardFunc(mykey);

init();

glutMainLoop();

return 0;

12. Wap using Opengl to perform a 3-Dimensional transformation such as translation, rotation and
reflection on a given triangle.

#include<stdio.h>

#include<iostream>

#include<GL/glut.h>

using namespace std;

float a1=100.0,b1=100.0,c1=0.0,a2=150.0,b2=200.0,c2=0.0,a3=200.0,b3=100.0,c3=0.0;

float rr,rx,ry,rz,tx,ty,tz,sx,sy,sz;

void refecty(float &x,float &y,float &z){


x=-x;

void refectx(float &x,float &y,float &z)

y=-y;

void refectz(float &x,float &y,float &z)

x=-x;

y=-y;

void rotate()

void init()

glClearColor(1,1,1,0);

glColor3f(0.2,0.2,0.2);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

glOrtho(-600,600,-600,600,-600,600);

-
void disp()

glClear(GL_COLOR_BUFFER_BIT);

glBegin(GL_LINES);

glVertex2i(-600,0);

glVertex2i(600,0);

glVertex2i(0,-600);

glVertex2i(0,600);

glEnd();

glPushMatrix();

glTranslatef(tx,ty,tz);

glRotatef(rr,rx,ry,rz);

glScalef(sx,sy,sz);

glBegin(GL_TRIANGLES);

glVertex3f(a1,b1,c1);

glVertex3f(a2,b2,c2);

glVertex3f(a3,b3,c3);

glEnd();

glPopMatrix();

glFlush();

glutSwapBuffers();

void mykey(unsigned char key,int x,int y)

if(key=='r')

rz=1;

rr=30;

if(key=='t')
{

tx=100;

ty=100;

if(key=='s')

sx=sy=sz=1.5;

if(key=='z')

refectz(a1,b1,c1);

refectz(a2,b2,c2);

refectz(a3,b3,c3);

if(key=='x')

refectx(a1,b1,c1);

refectx(a2,b2,c2);

refectx(a3,b3,c3);

if(key=='y')

refecty(a1,b1,c1);

refecty(a2,b2,c2);

refecty(a3,b3,c3);

if(key=='o')

rr=0.0;rx=0;ry=0;rz=0.0;

tx=ty=tz=0.0;

sz=sx=sy=1.0;
}

int main(int argc,char** argv)

glutInit(&argc,argv);

glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);

glutInitWindowSize(600,600);

glutInitWindowPosition(100,100);

glutCreateWindow("AS12");

rr=0.0;rx=0;ry=0;rz=0.0;

tx=ty=tz=0.0;

sz=sx=sy=1.0;

init();

glutKeyboardFunc(mykey);

glutDisplayFunc(disp);

glutIdleFunc(disp);

glutMainLoop();

return 0;

13. Show that two successive reflection about either of the coordinate axes is equivalent to a single
rotation about the coordinate origin. Run your program for an object in the first quadrant.

#include <stdio.h>

#include <iostream>

#include <GL/glut.h>
using namespace std;

int w=640,h=480;

GLint x1,x2,y1,y2;

void myInit (void)

glClearColor(1.0, 1.0, 1.0, 0.0);

glColor3f(0.2f, 0.2f, 0.2f);

glPointSize(4.0);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluOrtho2D(-640, 640.0, -480, 480.0);

void mykey(unsigned char key,int x,int y)

if(key=='x')

y1=-y1;

y2=-y2;

if(key=='y')

x1=-x1;

x2=-x2;

if(key=='o')

x1=-x1;

x2=-x2;

y2=-y2;
y1=-y1;

void myDisplay(void)

glClear (GL_COLOR_BUFFER_BIT);

glColor3f (0.0, 0.0, 0.0);

glPointSize(1.0);

glBegin(GL_LINES);

glVertex2i(x1,y1);

glVertex2i(x2,y2);

glEnd();

glBegin(GL_LINES);

glVertex2i(-640,0);

glVertex2i(640,0);

glEnd();

glEnd();

glBegin(GL_LINES);

glVertex2i(0,480);

glVertex2i(0,-480);

glEnd();

glFlush ();

int main(int argc, char** argv)

glutInit(&argc, argv);

glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);

glutInitWindowSize (w, h);

glutInitWindowPosition (600, 600);

glutCreateWindow ("Line Drawing Alogrithms");;

x1=150;x2=300;y1=200;y2=300;

glutKeyboardFunc(mykey);
glutDisplayFunc(myDisplay);

glutIdleFunc(myDisplay);

myInit ();

glutMainLoop();

return 0;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy