Upload
purush-thaman
View
746
Download
2
Embed Size (px)
Citation preview
SRIRAM ENGINEERING COLLEGE
Perumalpattu,Thiruvallur Dist-602 024.
DEPARTMENT OF COMPUTER SCIENCE
COMPUTER GRAPHICS LAB
VII SEM – CS 2405
NAME:
REGNO:
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
SRIRAM ENGINEERING COLLEGE
Perumalpattu,Thiruvallur Dist-602 024.
DEPARTMENT OF COMPUTER SCIENCE
Register.No:
BONAFIDE CERTIFICATE
NAME OF THE LAB: COMPUTER GRAPHICS LAB
DEPARTMENT: COMPUTER SCIENCE AND ENGINEERING
This is to certify that a bonafide record of work done by _________________________
Of VII semester Computer Science and Engineering class in the Computer Graphics Laboratory during
the year 2011-2012.
Signature of lab in charge Signature of Head of Dept
Submitted for the practical examination held on __________________
Internal Examiner External Examiner
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
INDEX
S.NO DATE NAME OF EXPERIMENT PAGE.NO SIGN
1. Bresenhams Algorithm – Line, Circle, Ellipse
2. Line, Circle and ellipse attributes
3. Two Dimensional transformations - Translation, Rotation, Scaling, Reflection, Shear.
4. Composite 2D Transformations
5. Cohen Sutherland 2D line clipping and Windowing
6. Sutherland – Hodgeman Polygon clipping Algorithm
7. Three dimensional transformations - Translation, Rotation, Scaling
8. Composite 3D transformations
9. Drawing three dimensional objects and Scenes
10. Generating Fractal images
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
Ex.No:1 IMPLEMENTATION OF BRESENHAM’S ALGORITHM
DATE: LINE, CIRCLE, ELLIPSE.
LINE:
AIM:To study and Implement Bresenham's Line Drawing Algorithm
ALGORITHM:
STEP 1: Input the two line endpoints and store the left end point in (x0,y0).
STEP 2: Load (x0,y0) into the frame buffer,that is,plot the first point.
STEP 3: Calculate constants Δx,Δy,2Δy,and 2Δy-2Δx,and obtain the starting value for the
decision parameter as p0=2Δy-Δx
STEP 4: At each x,along the line,starting at k=0,perform the following test. if pk< 0,the next
point to plot is (xk+1,yk)and pk+1=pk+2Δy otherwise,the next point to plot is
(xk+1,yk+1)and pk+1=pk+2Δy-2Δx
STEP 5: Repeat step 4Δx times.
Program:
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
LINE
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<graphics.h>
#define MINX 0
#define MINY 0
void begin();
int scale_x,scale_y,x_factory_factor;
void main()
{
int x,y,x0,y0,xn,yn,dx,dy,p,twody,twodydx,xend;
char te[20];
int d=DETECT,b;
int CENTREX,CENTREY,MAXX,MAXY;
printf(" \n enter the starting points:");
scanf(" %d %d",&x0,&y0);
printf("\n enter the end points:");
scanf(" %d%d",&xn,&yn);
printf("\n");
clrscr();
initgraph(&d,&b,"");
cleardevice();
setgraphmode(getgraphmode());
CENTREX=getmaxx()/2;
CENTREY=getmaxy()/2;
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
begin();
dx=abs(x0-xn);
dy=abs(y0-yn);
p=2*dy-dx;
twody=2*dy;
twodydx=2*(dy-dx);
if(x0>xn)
{
x=xn;
y=yn;
xend=x0;
}
else
{
x=x0;
y=yn;
xend=xn;
}
putpixel(CENTREX+x,CENTREY+y,3);
while(x<xend)
{
x++;
if(p<0)
p=p+twody;
else
{
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
y++;
p=p+twodydx;
}
putpixel(CENTREX+x,CENTREY+y,3);
}
getch();
closegraph();
}
void begin()
{
int CENTREX=getmaxx()/2;
int CENTREY=getmaxy()/2;
int MAXX=getmaxx();
//int MAXY=getmaxx();
line(0,getmaxy()/2,getmaxx(),getmaxy()/2);
line(getmaxx()/2,0,getmaxx()/2,getmaxx());
setcolor(50);
settextstyle(0,0,0);
outtextxy(CENTREX+2,CENTREY+5,"ORIGIN");
outtextxy(MAXX-40,CENTREY+4,"FIRST");
outtextxy(CENTREX+5,5,"SECOND");
}
OUTPUT:
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
Enter the starting points:0 0
Enter the end points:100 100
SECOND
ORIGIN
FIRST
RESULT:Thus the program for implementation of Bresenham’s line drawing Algorithm is successfully compiled and executed.
CIRCLE:
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
AIM:To study and Implement Bresenham's Circle Drawing Algorithm
ALGORITHM:
STEP 1: Input radius r and circle center(xc,yc) and obtain the first point on the circumference of the circle centered on the origin as (x0,y0)=(0,r)
STEP 2: Calculate the initial value of the decision parameter as p0=5/4-r
STEP 3: At each xk position,starting at k=0,perform the following test:
If pk<0 ,the next point along the circle centered on(0,0) is(xk+1,yk) and
Pk+1=pk+2xk+1 + 1
Otherwise the next point along the circle is(xk+1,yk-1) and pk+1=pk+2xk+1 +1 +2yk+1 where 2xk+1=2xk+2 and 2yk+1=2yk-2
STEP 4: Determine the symmetry points in the other seven octants
STEP 5:Move each calculated pixel position (x,y) on to the circular path centered on (xc,yc) and plot the coordinate values:
x=x+x0, y=y+yc
STEP 6: Repeat steps 3 through 5 until x>=y
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
CIRCLE:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<dos.h>
#include<graphics.h>
#define cenx 320
#define ceny 240
#define MAXX 639
#define MAXY 479
#define MINX 0
#define MINY 0
void main()
{
void cir(int a,int b,int c);
int gd=DETECT,gm;
int xcent,ycent,radi;
printf("\n center coordinates");
scanf("%d%d",&xcent,&ycent);
printf("\n Enter the radius");
scanf("%d",&radi);
clrscr();
initgraph(&gd,&gm,"");
cleardevice();
setbkcolor(BLUE);
cir(xcent,ycent,radi);
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
getch();
closegraph();
}
void cir(int xc,int yc,int radius)
{
void plotpoints(int a,int b,int c,int d);
int p,x,y;
line(getmaxx()/2,0,getmaxx()/2,getmaxx());
line(0,getmaxy()/2,getmaxx(),getmaxy()/2);
setbkcolor(111);
x=0;
y=radius;
plotpoints(xc,yc,x,y);
p=1-radius;
while(x<y)
{
if(p<0)
x++;
else
{
x++;
y--;
}
if(p<0)
{
p=p+(2*x)+1;
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
}
else
{
p=p+(2*(x-y))+1;
}
plotpoints(xc,yc,x,y);
}
}
void plotpoints(int xc,int yc,int x,int y)
{
putpixel(cenx+xc+x,ceny-yc+y,4);
putpixel(cenx+xc-x,ceny-yc+y,4);
putpixel(cenx+xc+x,ceny-yc-y,4);
putpixel(cenx+xc-x,ceny-yc-y,4);
putpixel(cenx+xc+y,ceny-yc+x,4);
putpixel(cenx+xc-y,ceny-yc+x,4);
putpixel(cenx+xc+y,ceny-yc-x,4);
putpixel(cenx+xc-y,ceny-yc-x,4);
}
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
OUTPUT:
Enter co-ordintaes :0 0
Enter the radius:100
RESULT:
Thus the program for implementation of Bresenham’s circle drawing Algorithm is successfully compiled and executed.
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
ELLIPSE:
AIM:To study and Implement Bresenham's Ellipse Drawing Algorithm
ALGORITHM:
STEP 1:Input rx,ry and ellipse center (xc,yc) and obtain the first point on an ellipse centered on the origin as (x0,y0)=(0,r)
STEP 2: Calculate the initial value of the decision parameter in region 1 as
P10=r2y-r2xry+1/4r2x
STEP 3: At each xk position in region 1,starting at k=0,perform the following test:
If p1k<0,the next point along the ellipse centered on (0,0) is (xk+1,yk) and
P1k+1=p1k+2r2yxk+1+r2y
Otherwise the next point along the circle is (xk+1,yk-1) and
P1k+1=p1k+2r2yxk+1-2r2xyk+1+r2y
STEP 4:Calculate the initial value of the decision parameter in region 2 using the last point (x0,y0) calculated in region 1 as
STEP 5:At each position in region 2,starting at k=0,perform the following test:
If p2k>0,the next point along the ellipse centered on(0,0) is (xk,yk-1) and
STEP 6:Determine the symmetry points in the other three quadrants
STEP 7:Move each calculated pixel position(x,y) onto the elliptical path centered on(xc,yc) and plot the coordinate values: x=x+x0 y=y+y0
STEP 8:Repeat the steps for region1 until 2r2yx>=2r2xy
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
ELLIPSE:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<graphics.h>
#include<math.h>
float p,rx2,ry2,xc,yc,x,y,ry,rx,try2,trx2,px,py;
float na;
int r;
int errorcode;
void plot()
{
int k;
putpixel(xc+x,yc+y,BLUE);
putpixel(xc-x,yc+y,BLUE);
for(k=xc-x;k<=xc+x;k++)
putpixel(xc+x,yc-y,BLUE);
putpixel(xc-x,yc-y,BLUE);
}
void brell()
{
plot();
ry2=ry*ry;
rx2=rx*rx;
try2=2*ry2;
trx2=2*rx2;
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
x=0;
y=ry;
plot();
na=(float)(r*(2/4));
if((na)+0.5<na)
na=ceil(na);
else
p=ry2-rx2*ry+na;
px=0;
py=trx2*y;
while(px<py)
{
x++;
px+=try2;
if(p>=0)
{
y--;
py=py-trx2;
}
if(p<0)
p+=ry2+px;
else
p=p+ry2+px-py;
plot();
}
na=ceil((float)(x+0.5));
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
p=ry2*(na)*(na)+rx2*(y-1)*(y-1)-rx2*ry2;
while(y>0)
{
y--;
py-=trx2;
if(p<0)
{
x++;
px+=try2;
}
if(p>0)
p+=rx2-py;
else
p+=rx2-py+px;
plot(1);
}
getch();
closegraph();
}
void main()
{
int gd=DETECT,gm,errorcode;
clrscr();
printf("\n enter the xradius");
scanf("%f",&rx);
printf("\n enter the yradius");
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
scanf("%f",&ry);
clrscr();
initgraph(&gd,&gm,"");
setbkcolor(7);
line(getmaxx()/2,0,getmaxx()/2,getmaxx());
line(0,getmaxy()/2,getmaxx(),getmaxy()/2);
xc=getmaxx()/2;
yc=getmaxy()/2;
brell();
}
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
OUTPUT:
Enter the X radius: 100
Enter the Y radius: 150
RESULT:
Thus the program for implementation of Bresenham’s ellipse drawing Algorithm is successfully compiled and executed.
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
Ex.No: 2 LIINE, CIRCLE AND ELLIPSE ATTRIBUTES
DATE:
AIM: To study and Implement of Line attributes by using opengl.
ALGORITHM:
STEP 1:This program demonstrates geometric primitives and their attributes.Using OpenGL Graphics Library to perform properties of output primitives.Include all header file such as glut.h,stdlib.h etc.,
STEP 2:Using glvertex2f to specify point ,line and polygon vertices within glbegin and glendInitialize the properties of line (color and shade)
STEP 3:Glshademodel specifies a symbolic value representing a shading technique. Accepted values are GL_FLAT and GL_SMOOTH.
STEP 4:Glclear(GL_COLOR-BUFFER-BIT) clear buffer to preset values and indicating the buffer currently enabled for color writing.
STEP 5:Glenable() to specify the enable or disable GL capability.Glinestipple(Glint factor,Pattern) to specify thr line stipple pattern.
STEP 6:Set the viewport glviewport(x,y,w,h)Glmatrixmode(GL_projection) specifying which matrix stack is the target for subsequent matrix operation.
STEP 7:Glloadidentity function replaces the current matrix with the identify matrix.Glortho2d to specify the 2d orthographic viewing region
STEP 8:Initialize the glut libraryInitialize the display mode GLUT_SINGLE(bit mask to select single buffered windows) and GLUT_RGB(bit mask to select the rgb)
STEP 9:Using Input events to perform following operationglutReshapeFunc() - what actions to be taken when the window is resized, moved or exposed.glutKeyboardFunc() - links a key with a routine that is invoked when a key is pressed. glutSpecialFunc() - F1, F2, F3 etc.glutMouseFunc() - links a mouse button with a routine that is invoked when the mouse button is pressed/released.
STEP 10:Stop the program.
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
PROGRAM:
#include <glut.h>#include <stdlib.h>#define drawOneLine(x1,y1,x2,y2) glBegin(GL_LINES); glVertex2f ((x1),(y1)); glVertex2f ((x2),(y2)); glEnd();void init(void) { glClearColor (0.0, 0.0, 0.0, 0.0); glShadeModel (GL_FLAT);}void display(void){ int i; glClear (GL_COLOR_BUFFER_BIT);
/* select white for all lines */ glColor3f (1.0, 1.0, 1.0);
/* in 1st row, 3 lines, each with a different stipple */ glEnable (GL_LINE_STIPPLE); glLineStipple (1, 0x0101); /* dotted */ drawOneLine (50.0, 125.0, 150.0, 125.0); glLineStipple (1, 0x00FF); /* dashed */ drawOneLine (150.0, 125.0, 250.0, 125.0); glLineStipple (1, 0x1C47); /* dash/dot/dash */ drawOneLine (250.0, 125.0, 350.0, 125.0);/* in 2nd row, 3 wide lines, each with different stipple */ glLineWidth (5.0); glLineStipple (1, 0x0101); /* dotted */ drawOneLine (50.0, 100.0, 150.0, 100.0); glLineStipple (1, 0x00FF); /* dashed */ drawOneLine (150.0, 100.0, 250.0, 100.0); glLineStipple (1, 0x1C47); /* dash/dot/dash */ drawOneLine (250.0, 100.0, 350.0, 100.0); glLineWidth (1.0);/* in 3rd row, 6 lines, with dash/dot/dash stipple *//* as part of a single connected line strip */ glLineStipple (1, 0x1C47); /* dash/dot/dash */ glBegin (GL_LINE_STRIP); for (i = 0; i < 7; i++) glVertex2f (50.0 + ((GLfloat) i * 50.0), 75.0); glEnd ();/* in 4th row, 6 independent lines with same stipple */ for (i = 0; i < 6; i++) { drawOneLine (50.0 + ((GLfloat) i * 50.0), 50.0, 50.0 + ((GLfloat)(i+1) * 50.0), 50.0); }/* in 5th row, 1 line, with dash/dot/dash stipple *//* and a stipple repeat factor of 5 */
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
glLineStipple (5, 0x1C47); /* dash/dot/dash */ drawOneLine (50.0, 25.0, 350.0, 25.0); glDisable (GL_LINE_STIPPLE); glFlush ();}void reshape (int w, int h){ glViewport (0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode (GL_PROJECTION); glLoadIdentity (); gluOrtho2D (0.0, (GLdouble) w, 0.0, (GLdouble) h);}void keyboard(unsigned char key, int x, int y){ switch (key) { case 27: exit(0); break; }}int main(int argc, char** argv){ glutInit(&argc, argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutInitWindowSize (400, 150); glutInitWindowPosition (100, 100); glutCreateWindow (argv[0]); init (); glutDisplayFunc(display); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutMainLoop(); return 0;}
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
OUTPUT:
RESULT:
Thus the program for implementation of Line, circle and ellipse Attributes are successfully compiled and executed.
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
Ex No:3 (a) TWO DIMENSIONAL TRANSFORMATIONS
DATE: TRANSLATION,ROTATION,SCALING,REFLECTION,SHEAR
AIM: To study and Implement 2D Transformation
ALGORITHM:
STEP 1:Start the program
STEP 2:Include the required variables and the graphics mode specifying the path.
STEP 3:Get the vertices and get the coordinates in each case.Get the options then using the suited case to follow transformation.
STEP 4:If the option1 get the tx and ty coordinate then perform the translation operation.Point[i]=point[i]+tx and point[i+1]=point[i+1]+ty
STEP 5:Repeat the operation for all the coordinates.
STEP 6:If option2 get the sx and sy coordinates then perform the scaling operation point[i]=point[i]*sx;Point[i+1]=point[i+1]*sy;
STEP 7:Repeat the operation for all coordinates.
STEP 8:Display the output result. If the option is 3 get the rotation angle(r) then perform the rotation operation and find the r1=r1*(3.14)/(180)Point[i]=point[i]*cos(r1)-point[i+1]*sin(r1)Point[i+1]=point[i]*sin(r1)-point[i+1]*cos(r1)
STEP 9:Repeat the operation for all the coordinates
STEP 10:Display the output result
STEP 11:If the option is 4 perform the reflection operation such that Temp[i]=point[i]Point[i]=point[i+1]Point[i+1]=temp[i]Repeat the operation for all the coordinates.
STEP 12:if the option is 5 get the shearing coordinates about x axis as shx and perform the shearing operation such as point[i]=point[i]+(shx*point[i+1]);Point[i+1]=point[i+1]Repeat the steps for all coordinates
STEP 13:If option 6 exit the program
STEP 14.:Stop the program.
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
float tx,ty,sx,sy,theta;
int x[10],y[10],i,n,ch,xf,yf,xr,yr,angle;
do
{
clrscr();
printf("\t\t\tTransformation of an Object");
printf("\n1.Transforamtion \n2.Scaling\n3.Scalinf of an Fixed point\n4.Rotaion\n5.Rotation of an fixed point\n6.Exit");
printf("\n Enter your choice");
scanf("%d",&ch);
if(ch==6)
exit(6);
printf("\n Enter the total number of vertices");
scanf("%d",&n);
printf("Enter the co ordinates:");
for(i=1;i<=n;i++)
scanf("%d%d",&x[i],&y[i]);
initgraph(&gd,&gm,"");
for(i=1;i<n;i++)
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
line(x[i],y[i],x[i+1],y[i+1]);
line(x[n],y[n],x[1],y[1]);
getch();
closegraph();
switch(ch)
{
case 1:
printf("\n enter the translation vector");
scanf("%f%f%f",&tx,&ty);
for(i=1;i<=n;i++)
{
x[i]=x[i]+tx;
y[i]=y[i]+ty;
}
break;
case 2:
printf("\n enter the scaling vector");
scanf("%f%f%f",&sx,&sy);
for(i=1;i<n;i++)
{
x[i]=x[i]*sx;
y[i]=y[i]*sy;
}
break;
case 3:
printf("\n enter the fixed point");
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
scanf("%d%d%d",&sx,&sy);
for(i=1;i<n;i++)
{
x[i]=x[i]*sx+(1-sx)*xf;
y[i]=y[i]*sy+(1-sy)*yf;
}
break;
case 4:
printf("\n enter the rotation angle");
scanf("%d",angle);
theta=(3.14/180)*angle;
for(i=1;i<n;i++)
{
x[i]=x[i]*cos(theta)+y[i]*sin(theta);
y[i]=-x[i]*sin(theta)+y[i]*cos(theta);
}
break;
case 5:
printf("\n enter the pilot number");
scanf("%d%d",&xr,&yr);
printf("\n enter the rotation angle");
scanf("%d",angle);
theta=(3.14/180)*(float)angle;
for(i=1;i<=n;i++)
{
x[i]=xr+(x[i]-xr)*cos(theta)+(y[i]-yr)*sin(theta);
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
y[i]=yr+(x[i]-xr)*sin(theta)+(y[i]-yr)*cos(theta);
}
break;
default:
exit(0);
}
initgraph(&gd,&gm,"");
printf("\n After transformation");
for(i=1;i<n;i++)
line(x[i],y[i],x[i+1],y[i+1]);
line(x[n],y[n],x[1],y[1]);
getch();
closegraph();
}
while(ch!=6);
}
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
OUTPUT:
Transformation of an Object
1. Transformation
2. Scaling
3. Scaling of a fixed point
4. Rotation
5. Rotation of a fixed pointexit
Enter your choice: 1
Enter the total no of vertices:3
Enter the co-ordinates:10 10
0 50
50 0
Enter the translation vector: 10 10
20 20
After transformation:
Transformation of an Object
1. Transformation
2. Scaling
3. Scaling of a fixed point
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
4. Rotation
5. Rotation of a fixed pointexit
Enter your choice: 2
Enter the total no of vertices:3
Enter the co-ordinates:10 10
0 50
50 0
Enter the scaling vector: 10 10
20 20
After transformation:
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
Transformation of an Object
1. Transformation
2. Scaling
3. Scaling of a fixed point
4. Rotation
5. Rotation of a fixed pointexit
Enter your choice: 3
Enter the total no of vertices:3
Enter the co-ordinates:10 10
0 50
50 0
Enter the fixed point:10 20 30
After transformation:
Transformation of an Object
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
1. Transformation
2. Scaling
3. Scaling of a fixed point
4. Rotation
5. Rotation of a fixed pointexit
Enter your choice: 3
Enter the total no of vertices:4
Enter the co-ordinates:10 10
0 50
50 0
Enter the rotating angle:20
After transformation:
Transformation of an Object
1. Transformation
2. Scaling
3. Scaling of a fixed point
4. Rotation
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
5. Rotation of a fixed pointexit
Enter your choice: 3
Enter the total no of vertices:5
Enter the co-ordinates:10 10
0 50
50 0
Enter the fixed point: 0 10
0 20
Enter the rotating angle:20
RESULT:
Thus the program for implementation of Two dimensional Transformations for Transformation, Scaling and Rotation are successfully compiled and executed.
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
Ex No:3 (b) REFLECTION OF AN OBJECT
Program:
#include <stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
int x0=320,y0=240;
int t[3][2]={100,100,150,50,50,50};
void main()
{
int gd=DETECT,gm,ch;
initgraph(&gd,&gm," ");
clrscr();
do
{
cleardevice();
gotoxy(1,1);
printf("\t\t reflection of an object\t\t");
printf("\n1.original\n2.reflection\n3.exit");
printf("\n enter ur choice:");
scanf("%d",&ch);
if(ch==1)
{
cleardevice();
triangle(0,1,1,1);
getch();
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
}
if(ch==2)
reflect();
}
while(ch!=3);
}
triangle(int i,int j,int rx,int ry)
{
line(x0,0,x0,2*y0);
line(0,y0,2*x0,y0);
line(x0+t[0][i]*rx,y0-t[0][j]*ry,x0+t[1][i]*rx,y0-t[1][j]*ry);
line(x0+t[1][i]*rx,y0-t[1][j]*ry,x0+t[2][i]*rx,y0-t[2][j]*ry) ;
line(x0+t[2][i]*rx,y0-t[2][j]*ry,x0+t[0][i]*rx,y0-t[0][j]*ry) ;
return;
}
reflect()
{
int ch;
printf("\n 1.x ases\n 2.y axis\n 3.orgin\n4.y=x\n5.y=-x");
printf("enter ur choice");
scanf("%d",&ch);
cleardevice();
triangle(0,1,1,1);
if(ch==1)
triangle(0,1,1,-1);
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
if(ch==2)
triangle(0,1,-1,1);
if(ch==3)
triangle(0,1,-1,-1);
if(ch==4)
triangle(1,0,1,1);
if(ch==5)
triangle(1,0,-1,-1);
getch();
return;
}
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
OUTPUT:
Reflection of an object
Original
Reflection
Exit
Enter your choice:1
Reflection of an object
1.Original
2.Reflection
3.Exit
Enter your choice:2
x – axis
y – axis
origin
y=x
y=-x Enter ur choice : 1
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
Reflection of an object
1.Original
2.Reflection
3.Exit
Enter your choice:2
1.x – axis
2.y – axis
3.origin
4.y=x
5.y=-x Enter ur choice : 2
Reflection of an object
1.Original
2.Reflection
3.Exit
Enter your choice:2
1.x – axis
2.y – axis
3.origin
4.y=x
5.y=-x Enter ur choice : 3
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
Reflection of an object
1.Original
2.Reflection
3.Exit
Enter your choice:2
1.x – axis
2.y – axis
3.origin
4.y=x
5.y=-x Enter ur choice : 4
Reflection of an object
1.Original
2.Reflection
3.Exit
Enter your choice:2
1.x – axis
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
2.y – axis
3.origin
4.y=x
5.y=-x Enter ur choice : 5
RESULT:
Thus the program for implementation of Two dimensional Transformations for Reflection are successfully compiled and executed.
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
Ex No:3(c) SHEARING OF AN OBJECT
Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm,x[10],y[10],x1[10],y1[10],i,n,s,ch;
clrscr();
printf("\t\t\tSHEAR TRANSFORMATION\n");
printf("\t\t\t\-------------------");
printf("\nEnter the total number of vetices:");
scanf("\n%d",&n);
printf("\nEnter the co-ordeinates");
for(i=1;i<=n;i++)
scanf("%d%d",&x[i],&y[i]);
initgraph(&gd,&gm," ");
start:
cleardevice();
for(i=1;i<=n;i++)
{
x1[i]=x[i];
y1[i]=y[i];
}
for(i=1;i<n;i++)
line(x1[i],y1[i],x1[i+1],y1[i+1]);
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
line(x1[n],y1[n],x1[1],y1[1]);
gotoxy(1,1);
printf("\n1.X-SHEAR\n2.Y-SHEAR\n3.EXIT");
printf("\nEnter your choice");
switch(ch)
{
case 1:
gotoxy(1,5);
printf("\nEnter the shearing distance");
scanf("%d",&s);
for(i=0;i<n/2;i++)
x1[i]+=s;
break;
case 2:
gotoxy(1,5);
printf("\nEnter the shearing distance");
scanf("%d",&s);
for(i=1;i<=n/2;i++)
y1[i]+=s;
break;
case 3:
exit(0);
break;
default:
printf("\nEnter a valid choice");
getch();
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
goto start;
break;
}
for(i=1;i<n;i++)
line(x1[i],y1[i],x1[i+1],y1[i+1]);
line(x1[n],y[n],x1[1],y1[1]);
getch();
goto start;
}
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
OUTPUT:
SHEAR TRANSFORMATION
Enter the total number of Vertices: 3
Enter the co-ordinates:10 10
0 50
50 0
1.X-SHEAR
2.Y-SHEAR
3.EXIT
Enter your choice:1
Enter the shearing distance: 50
1.X-SHEAR
2.Y-SHEAR
3.EXIT
Enter your choice:2
Enter the shearing distance: 50
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
RESULT:
Thus the program for implementation of Two dimensional Transformations for Shearing of an object is successfully compiled and executed.
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
Ex No: 4 COMPOSITE 2D TRANSFORMATION
DATE:
AIM: To study and Implement 2D Composite Transformation
ALGORITHM:
STEP 1. Start the program
STEP 2. Include the required variables and the graphics mode specifying the path.
STEP 3. Get the vertices and get the coordinates(x,y)
STEP 4. Translate the object so that the rotation axis passes through the coordinate origin
STEP 5. Rotate the object so that the axis rotation coincides with one of the coordinate axes
STEP 6. Perform the specified rotation about that coordinate axis
STEP 7. Apply the inverse translation to bring the rotation axis back to its original position STEP 8. Stop the program
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
PROGRAM:
#include <glut.h>#include <stdlib.h>static int year = 0, day = 0;void init(void) { glClearColor (0.0, 0.0, 0.0, 0.0); glShadeModel (GL_FLAT);}void display(void){ glClear (GL_COLOR_BUFFER_BIT); glColor3f (1.0, 1.0, 1.0); glPushMatrix(); glutWireSphere(1.0, 20, 16); /* draw sun */ glRotatef ((GLfloat) year, 0.0, 1.0, 0.0); glTranslatef (2.0, 0.0, 0.0); glRotatef ((GLfloat) day, 0.0, 1.0, 0.0); glutWireSphere(0.2, 10, 8); /* draw smaller planet */ glPopMatrix(); glutSwapBuffers();}void reshape (int w, int h){ glViewport (0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode (GL_PROJECTION); glLoadIdentity (); gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);}void keyboard (unsigned char key, int x, int y){ switch (key) { case 'd': day = (day + 10) % 360; glutPostRedisplay(); break; case 'D': day = (day - 10) % 360; glutPostRedisplay(); break; case 'y': year = (year + 5) % 360; glutPostRedisplay(); break; case 'Y': year = (year - 5) % 360; glutPostRedisplay();
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
break; case 27: exit(0); break; default: break; }}int main(int argc, char** argv){ glutInit(&argc, argv); glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize (500, 500); glutInitWindowPosition (100, 100); glutCreateWindow (argv[0]); init (); glutDisplayFunc(display); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutMainLoop(); return 0;}
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
OUTPUT:
RESULT:
Thus the program for implementation of Composite 2D Transformation is successfully compiled and executed.
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
Ex No:5 COHEN SUTHERLAND 2D
DATE: LINE CLIPPING AND WINDOWING
AIM:
To study and Implement Cohen Sutherland 2D line clipping and Windowing
ALOGRITHM:
procedure CohenSutherlandLineClipAndDraw( x0,y0,x1,y1,xmin,xmax,ymin,ymax : real ; value: integer);{ Cohen-Sutherland clipping algorithm for line P0=(x1,y0) to P1=(x1,y1)and clip rectangle with diagonal from (xmin,ymin) to (xmax,ymax).}type edge = (LEFT,RIGHT,BOTTOM,TOP); outcode = set of edge;var accept,done : boolean; outcode0,outcode1,outcodeOut : outcode; {Outcodes for P0,P1, and whichever point lies outside the clip rectangle} x,y : real; procedure CompOutCode(x,y: real; var code:outcode); {Compute outcode for the point (x,y) } begin code := []; if y > ymax then code := [TOP] else if y < ymin then code := [BOTTOM]; if x > xmax then code := code +[RIGHT] else if x < xmin then code := code +[LEFT] end;
begin accept := false; done := false; CompOutCode (x0,y0,outcode0); CompOutCode (x1,y1,outcode1); repeat if(outcode0=[]) and (outcode1=[]) then {Trivial accept and exit} begin accept := true; done:=true end else if (outcode0*outcode1) <> [] then done := true {Logical intersection is true, so trivial reject and exit.} else {Failed both tests, so calculate the line segment to clip; from an outside point to an intersection with clip edge.} begin {At least one endpoint is outside the clip rectangle; pick it.} if outcode0 <> [] then outcodeOut := outcode0 else outcodeOut := outcode1; {Now find intersection point; use formulas y=y0+slope*(x-x0),x=x0+(1/slope)*(y-y0).}
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
if TOP in outcodeOut then begin {Divide line at top of clip rectangle} x := x0 + (x1 - x0) * (ymax - y0) / (y1 - y0); y := ymax end if BOTTOM in outcodeOut then begin {Divide line at bottom of clip rectangle} x := x0 + (x1 - x0) * (ymin - y0) / (y1 - y0); y := ymax end else if RIGHT in outcodeOut then begin {Divide line at right edge of clip rectangle} y := y0 + (y1 - y0) * (xmax - x0) / (x1 - x0); x := xmax end else if LEFT in outcodeOut then begin {Divide line at left edge of clip rectangle} y := y0 + (y1 - y0) * (xmin - x0) / (x1 - x0); x := xmin end; {Now we move outside point to intersection point to clip, and get ready for next pass.} if (outcodeOut = outcode0) then begin x0 := x; y0 := y; CompOutCode(x0,y0,outcode0) end else begin x1 := x; y1 := y; CompOutCode(x1,y1,outcode1); end end {subdivide} until done; if accept then MidpointLineReal(x0,y0,x1,y1,value) {Version for real coordinates}end; {CohenSutherlandLineClipAndDraw}
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
Program:
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
typedef unsigned int outcode;
enum { TOP=0x1, BOTTOM=0x2, RIGHT=0x4, LEFT=0x8 };
void lineclip(x0,y0,x1,y1,xwmin,ywmin,xwmax,ywmax )
float x0,y0,x1,y1,xwmin,ywmin,xwmax,ywmax;
{
int gd,gm;
outcode code0,code1,codeout;
int accept = 0, done=0;
code0 = calcode(x0,y0,xwmin,ywmin,xwmax,ywmax);
code1 = calcode(x1,y1,xwmin,ywmin,xwmax,ywmax);
do{
if(!(code0 | code1))
{ accept =1 ; done =1; }
else
if(code0 & code1) done = 1;
else
{
float x,y;
codeout = code0 ? code0 : code1;
if(codeout & TOP)
{
x = x0 + (x1-x0)*(ywmax-y0)/(y1-y0);
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
y = ywmax;
}
else
if( codeout & BOTTOM)
{
x = x0 + (x1-x0)*(ywmin-y0)/(y1-y0);
y = ywmin;
}
else
if ( codeout & RIGHT)
{
y = y0+(y1-y0)*(xwmax-x0)/(x1-x0);
x = xwmax;
}
else
{
y = y0 + (y1-y0)*(xwmin-x0)/(x1-x0);
x = xwmin;
}
if( codeout == code0)
{
x0 = x; y0 = y;
code0=calcode(x0,y0,xwmin,ywmin,xwmax,ywmax);
}
else
{
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
x1 = x; y1 = y;
code1 = calcode(x1,y1,xwmin,ywmin,xwmax,ywmax);
}
}
} while( done == 0);
if(accept) line(x0,y0,x1,y1);
rectangle(xwmin,ywmin,xwmax,ywmax);
getch();
}
int calcode (x,y,xwmin,ywmin,xwmax,ywmax)
float x,y,xwmin,ywmin,xwmax,ywmax;
{
int code =0;
if(y> ywmax)
code |=TOP;
else if( y<ywmin)
code |= BOTTOM;
else if(x > xwmax)
code |= RIGHT;
else if ( x< xwmin)
code |= LEFT;
return(code);
}
main()
{
float x2,y2,x1,y1,xwmin,ywmin,xwmax,ywmax;
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
int gd=DETECT,gm;
clrscr();
initgraph(&gd,&gm,"c:\\tc\\bgi");
printf("\n\n\tEnter the co-ordinates of Line :");
printf("\n\n\tX1 Y1 : ");
scanf("%f %f",&x1,&y1);
printf("\n\n\tX2 Y2 : ");
scanf("%f %f",&x2,&y2);
printf("\n\tEnter the co_ordinates of window :\n ");
printf("\n\txwmin , ywmin : ");
scanf("%f %f",&xwmin,&ywmin);
printf("\n\txwmax , ywmax : ");
scanf("%f %f",&xwmax,&ywmax);
clrscr();
line(x1,y1,x2,y2);
rectangle(xwmin,ywmin,xwmax,ywmax);
getch();
clrscr();
lineclip(x1,y1,x2,y2,xwmin,ywmin,xwmax,ywmax );
getch();
closegraph();
}
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
OUTPUT:Enter the C0-ordinates of line
x1,y1: 150 200x2,y2: 300 450Enter the Co-ordinates of windowxwmin,ywmin: 150 180xwmax,ywmax: 420 390Before Clipping
After Clipping
RESULT:
Thus the program for implementation of Cohen Sutherland 2-D Line, Clipping and Windowing are successfully compiled and executed.
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
EX NO. 6 SUTHERLAND – HODGEMAN POLYGON CLIPPING
DATE:
AIM: To study and implement Sutherland – Hodgeman polygon clipping using TC.
ALGORITHM:
type vertex = point; {point hold real x,y} edge = array[1..2] of vertex; vertexArray = array[1..MAX] of vertex; {MAX is a declared constant}
procedure SutherlandHodgmanPolygoClip ( inVertexArray : vertexArray; {Input vertex array} var outVertexArray : vertexArray; {Output vertex array} inLength : integer; {Number of entries in inVertexArray} var outLength : integer; {Number of entries in outVertexArray} clipBoundary : edge); {Edge of clip polygon}var s,p, {Start, end point of current polygon edge} i : vertex; {Intersection point with a clip boundary} j : integer; {Vertex loop counter}
procedure Output( newVertex : vertex; var outLength : integer; var outVertexArray : vertexArray); {Adds newVertex to outVertexArray and then updates outLength } begin ... end;
function Inside(testVertex : vertex; clipBoundary : edge):boolean; {Checks whether the vertex lies inside the clip edge or not} begin ... end;
procedure Intersect(first,second:vertex; clipBoundary:edge; var intersectPt:vertex); {Clips polygon edge (first,second) against clipBoundary, outputs the new point} begin ... end;
begin outLength := 0; s := inVertexArray[inLength]; {Start with the last vertex in inVertexArray}
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
for j := 1 to inLength do begin p := inVertexArray[j]; {Now s and p correspond to the vertices in Fig. 3.48} if Inside(p,clipBoundary) then {Cases 1 and 4} if Inside(s, clipBoundary) then Output(p, outLength, outVertexArray) {Case 1} else begin Intersect(s, p, clipBoundary, i); Output(i, outLength, outVertexArray); Output(p, outLength, outVertexArray) end else {Cases 2 and 3} if Inside(s, clipBoundary) then {Cases 2} begin Intersect(s, p, clipBoundary, i); Output(i, outLength, outVertexArray) end; {No action for case 3} s := p {Advance to next pair of vertices} end {for}end; {SutherlandHodgmanPolygonClip}
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
Program:
#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#define round(a)((int)(a+0.5))
int k;
float xmin,ymin,xmax,ymax,arr[20],m;
void clipl(float x1,float y1,float x2,float y2)
{
if(x2-x1)
m=(y2-y1)/(x2-x1);
else
m=100000;
if(x1>=xmin && x2>=xmin)
{
arr[k]=x2;
arr[k+1]=y2;
k+=2;
}
if(x1<xmin && x2>=xmin)
{
arr[k]=xmin;
arr[k+1]=y1+m*(xmin-x1);
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
}
if(x1>=xmin && x2<xmin)
{
arr[k]=xmin;
arr[k+1]=y1+m*(xmin-x1);
k+=2;
}
}
void clipt(float x1,float y1,float x2,float y2)
{
if(y2-y1)
m=(x2-x1)/(y2-y1);
else
m=100000;
if(y1<=ymax && y2<=ymax)
{
arr[k]=x2;
arr[k+1]=y2;
k+=2;
}
if(y1>ymax && y2<=ymax)
{
arr[k]=x1+m*(ymax-y1);
arr[k+1]=ymax;
arr[k+2]=x2;
arr[k+3]=y2;
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
k+=4;
}
if(y1<=ymax && y2>ymax)
{
arr[k]=x1+m*(ymax-y1);
arr[k+1]=ymax;
k+=2;
}
}
void clipr(float x1,float y1,float x2,float y2)
{
if(x2-x1)
m=(y2-y1)/(x2-x1);
else
m=100000;
if(x1<=xmax && x2<=xmax)
{
arr[k]=x2;
arr[k+1]=y2;
k+=2;
}
if(x1>xmax && x2<=xmax)
{
arr[k]=xmax;
arr[k+1]=y1+m*(xmax-x1);
arr[k+2]=x2;
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
arr[k+3]=y2;
k+=4;
}
if(x1<=xmax && x2>xmax)
{
arr[k]=xmax;
arr[k+1]=y1+m*(xmax-x1);
k+=2;
}
}
void clipb(float x1,float y1,float x2,float y2)
{
if(y2-y1)
m=(x2-x1)/(y2-y1);
else
m=100000;
if(y1>=ymin && y2>=ymin)
{
arr[k]=x2;
arr[k+1]=y2;
k+=2;
}
if(y1<ymin && y2>=ymin)
{
arr[k]=x1+m*(ymin-y1);
arr[k+1]=ymin;
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
}
if(y1>=ymin && y2<ymin)
{
arr[k]=x1+m*(ymin-y1);
arr[k+1]=ymin;
k+=2;
}
}
void main()
{
int gdriver=DETECT,gmode;
int n,poly[20];
float xi,yi,xf,yf,polyy[20];
clrscr();
cout<<" Coordinates of rectangular clip window :\n xmin,ymin :";
cin>>xmin>>ymin;
cout<<"xmax,ymax :";
cin>>xmax>>ymax;
cout<<"\n\n Polygon to be clipped: \n No of sides :";
cin>>n;
cout<<"Enter the coordinates:";
for(int i=0;i<2*n;i++)
cin>>polyy[i];
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
polyy[i]=polyy[0];
poly[i+1]=polyy[1];
for(i=0;i<2*n+2;i++)
poly[i]=round(polyy[i]);
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
setcolor(RED);
rectangle(xmin,ymax,xmax,ymin);
cout<<"\t\t UNCLIPPED POLYGON";
setcolor(WHITE);
fillpoly(n,poly);
getch();
cleardevice();
k=0;
for(i=0;i<2*n;i+=2)
clipl(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
n=k/2;
for(i=0;i<k;i++)
polyy[i]=arr[i];
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
k=0;
for(i=0;i<2*n;i+=2)
clipt(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
n=k/2;
for(i=0;i<k;i++)
polyy[i]=arr[i];
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
k=0;
for(i=0;i<2*n;i+=2)
clipr(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
n=k/2;
for(i=0;i<k;i++)
polyy[i]=arr[i];
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
k=0;
for(i=0;i<2*n;i+=2)
clipb(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
for(i=0;i<k;i++)
poly[i]=round(arr[i]);
if(k)
fillpoly(k/2,poly);
setcolor(RED);
rectangle(xmin,ymax,xmax,ymin);
cout<<"\t CLIPPED POLYGON";
getch();
closegraph();
}
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
OUTPUT:
Co ordinates of regular clip window:
Xmin,ymin:150 180
Xmax,ymax:200 260
Polygon to be clipped:
No of sides:4
Enter the co-ordinates:10 20
30 45
50 60
70 80
Unclipped Window:
Clipped Window:
RESULT:
Thus the program for implementation of Sutherland Hodgeman Polygon Clipping is successfully compiled and executed.
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
Ex No:7 THREE DIMENSIONAL TRANSFORMATIONS
DATE: TRANSLATION, ROTATION, SCALING
AIM: To study and Implement 3D Transformation
ALGORITHM:
STEP 1:Start the program
STEP 2:Include the required variables and the graphics mode specifying the path.STEP 3:Get the vertices and get the coordinates in each case.STEP 4:Get the options then using the suited case to follow transformation.STEP 5:If the option1 get the tx ,ty and tz coordinate then perform the translation operation.
STEP 6:Repeat the operation for all the coordinates.STEP 7:If option2 get the sx ,sy and sz coordinates then perform the scaling operation
STEP 8:Repeat the operation for all coordinates.STEP 9:Display the output result. If the option is 3 get the rotation angle(r) then perform the rotation operation and find the r1=r1*(3.14)/(180)
STEP 10.To perform rotation use switch case,case1 for x direction rotation,case 2 for y direction rotation and case 3 for z direction rotation ,case 4 for exit operation. In x direction rotation, perform the operation
In y direction rotation, perform the operation
In z direction rotation, perform the operation
STEP 11. Display the output resultSTEP 12:If option 4 exit the programSTEP 13:Stop the program.
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int maxx,maxy,midx,midy;
void axis()
{
getch();
cleardevice();
line(midx,0,midx,maxy);
line(0,midy,maxx,midy);
}
void main()
{
int gd,gm,x,y,z,o,x1,x2,y1,y2;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"d:\\tc\\bgi");
setfillstyle(0,getmaxcolor());
maxx=getmaxx();
maxy=getmaxy();
midx=maxx/2;
midy=maxy/2;
axis();
bar3d(midx+100,midy-150,midx+60,midy-100,10,1);
printf("\Enter the translation factor");
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
scanf("%d%d",&x,&y);
axis();
printf("After translation");
bar3d(midx+100,midy-150,midx+60,midy-100,10,1);
bar3d(midx+x+100,midy-(y+150),midx+x+60,midy-(y+100),10,1);
axis();
bar3d(midx+100,midy-150,midx+60,midy-100,10,1);
printf("Enter the scaling factor");
scanf("%d%d%d",&x,&y,&z);
axis();
printf("After scaling");
bar3d(midx+100,midy-150,midx+60,midy-100,10,1);
bar3d(midx+(x*100),midy-(y*150),midx+(x*60),midy-(y*100),10*z,1);
axis();
bar3d(midx+100,midy-150,midx+60,midy-100,10,1);
printf("Enter the rotation angle");
scanf("%d",&o);
x1=50*cos(o*3.14/180)-100*sin(o*3.14/180);
y1=50*sin(o*3.14/180)+100*cos(o*3.14/180);
x2=60*cos(o*3.14/180)-90*sin(o*3.14/180);
y2=60*sin(o*3.14/180)+90*cos(o*3.14/180);
axis();
printf("After rotating about Z-axis");
bar3d(midx+100,midy-150,midx+60,midy-100,10,1);
bar3d(midx+x1,midy-y1,midx+x2,midy-y2,10,1);
axis();
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
printf("After rotating about x-axis");
bar3d(midx+100,midy-150,midx+60,midy-100,10,1);
bar3d(midx+100,midy-x1,midx+60,midy-x2,10,1);
axis();
printf("After rotating about Y-axis");
bar3d(midx+100,midy-150,midx+60,midy-100,10,1);
bar3d(midx+x1,midy-150,midx+x2,midy-100,10,1);
getch();
closegraph();
}
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
OUTPUT:
Enter the translation factor: 10 10 10 10
After translation:
Enter scaling factor: 10 20 30
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
After scaling:
Enter rotation angle: 20
After Rotaion about z-axis:
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
After Rotaion about x-axis:
After Rotaion about y-axis:
RESULT:
Thus the program for implementation of Three Dimensional transformations, Scaling and Rotation are successfully compiled and executed.
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
Ex No:8 COMPOSITE 3D TRANSFORMATIONS
DATE:
AIM: To study and Implement 3D Composite Transformation
ALGORITHM:
STEP 1:Start the program STEP 2: Include the required variables and the graphics mode specifying the path.
STEP 3: Get the vertices and get the coordinates(x,y,z)
STEP 4: Translate the object so that the rotation axis passes through the coordinate origin
STEP 5: Rotate the object so that the axis rotation coincides with one of the coordinate axes
STEP 6: Perform the specified rotation about that coordinate axis
STEP 7: Apply the inverse translation to bring the rotation axis back to its original position
STEP 8: Stop the program
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
PROGRAM:
#include "stdafx.h"#include <glut.h>#include <stdlib.h>static int shoulder = 0, elbow = 0;void init(void) { glClearColor (0.0, 0.0, 0.0, 0.0); glShadeModel (GL_FLAT);}void display(void){ glClear (GL_COLOR_BUFFER_BIT); glPushMatrix(); glTranslatef (-1.0, 0.0, 0.0); glRotatef ((GLfloat) shoulder, 0.0, 0.0, 1.0); glTranslatef (1.0, 0.0, 0.0); glPushMatrix(); glScalef (2.0, 0.4, 1.0); glutWireCube (1.0); glPopMatrix();
glTranslatef (1.0, 0.0, 0.0); glRotatef ((GLfloat) elbow, 0.0, 0.0, 1.0); glTranslatef (1.0, 0.0, 0.0); glPushMatrix(); glScalef (2.0, 0.4, 1.0); glutWireCube (1.0); glPopMatrix();
glPopMatrix(); glutSwapBuffers();}void reshape (int w, int h){ glViewport (0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode (GL_PROJECTION); glLoadIdentity (); gluPerspective(65.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef (0.0, 0.0, -5.0);}void keyboard (unsigned char key, int x, int y){ switch (key) { case 's': shoulder = (shoulder + 5) % 360; glutPostRedisplay(); break;
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
case 'S': shoulder = (shoulder - 5) % 360; glutPostRedisplay(); break; case 'e': elbow = (elbow + 5) % 360; glutPostRedisplay(); break; case 'E': elbow = (elbow - 5) % 360; glutPostRedisplay(); break; case 27: exit(0); break; default: break; }}int main(int argc, char** argv){
glutInit(&argc, argv); glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize (500, 500); glutInitWindowPosition (100, 100); glutCreateWindow (argv[0]); init (); glutDisplayFunc(display); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutMainLoop(); return 0;}
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
OUTPUT:
RESULT:
Thus the program for implementation of Implement 3D Composite Transformation is successfully compiled and executed.
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
Ex No:9 DRAWING THREE DIMENSIONAL OBJECTS AND SCENES
DATE:
AIM: To study and Drawing three dimensional objects and Scenes
ALGORITHM:
OpenGL Command Syntax
● Uses the prefix gl● Capital letters for each word making up the command name. E.g. glClearColor()● The prefix glu is used when the OpenGL command is from the utility library.● The prefix glX is used when the OpenGL command is from the X-window system.● Constants begin with GL_ . And is all written in capital letters. E.g. GL_COLOR_BUFFER_BIT.● Root names are glColor() and glVertex()
Drawing Geometric Primitives
● glVertex{2,3,4}{s,i,f,d}[v]() can be used to create a set of points, lines or a filled polygon.
● Example: glBegin(GL_POLYGON); glVertex2f(0.0,0.0); glVertex2f(0.0,3.0); glVertex2f(4.0,3.0); glVertex2f(6.0,1.5); glVertex2f(4.0,0.0); glEnd();
Specifying Colors
glColor3f(1.0,0.0,0.0); (Red) Is the same as float red[3] = {1.0,0.0,0.0};glColor3fv(red); v means pointer to a vectorClearing Buffers glClearColor(0.0,0.0,0.0,0.0); (RGB mode)glClearDepth(0.0);glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT);Forcing Completion of Drawing glFlush(); makes sure the OpenGL commands are executed in finite time.
Window Management using GLUT
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
● glutInitDisplayMode() - tells whether to create RGB or color index window; a single- or double buffered window can be specified; a depth buffer can also be specified.● glutInitWindowSize() - indicates the windows’ size in pixels.● glutInitWindowPosition() - tells where to position a window on the screen.● glutCreateWindow() - opens a window on the screen and returns a unique identifier for the window.
Handling Input Events using GLUT
glutReshapeFunc() - what actions to be taken when the window is resized, moved or exposed.glutKeyboardFunc() - links a key with a routine that is invoked when a key is pressed. glutSpecialFunc() - F1, F2, F3 etc.glutMouseFunc() - links a mouse button with a routine that is invoked when the mouse button is pressed/released.glutMotionFunc() - is called when the mouse moves within the window while one or more buttons are pressed. Also,glutPassiveMotionFunc() (no buttons pressed).
Attributes of Output Primitives
glPointSize(size); Default is 1.0.glLineWidth(width); Default is 1.0.Default line type attribute is solid.gLineStripple(repeatfactor,pattern); Default pattern 0xFFFF.For example, glLineStripple(1,0x3F07); ex. lines.cglEnable(GL_LINE_STRIPPLE); Turn off: glDisableglPolygonStripple(filpattern); ex. polys.cglEnable(GL_POLYGON_STRIPPLE);
Hidden Surface Removal
● When setting up your window, specify a dept buffer: glutInitDisplayMode(…|…| GLUT_DEPTH);● When clearing, make sure to: glClear(GL_DEPTH_BUFFER_BIT);● glEnable(GL_DEPTH_TEST);● Set the depth test comparison operation: glDepthFunc(GL_LESS); (this is the default)
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
PROGRAM:
#include <windows.h>#include <glut.h>void drawCube(){ glBegin(GL_QUADS); glColor3f(1,0,0); glVertex3f(1,1,1); glVertex3f(-1,1,1); glVertex3f(-1,-1,1); glVertex3f(1,-1,1); glColor3f(0,1,1); glVertex3f(1,1,-1); glVertex3f(-1,1,-1); glVertex3f(-1,-1,-1); glVertex3f(1,-1,-1); glColor3f(0,1,0); glVertex3f(1,1,1); glVertex3f(1,-1,1); glVertex3f(1,-1,-1); glVertex3f(1,1,-1); glColor3f(1,0,1); glVertex3f(-1,1,1); glVertex3f(-1,-1,1); glVertex3f(-1,-1,-1); glVertex3f(-1,1,-1); glColor3f(0,0,1); glVertex3f(1,1,1); glVertex3f(-1,1,1); glVertex3f(-1,1,-1); glVertex3f(1,1,-1); glColor3f(1,1,0); glVertex3f(1,-1,1); glVertex3f(-1,-1,1); glVertex3f(-1,-1,-1); glVertex3f(1,-1,-1); glEnd();}void idle(){ glutPostRedisplay();}float deg= 0.0;void display(){ glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0,0,-3); deg += 0.01; glRotatef(2*deg,0,0,1); glRotatef(deg,0,1,0); drawCube();
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
glutSwapBuffers();}void reshape(int width,int height){ glMatrixMode(GL_PROJECTION); glLoadIdentity(); glViewport(0,0,width,height); float fovy = 60; float aspect_ratio = float(width) / height; float near_clip = 0.01; float far_clip = 100; gluPerspective(fovy, aspect_ratio, near_clip, far_clip ); glMatrixMode(GL_MODELVIEW);}void key(unsigned char key,int x,int y){ if(key == 27)//esc exit(0);}void init(){ glutInitWindowSize(800,600); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); glutCreateWindow("Tutorial Basics"); glutIdleFunc(idle); glutDisplayFunc(display); glutReshapeFunc(reshape); glutKeyboardFunc(key); glEnable(GL_DEPTH_TEST);}int main(int argc, char ** argv){ glutInit(&argc, argv); init(); glutMainLoop(); return 0;}
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
OUTPUT:
RESULT:
Thus the program for implementation of three dimensional objects and Scenes is successfully compiled and executed.
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
Ex No:10 GENERATING FRACTAL IMAGES
DATE:
AIM:
To study and Generating fractal images using blender.
ALGORITHM:
Step 1: Create a sphere of any size. Under the Parameters tab, put “100” in Segments to ensure there will be no jaggies around the asteroids and to make it smooth.
Step 2: Go into the Modifier dialog and apply a noise modifier. In the Noise parameters, apply these values: Seed 2, Scale 30, Click on the FractalCheckbox, X 40, Y 5, Z 20.
Step 3: Now that we’ve got our basic asteroid shape, it’s time to give it a material. This is what will make it seem real. In the main window, press M.The material editor box will pop up. Choose any box. Under shader properties, choose Blinn (this will give it realistic shading). Specular level 5 & Glossiness 25. Now we're going to give it a material.Next to Diffuse click the little box that has an “M” in it. When the window pops up, choose Bitmap. Go into your 3D Studio Max directory into 3Dsmax5\maps\Space and choose Moon.jpg, this will be the material for the asteroid. Now, go back to the main material window. Under the maps properties, click the checkbox next to Bump and set the value to 100. Click the box next to it and choose Bitmap and choose the same moon.jpg as before. Now if you want to see how the material will look in the scene, click the little blue checkered box.
Step 4: You’ve got your asteroid now. This is the long part. If you want to make more than 1 asteroid, all you have to do is clone the asteroid. You can clone it by selecting the asteroid, then click on Edit-Clone (on the top bar next to File) and click OK. You can rotate it, scale it to different sizes and move it around. To make the process quicker, you select the two asteroids and click Group-Group and both asteroids become one. Now you clone them and you get 2 instead of 1. Keep selecting all the asteroids then group them. It multiplies, so you don’t have to do each one separately.
Step 5: Adding light. You can add light to simulate where the light source is coming from. To do this, you put an omni light into the scene. Click the Lights icon. Place the omni light anywhere you want. To change the intensity and whatnot, go into Lights&Cameras tab and click on Light Lister. In the window, you can change the multiplier and shadows. For more realism, you can add decay. Under the Decay list, choose inverse.To change the intensity of decay, change the value of Start.
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024
Step 6: Rendering. When you like how your scene looks, click Render. Under Anti-Aliasing properties - on the Filter drop down list choose Catmull-Rom. Deselect the Filter Maps checkbox. This will give the asteroids a more rugged and less blurry look. Render the scene.
RESULT:
Thus Generating fractal images using 3ds max is executed and Output is verified successfully.
SRIRAM ENGINEERING COLLEGE PERUMALPATTU-602024