You are on page 1of 98

Department of Computer Science and Engineering /Dr.N.G.

P IT

Dr. N.G.P. INSTITUTE OF TECHNOLOGY

Coimbatore-641048

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

LAB MANUAL

COMPUTER GRAPHICS LABORATORY (R2013)

Prepared by Approved by
Ms.G.Selva Priya,AP/CSE HoD/CSE
Department of Computer Science and Engineering /Dr.N.G.P IT

BRESENHAM’S LINE DRAWING ALGORITHM

AIM:
To implement Bresenham’s line drawing Algorithm for drawing lines.

FUNCTIONS USED:

Line()

The function line() is used to draw a line from(x1,y1)to (x2,y2)
Department of Computer Science and Engineering /Dr.N.G.P IT

Syntax:
line (x1,y1,x2,y2)

initgraph().

This function takes thee arguments and they are
i).the video driver to be used (gd).
ii).the graphics mode (gm).
iii).the path name.

Syntax:
Initgraph(gd,gm,path)

ALGORITHM:

Step 1: Start
Step 2: Get the values of the end points as(x1, y1) &(x2, y2)
Step 3: Assign x=x1, y=y1;
Step 4: Compute dx=x2-x1
Step 5: Compute dy=y2-y1
Step 6: Assign sx=x2-x1, sy=y2-y1
Step 7: If dy>dx then interchange the values of dx and dy and assign exch=1
Step 8: Compute p=2xdy-dx
Step 9: Put a pixel on(x,y)
Step 10: If exch=1, y=sy else x=x+sx
Step 11: If p>0 and exch =1, x=x+sx else y=y+sy, p=p-2xdx
Step 12: Compute p=p+2xdy
Step 13: Do steps (9) t0 (12) for dx times
Step 14: Stop

PROGRAM:

#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
int signs(int m);
void bres_line(int,int,int,int);
void main()
{
int gd=DETECT,gm;
int x1,x2,y1,y2;
initgraph(&gd,&gm,"");
cout<<"enter starting value of X-Axis----->";
cin>>x1;
cout<<"enter starting value of Y-Axis----->";
Department of Computer Science and Engineering /Dr.N.G.P IT

cin>>y1;
cout<<"enter ending value of X-Axis----->";
cin>>x2;
cout<<"enter ending value of y-Axis----->";
cin>>y2;
bres_line(x1,y1,x2,y2);
getch();
closegraph();
}
void bres_line(int x1,int y1,int x2,int y2)
{
int x,y,sx,sy,p,temp,exch=0,i,dx,dy;
x=x1;
y=y1;
dy=abs(y1-y2);
dx=abs(x1-x2);
sx=signs(x2-x1);
sy=signs(y2-y1);
if(dy>dx)
{
temp=dx;
dx=dy;
dy=temp;
exch=1;
}
p=2*dy-dx;
for(i=0;i<=dx;i++)
{putpixel(x,y,15);
if(exch==1)
y+=sy;
else
x+=sx;
if(p>=0)
{
if(exch==1)
x+=sx;
else
y+=sy;p=p-2*dx;}
p=p+2*dy;
}
}
int signs(int m)
{
if(m<0)
return(-1);
else if(m>0)
return(1);
Department of Computer Science and Engineering /Dr.N.G.P IT

else
return(0);
}

OUTPUT:

RESULT:

Thus the program to draw line using Bresenham’s line drawing Algorithm was executed
successfully.

CIRCLE DRAWING

AIM:
To write a program to draw a circle using Bresenham’s circle drawing Algorithm.

FUNCTIONS USED:

Circle()
Department of Computer Science and Engineering /Dr.N.G.P IT

The function circle() is used to draw a circle using(x,y) as centre point.

Syntax:
circle (x,y,radius)

initgraph().

This function takes thee arguments and they are
i).the video driver to be used (gd).
ii).the graphics mode (gm).
iii).the path name.

Syntax:
Initgraph(gd,gm,path)

Putpixel ()

The function putpixel() is used to place a pixel at particular coordinate

Syntax:
Putpixel(x,y,color)

ALGORITHM:

Step 1: Start
Step 2: Get the center point as (xc,yc),get the radius as r.
Step 3: Assign y=r,x=0
Step 4: Calculate p=3-2r
Step 5: If p<0,p=p+4x+6 else p=p+10+4(x-y) and y=y-1
Step 6: Increment x by 1
Step 7: Do steps (9) to (16)
Step 8: Repeat steps (5) to (9) until x<=y
Step 9: Put a pixel on (xc+x,yc+y,15);
Step 10: Put a pixel on (xc+x,yc-y,15);
Step 11: Put a pixel on (xc-x,yc+y,15);
Step 12: Put a pixel on (xc-x, yc-y, 15);
Step 13: Put a pixel on (xc+y,yc+x,15);
Step14: Put a pixel on (xc+y, yc-x, 15);
Step 15: Put a pixel on (xc-y, yc+x, 15);
Step 16: Put a pixel on (xc-y, yc-x, 15);
Step 17: Stop.

PROGRAM:

#include<iostream.h>
#include<graphics.h>
#include<conio.h>
Department of Computer Science and Engineering /Dr.N.G.P IT

#include<math.h>
void my_circle(int,int,int);
void plot(int,int,int,int);
void main()
{
int gd=DETECT,gm;
int xc,yc,r;
initgraph(&gd,&gm,"");
cout<<"enter X-Axis----->";
cin>>xc;
cout<<"enter Y-Axis----->";
cin>>yc;
cout<<"enter radius----->";
cin>>r;
my_circle(xc,yc,r);
getch();
closegraph();
}
void my_circle(int xc,int yc,int r)
{
int x,y,p;
p=3-2*r;
x=0;
y=r;
while(x<=y)
{
if(p<0)
p=p+(4*x+6);
else
{
p=p+10+4*(x-y);
y=y-1;
}
plot(xc,yc,x,y);
x=x+1;
}
}
void plot(int xc,int yc,int x,int y)
{
putpixel(xc+x,yc+y,15);
putpixel(xc+x,yc-y,15);
putpixel(xc-x,yc+y,15);
putpixel(xc-x,yc-y,15);
putpixel(xc+y,yc+x,15);
putpixel(xc+y,yc-x,15);
putpixel(xc-y,yc+x,15);
putpixel(xc-y,yc-x,15);
Department of Computer Science and Engineering /Dr.N.G.P IT

}

OUTPUT:

RESULT:

Thus the program to draw circle using Bresenham’s circle drawing Algorithm was executed
successfully.

ELLIPSE DRAWING

AIM:
To write a program to draw a ellipse using Bresenham’s ellipse drawing Algorithm.

FUNCTIONS USED:

initgraph().
Department of Computer Science and Engineering /Dr.N.G.P IT

This function takes thee arguments and they are
i).the video driver to be used (gd).
ii).the graphics mode (gm).
iii).the path name.

Syntax:
Initgraph(gd,gm,path)

Putpixel ()

The function putpixel() is used to place a pixel at particular coordinate

Syntax:
Putpixel(x,y,color)

ALGORITHM:
Step 1: Start
Step 2: Get the center point as(x1, y1)
Step 3: Get the length of semi-major, semi-minor axes as r1 & r2
Step 4: Calculate t=pi/180
Step 5: Initialise i=0;
Step 6: Compute d=i*t
Step 7: Compute x=x1+y1*sin(d), y=y1+r2*cos(d).
Step 8: Put a pixel on(x,y)
Step 9: Increment I by 1
Step 10: Repeat steps(6) to (9) until i<360
Step 11: Stop

PROGRAM:

#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
void main()
{
int gd=DETECT,gm,x1=0,y1=0,r1=0,r2=0;
float x=0,y=0,t,d,i;
initgraph(&gd,&gm,"");
cout<<"enter the center co-or:";
cin>>x1>>y1;
cout<<"enter the radius1:";
cin>>r1;
cout<<"enter radius2:";
cin>>r2;
Department of Computer Science and Engineering /Dr.N.G.P IT

for(i=0;i<360;i++)
{
t=3.14/180;
d=i*t;
x=x1+ceil(r1*sin(d));
y=y1+ceil(r2*cos(d));
putpixel(x,y,15);
}
getch();
closegraph();
}

OUTPUT:

RESULT:

Thus the program to draw ellipse using Bresenham’s ellipse drawing Algorithm was executed
successfully.

IMPLEMENTATION OF LINE, CIRCLE AND ELLIPSE ATTRIBUTES

AIM:
To write a program to draw a Line, Circle and ellipse Attributes
Department of Computer Science and Engineering /Dr.N.G.P IT

FUNCTIONS USED:

Circle()

The function circle() is used to draw a circle using(x,y) as centre point.

Syntax:
circle (x,y,radius)

initgraph().

This function takes thee arguments and they are
i).the video driver to be used (gd).
ii).the graphics mode (gm).
iii).the path name.

Syntax:
Initgraph(gd,gm,path)

Putpixel ()

The function putpixel() is used to place a pixel at particular coordinate

Syntax:
Putpixel(x,y,color)

ALGORITHM:

Step 1: Start
Step 2: Get the center point as (xc,yc),get the radius as r.
Step 3: Assign y=r,x=0
Step 4: Calculate p=3-2r
Step 5: If p<0,p=p+4x+6 else p=p+10+4(x-y) and y=y-1
Step 6: Increment x by 1
Step 7:stop

PROGRAM:

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#define ROUND(a) ((int)(a+0.5))
//Line drawing
void lineBres(int xa,int ya,int xb,int yb)
{
Department of Computer Science and Engineering /Dr.N.G.P IT

int dx=abs(xa-xb),dy=abs(ya-yb);
int p=2*dy-dx;
int twoDy=2*dy,twoDyDx=2*(dy-dx);
int x,y,xEnd;
/* Determine which point to use as start,which as end */
if(xa>xb)
{
x=xb;
y=yb;
xEnd=xa;
}
else
{
x=xa;
y=ya;
xEnd=xb;
}
putpixel(x,y,15);
while(x<xEnd)
{
x++;
if(p<0)
p+=twoDy;
else
{
y++;
p+=twoDyDx;
}
putpixel(x,y,15);
}
}
//Circle drawing
void circleMidPoint(int xCenter,int yCenter,int radius)
{
int x=0;
int y=radius;
int p=1-radius;
void circlePlotPoints(int,int,int,int);
/*plot first set of points*/
circlePlotPoints(xCenter,yCenter,x,y);
while(x<y)
{
x++;
if(p<0)
p+=2*x+1;
else
{
Department of Computer Science and Engineering /Dr.N.G.P IT

y–;
p+=2*(x-y)+1;
}
circlePlotPoints(xCenter,yCenter,x,y);
}
}
void circlePlotPoints(int xCenter,int yCenter,int x,int y)
{
putpixel(xCenter+x,yCenter+y,WHITE);
putpixel(xCenter-x,yCenter+y,WHITE);
putpixel(xCenter+x,yCenter-y,WHITE);
putpixel(xCenter-x,yCenter-y,WHITE);
putpixel(xCenter+y,yCenter+x,WHITE);
putpixel(xCenter-y,yCenter+x,WHITE);
putpixel(xCenter+y,yCenter-x,WHITE);
putpixel(xCenter-y,yCenter-x,WHITE);
}//Ellipse drawing
void ellipsemidpoint(int xcenter,int ycenter,int rx,int ry)
{
int rx2=rx*rx;
int ry2=ry*ry;
int tworx2=2*rx2;
int twory2=2*ry2;
int p,x=0,y=ry,px=0;
int py=tworx2*y;
void ellipseplotpoints(int,int,int,int);
ellipseplotpoints(xcenter,ycenter,x,y);
p=ROUND(ry2-(rx2*ry)+(0.25*rx2));
while(px<py)
{
x++;
px+=twory2;
if(p<0)
p+=ry2+px;
else
{
y–;
py-=tworx2;
p+=ry2+px-py;
}
ellipseplotpoints(xcenter,ycenter,x,y);
}
p=ROUND(ry2*(x+0.5)*(x+0.5)+rx2*(y-1)*(y-1)-rx2*ry2);
while(y>0)
{
y–;
py-=tworx2;
Department of Computer Science and Engineering /Dr.N.G.P IT

if(p>0)
p+=rx2-py;
else
{
x++;
px+=twory2;
p+=rx2-px+px;
}
ellipseplotpoints(xcenter,ycenter,x,y);
}
}
void ellipseplotpoints(int xcenter,int ycenter,int x,int y)
{
putpixel(xcenter+x,ycenter+y,5);
putpixel(xcenter-x,ycenter+y,5);
putpixel(xcenter+x,ycenter-y,5);
putpixel(xcenter-x,ycenter-y,5);
}
void main()
{
int ch,c;
co:
clrscr();
printf(“\n\t\tBRESENHAM BDRAWINGS\n”);
printf(“\n\t\t1-Line drawing”);
printf(“\n\t\t2-Circle drawing”);
printf(“\n\t\t3-Ellipse drawing”);
printf(“\n\t\t4-Exit”);
printf(“\nEnter your choice :”);
scanf(“%d”,&ch);
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, ” “);
switch(ch)
{
case 1:
int x1,y1,x2,y2;
printf(“Enter the starting co-ordinates: “);
scanf(“%d %d”,&x1,&y1);
printf(“Enter the ending co-ordinates: “);
scanf(“%d %d”,&x2,&y2);
lineBres(x1,y1,x2,y2);
getch();
printf(“\n\n\n\n\n\n\n\n\nDo u continue(1-yes,0-no):”);
scanf(“%d”,&c);
if (c==1)
goto co;
break;
Department of Computer Science and Engineering /Dr.N.G.P IT

case 2:
int xc1,yc1,r;
printf(“Enter the centre co-ordinates: “);
scanf(“%d %d”,&xc1,&yc1);
printf(“Enter the radius: “);
scanf(“%d”,&r);
circleMidPoint(xc1,yc1,r);
getch();
printf(“\n\n\n\n\n\n\n\n\nDo u continue(1-yes,0-no):”);
scanf(“%d”,&c);
goto co;
break;
case 3:
int xc,yc,rx,ry;
printf(“Enter the value of xcenter and ycenter co-ordinates: “);
scanf(“%d %d”,&xc,&yc);
printf(“Enter the radius of x and y: “);
scanf(“%d %d”,&rx,&ry);
ellipsemidpoint(xc,yc,rx,ry);
getch();
printf(“\n\n\n\n\n\n\n\n\nDo u continue(1-yes,0-no):”);
scanf(“%d”,&c);
if (c==1)

goto co;
break;
case 4: break;
}
}
INPUT:
Ellipse Drawing Algorithm
Enter the co-ordinates
Xc = 200
Yc = 200
A = 100
B = 70

OUTPUT:

RESULT:
Thus the program to draw. Implementation of Line, Circle and ellipse Attributes was executed successfully.

2D TRANSFORMATION
Department of Computer Science and Engineering /Dr.N.G.P IT

Aim:
To perform the 2D transformation such as translation, rotation, scaling, shearing,
Reflection

Function used:
Line()
The function line() is used to draw a line from(x1,y1)to (x2,y2)

Syntax:
line (x1,y1,x2,y2)

initgraph().
This function takes thee arguments and they are
i).the video driver to be used (gd).
ii).the graphics mode (gm).
iii).the path name.

Syntax:
Initgraph(gd,gm,path)

Algorithm:

Step1. Declare the variables xa,ya,xa1,ya1 of array type.
Step2.Declare the variables gd,gm,n,i,op,tx,ty,xf,yf,rx,ry.
Step3. Initialise the graphics function.
Step4. Input the number of points.
=Step5. Input the value of co-ordinate according to number of points.
Step6. Using switch statement selects the option to perform
translation, rotation, scaling, reflection and shearing.

Step7. Translation:
a).input the translation vector
b).add the translation vectors with the coordinates
xa1[i]=xa[i]=tx, ya1[i]=ya[i]=ty,
c).using the function line,display the object before and after translation.

Step8. Rotation:
a). input the rotation angle
b). using formula theta=(theta*3.14)/180
c).input the value of reference point
d). calculate new coordinate point using formula
xa1[i]=xf+(xa[i]-xf)*cos(theta)-(ya[i]-yf)*sin(theta),
ya1[i]=yf+(xa[i]-xf)*sin(theta)-(ya[i]-yf)*cos(theta),
e). using the function line,display the object before and after rotation.

Step9. Scaling:
a).input the scaling factor and reference point
Department of Computer Science and Engineering /Dr.N.G.P IT

b).calculate new coordinate point using formula
xa1[i]=(xa[i]*sx+rx*(1-sx),
ya1 [i] = (ya[i]*sy+ry*(1-sy)
c). using the function line, display the object before and after scaling.

Step10. Shearing:
a).input the shearing value and reference point.
b). input the shear direction x or y
i).if direction x
xa1[i]=xa[i]+shx*(ya[i]-yref)
ii).otherwise
ya1[i]=ya[i]+shy*(xa[i]-xref)

iii). using the function line, display the object before and after shearing.
Step11. Reflection:
a).display the object before reflection using the function line
b). display the object after reflection using the function line
Step12. Stop.

Program:

#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
#include<stdlib.h>
void main()
{
int gd,gm,n,i,xa[10],ya[10],op,tx,ty,xa1[10],ya1[10],theta,xf,yf,rx,ry,sx,sy,shx,shy,xref,yref;
char d;
gd=DETECT;
initgraph(&gd,&gm,"");
cout<<"enter the no of points";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"enter the coordinates"<<i+1;
cin>>xa[i]>>ya[i];
}

do
{
cout<<"menu";
cout<<"\n1.translation\n2.rotation\n3.scaling\n4.shearing\n5.reflection\n6.exit";
cin>>op;
switch(op)
Department of Computer Science and Engineering /Dr.N.G.P IT

{
case 1:
cout<<"enter the translation vector";
cin>>tx>>ty;
for(i=0;i<n;i++)
{
xa1[i]=xa[i]+tx;
ya1[i]=ya[i]+ty;
}
cout<<"before translation";
for(i=0;i<n;i++)
{
line(xa[i],ya[i],xa[(i+1)%n],ya[(i+1)%n]);
}
cout<<"after translation";
for(i=0;i<n;i++)
{
line(xa1[i],ya1[i],xa1[(i+1)%n],ya1[(i+1)%n]);
}
getch();
cleardevice();
break;
case 2:
cout<<"enter the rotation angle";
cin>>theta;
theta=(theta*3.14)/180;
cout<<"enter the reference points";
cin>>xf>>yf;
for(i=0;i<n;i++)
{
xa1[i]=xf+(xa[i]-xf)*cos(theta)-(ya[i]-yf)*sin(theta);
ya1[i]=yf+(xa[i]-xf)*sin(theta)-(ya[i]-yf)*cos(theta);
}
cout<<"before rotation";
for(i=0;i<n;i++)
{
line(xa[i],ya[i],xa[(i+1)%n],ya[(i+1)%n]);
}
cout<<"after rotation";
for(i=0;i<n;i++)
{
line(xa1[i],ya1[i],xa1[(i+1)%n],ya1[(i+1)%n]);
}
getch();
cleardevice();
break;
case 3:
Department of Computer Science and Engineering /Dr.N.G.P IT

cout<<"enter the scaling factor";
cin>>sx>>sy;
cout<<"enter the reference point";
cin>>rx>>ry;
for(i=0;i<n;i++)
{
xa1[i]=xa[i]*sx+rx*(1-sx);
ya1[i]=ya[i]*sy+ry*(1-sy);
}
cout<<"before scaling";
for(i=0;i<n;i++)
{
line(xa[i],ya[i],xa[(i+1)%n],ya[(i+1)%n]);
}
cout<<"after scaling";
for(i=0;i<n;i++)
{
line(xa1[i],ya1[i],xa1[(i+1)%n],ya1[(i+1)%n]);
}
getch();
cleardevice();
break;
case 4:
cout<<"enter the shear value";
cin>>shx>>shy;
cout<<"enter the reference point";
cin>>xref>>yref;
cout<<"enter the shear direction x or y";
cin>>d;
if(d=='x')
{
for(i=0;i<n;i++)
{xa1[i]=xa[i]+shx*(ya[i]-yref);
ya1[i]=ya[i]; }}
cout<<"before shearing";
for(i=0;i<n;i++)
{line(xa[i],ya[i],xa[(i+1)%n],ya[(i+1)%n]);
}cout<<"after shearing";
for(i=0;i<n;i++)
{line(xa1[i],ya1[i],xa1[(i+1)%n],ya1[(i+1)%n]);
}getch();
cleardevice();
break;
case 5:
cout<<"before reflection";
for(i=0;i<n;i++)
{
Department of Computer Science and Engineering /Dr.N.G.P IT

line(xa[i],ya[i],xa[(i+1)%n],ya[(i+1)%n]);
}
cout<<"after reflection";
for(i=0;i<n;i++)
{
line(ya[i],xa[i],ya[(i+1)%n],xa[(i+1)%n]);
}
getch();
cleardevice();
break;
case 6:
exit(0);
break;
}
}while(op!=6);
}

OUTPUT:
1.TRANSLATION 2. SCALING 3. ROTATION 4. EXIT
ENTER YOUR OPTION : 1
ENTER THE TX AND TY VALUE : 150 150

ORIGINAL OBJECT TRANSLATION OBJECT

Result:
Thus the program for 2D transformation was executed successfully
Department of Computer Science and Engineering /Dr.N.G.P IT

COMPOSITE 2D TRANSFORMATIONS

Aim:
To perform the Composite 2D Transformations

Line()
The function line() is used to draw a line from(x1,y1)to (x2,y2)

Syntax:
line (x1,y1,x2,y2)

initgraph().
This function takes thee arguments and they are
i).the video driver to be used (gd).
ii).the graphics mode (gm).
iii).the path name.

Syntax:
Initgraph(gd,gm,path)

Algorithm:

Step1. Declare the variables xa,ya,xa1,ya1 of array type.
Step2.Declare the variables gd,gm,n,i,op,tx,ty,xf,yf,rx,ry.
Step3. Initialise the graphics function.
Step4. Input the number of points.
Step5. Input the value of co-ordinate according to number of points.
Step6. Using switch statement selects the option to perform
translation, rotation, scaling, reflection and shearing.
Step7.
a).input the translation vector
b).add the translation vectors with the coordinates
xa1[i]=xa[i]=tx, ya1[i]=ya[i]=ty,
c).using the function line,display the object before and after translation.
Step8.
a). input the rotation angle
b). using formula theta=(theta*3.14)/180
c).input the value of reference point
d). calculate new coordinate point using formula
xa1[i]=xf+(xa[i]-xf)*cos(theta)-(ya[i]-yf)*sin(theta),
ya1[i]=yf+(xa[i]-xf)*sin(theta)-(ya[i]-yf)*cos(theta),
e). using the function line,display the object before and after rotation.

Step9.
a).input the scaling factor and reference point
b).calculate new coordinate point using formula
xa1[i]=(xa[i]*sx+rx*(1-sx),
Department of Computer Science and Engineering /Dr.N.G.P IT

ya1 [i] = (ya[i]*sy+ry*(1-sy)
c). using the function line, display the object before and after scaling.
Step10.: Composite 2D Transformations
a).input the shearing value and reference point.
b). input the shear direction x or y
i).if direction x
xa1[i]=xa[i]+shx*(ya[i]-yref)
ii).otherwise
ya1[i]=ya[i]+shy*(xa[i]-xref)
iii). using the function line, display the object before and after shearing.
Step11.
a).display the object before reflection using the function line
b). display the object after reflection using the function line
Step12. Stop.

Program:
#include<stdio.h>
#include<process.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void disp(int n,float c[][3])
{
float maxx,maxy;
int i;
maxx=getmaxx();
maxy=getmaxy();
maxx=maxx/2;
maxy=maxy/2;
i=0;
while(i<n-1)
{
line(maxx+c[i][0],maxy-c[i][1],maxx+c[i+1][0],maxy-c[i+1][1]);
i++;
}
i=n-1;
line(maxx+c[i][0],maxy-c[i][1],maxx+c[0][0],maxy-c[0][1]);
setcolor(GREEN);
line(0,maxy,maxx*2,maxy);
line(maxx,0,maxx,maxy*2);
setcolor(WHITE);
}
void mul(int n,float b[][3],float c[][3],float a[][3])
{
int i,j,k;
for(i=0;i<n;i++)
Department of Computer Science and Engineering /Dr.N.G.P IT

for(j=0;j<3;j++)
a[i][j]=0;
for(i=0;i<n;i++)
for(j=0;j<3;j++)
for(k=0;k<3;k++)
{
a[i][j] = a[i][j] + (c[i][k] * b[k][j]);
}
}
void translation(int n,float c[][3],float tx,float ty)
{
int i;
for(i=0;i<n;i++)
{
c[i][0]=c[i][0]+tx;
c[i][1]=c[i][1]+ty;
}
}
}
void scaling(int n,float c[][3],float sx,float sy)
{
float b[10][3],a[10][3];
int i=0,j;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
b[i][j]=0;
b[0][0]=sx;
b[1][1]=sy;
b[2][2]=1;
mul(n,b,c,a);
setcolor(RED);
disp(n,a);
}
void rotation(int n,float c[][3],float ra)
{
int i=0,j;
float b[10][3],xp,yp,a[10][3];
xp=c[0][0];
yp=c[0][1];
for(i=0;i<3;i++)
for(j=0;j<3;j++)
b[i][j]=0;
b[0][0]=b[1][1]=cos(ra*3.14/180);
b[0][1]=sin(ra*3.14/180);
b[1][0]=-sin(ra*3.14/180);
b[2][0]=(-xp*cos(ra*3.14/180))+(yp*sin(ra*3.14/180))+xp;
b[2][1]=(-xp*sin(ra*3.14/180))-(yp*cos(ra*3.14/180))+yp;
Department of Computer Science and Engineering /Dr.N.G.P IT

b[2][2]=1;
mul(n,b,c,a);
setcolor(RED);
disp(n,a);
}
void main()
{
int i,j,k,cho,n,gd=DETECT,gm;
float c[10][3],tx,ty,sx,sy,ra;
initgraph(&gd,&gm,”d:\\turboc~1\\bgi”);
printf(“\nEnter the number of vertices : “);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{printf(“\nEnter the co-ordinates of the %d vertex :”,i+1);
scanf(“%f%f”,&c[i][0],&c[i][1]);
c[i][2]=1;
}do
{clrscr();
cleardevice();
printf(“\n\t\t\t * * * MENU * * *”);
printf(“\n\t 1) TRANSLATION”);
printf(“\n\t 2) SCALING “);
}while(cho!=4);
getch();
closegraph();
}

OUTPUT:
Department of Computer Science and Engineering /Dr.N.G.P IT

Result:
Thus the C program for Composite 2D transformation was executed successfully

COHEN-SUTHERLAND CLIPPING & WINDOWING

AIM:
To implement Cohen-Sutherland clipping& Windowing Algorithm.

FUNCTIONS USED:

Line()
The function line() is used to draw a line from(x1,y1)to (x2,y2)

Syntax:
line (x1,y1,x2,y2)

initgraph().
This function takes thee arguments and they are
i).the video driver to be used (gd).
ii).the graphics mode (gm).
iii).the path name.

Syntax:
Initgraph(gd,gm,path)

Setcolor().
This function changes the drawing colour.

Syntax:
Setcolor(value of the color)

Settextstyle().
The function settextstyle() is used to change the style of the text.

Syntax:
Settextstyle(font,direction,charsize)

Where font is the constant value or the font filename, direction is the number either 0 or 1, which
makes the output to display in horizontal, or in vertical direction, charsize is the character size or
magnification factor and it varies from 1 to 10.

Outtext().
This function display a text message on upper left of the screen
Department of Computer Science and Engineering /Dr.N.G.P IT

Syntax:
Outtext(“message”);

ALGORITHM:

Step 1. Create a class sulc with functions drawwindow, drawline, setcode, visibility and
reset endpoint.
Step 2. Using the function line set the parameters to draw window.
Step 3. Using the function defined in class sulc, setcode is used to save the line inside the
window and to the line outside the window.
Step 4. Using the function visibility
i).check the code to know the points inside or outside the window.
ii).if the code value is zero the point is inside the window.
Step 5. Using the function reset end point
i). if the code value for the line is outside the window.
ii).reset the endpoint to the boundary of the window.
Step 6. Initialize the graphics functions
Step 7. Declare the variables x1, x2, y1, y2 of array type.
Step 8. Get the value of two endpoints x1, y1 and x2, y2 to draw the line.
Step 9. Using the object c, display the window before clipping.
Step 10. Using the function setcode, visibility display the clipped window only with lines
inside the window class was displayed after clipping.

PROGRAM:

#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<stdlib.h>
typedef struct coord
{
int x,y;
char code[4];
}pt;
class sulc
{
public:
void drawwindow();
void drawline(pt p1,pt p2,int c1);
pt setcode(pt p);
int visibility(pt p1,pt p2);
pt resetendpt(pt p1,pt p2);
};
void sulc::drawwindow()
{
setcolor(WHITE);
Department of Computer Science and Engineering /Dr.N.G.P IT

line(150,100,450,100);
line(450,100,450,350);
line(450,350,150,350);
line(150,350,150,100);
}
void sulc::drawline(pt p1,pt p2,int c1)
{
setcolor(c1);
line(p1.x,p1.y,p2.x,p2.y);
}
pt sulc::setcode(pt p)
{
pt ptemp;
if(p.y<100)
ptemp.code[0]='1';
else
ptemp.code[0]='0';
if(p.y>350)
ptemp.code[1]='1';
else
ptemp.code[1]='0';
if(p.y>450)
ptemp.code[2]='1';
else
ptemp.code[2]='0';
if(p.y<150)
ptemp.code[3]='1';
else
ptemp.code[3]='0';
ptemp.x=p.x;
ptemp.y=p.y;
return(ptemp);
}
int sulc::visibility(pt p1,pt p2)
{
int i,flag=0;
for(i=0;i<4;i++)
{
if((p1.code[i]!='0')||(p2.code[i]!='0'))
flag=1;
}
if(flag==0)
return(0);
for(i=0;i<4;i++)
{
if((p1.code[i]==p2.code[i])&&(p1.code[i]=='1'))
flag=0;}
Department of Computer Science and Engineering /Dr.N.G.P IT

if(flag==0)
return(1);
return(2);
}
pt sulc::resetendpt(pt p1,pt p2)
{
pt temp;
int x,y,i;
float m,k;
if(p1.code[3]=='1')
x=150;
if(p1.code[2]=='1')
x=450;
if((p1.code[3]=='1')||(p1.code[2]=='1'))
{
m=(float)(p2.y-p1.y)/(p2.x-p1.x);
k=(p1.y+(m*(x-p1.x)));
temp.y=k;
temp.x=x;
for(i=0;i<4;i++)
temp.code[i]=p1.code[i];
if(temp.y<=350 &&temp.y>=100)
return(temp);
}
if(p1.code[0]=='1')
y=100;
if(p1.code[1]=='1')
y=350;
if((p1.code[0]=='1')||(p1.code[1]=='1'))
{
m=(float)(p2.y-p1.y)/(p2.x-p1.x);
k=(float)p1.x+(float)(y-p1.y)/m;
temp.x=k;
temp.y=y;
for(i=0;i<4;i++)
temp.code[i]=p1.code[i];
if(temp.y<=350 &&temp.y>=100)
return(temp);
}
else
return(p1);
}
void main()
{
int gd=DETECT,gm,v;
sulc c1;
pt p1,p2,ptemp;
Department of Computer Science and Engineering /Dr.N.G.P IT

initgraph(&gd,&gm,"");
int x1[10],y1[10],x2[10],y2[10];
cleardevice();
int i,n;
settextstyle(4,0,4);
outtext("cohen sutherland line clipping");
cout<<"\n\n enter the no.of lines:";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"\n\n enter end-point1(x1,y1):";
cin>>x1[i]>>y1[i];
cout<<"\n\n enter end-point2(x2,y2):";
cin>>x2[i]>>y2[i];
}
cleardevice();
settextstyle(0,0,3);
outtext("before clipping");
c1.drawwindow();
for(i=0;i<n;i++)
{
p1.x=x1[i];
p1.y=y1[i];
p2.x=x2[i];
p2.y=y2[i];
c1.drawline(p1,p2,15);
}
getch();
cleardevice();
settextstyle(0,0,3);
outtext("after clipping");
for(i=0;i<n;i++)
{
p2.x=x2[i];
p2.y=y2[i];
p1=c1.setcode(p1);
p2=c1.setcode(p2);
v=c1.visibility(p1,p2);
switch(v)
{
case 0:
c1.drawwindow();
c1.drawline(p1,p2,15);
break;
case 1:
c1.drawwindow();
break;
Department of Computer Science and Engineering /Dr.N.G.P IT

case 2:
p1=c1.resetendpt(p1,p2);
p2=c1.resetendpt(p2,p1);
c1.drawwindow();
c1.drawline(p1,p2,15);
break;
}
}
getch();
closegraph();
}

OUTPUT:

BEFORE CLIPPING

AFTER CLIPPING
Department of Computer Science and Engineering /Dr.N.G.P IT

RESULT:
Thus the program to implement Cohen-Sutherland clipping& Windowing is executed successfully.

3D- TRANSFORMATION
AIM:
To perform 3D transformations such as translation, rotation and scaling.

FUNCTIONS USED:

Line()
The function line() is used to draw a line from(x1,y1)to (x2,y2)
Syntax:
line (x1,y1,x2,y2)

initgraph().
This function takes thee arguments and they are
i).the video driver to be used (gd).
ii).the graphics mode (gm).
iii).the path name.
Syntax:
Initgraph(gd,gm,path)

ALGORITHM:
Step 1. Create a class cube with function draw cube.
Step 2. Use the function draw cube to draw a cube using eight points by means of
functions line.
Step 3. Declare the variables x1, y1, x2, y2, x3, y3, in array type which of data type int.
Step 4.Declare the variables theta,op,ch,tx,ty,sx,sy,sz,lz+xy,zf,i,x,y,z.
Step 5.Initialise graphics functions.
Step 6.Input the first point in the cube.
Step 7.Input the size of the edge.
Step 8.Create an object to call the function.
Step 9.Using switch operation select the operation to perform translation, rotation,scaling.
Step 10.Translation
a).input the translation vectortx,ty,tz.
b).calculate points using formula
x3[i]=x1[i]+tx.
y3[i]=y1[i]+ty
z3[i]=z1[i]+tz.
x4[i]=x3[i]+z3[i]/2
Department of Computer Science and Engineering /Dr.N.G.P IT

y4[i]=y3[i]+z3[i]/2
c).using the function line, display the object before and after translation.
Step11. Rotation:
a). input the rotation angle
b). using formula theta=(theta*3.14)/180
c).input the direction in x,y,z axis
d). if the direction is along x axis,
x3[i]=x1[i].
y3[i]=y1[i]*cos(theta)-z1[i]*sin(theta),
y3[i]=y1[i]*sin(theta)-z1[i]*cos(theta),
if the direction is along yaxis,
y3[i]=y1[i].
z3[i]=z1[i]*cos(theta)-x1[i]*sin(theta),
x3[i]=z1[i]*sin(theta)-x1[i]*cos(theta),
if the direction is along z axis,
z3[i]=z1[i].
x3[i]=x1[i]*cos(theta)-y1[i]*sin(theta),
y3[i]=x1[i]*sin(theta)-y1[i]*cos(theta),
e).calculate the points using the formula
x4[i]=x3[i]+z3[i]/2
y4[i]=y3[i]+z3[i]/2
f). using the function line,display the object before and after rotation.
Step12. Scaling:
a).input the scaling factor and reference point
b).calculate coordinate point using formula
x3[i]=xf+(x1[i]*sx+xf*(1-sx),
y3 [i] =yf+ (y1[i]*sy+yf*(1-sy)
z3 [i] =zf+ (z1[i]*sz+zf*(1-sz)
c). calculate the points using the formula
x4[i]=x3[i]+z3[i]/2
y4[i]=y3[i]+z3[i]/2
d). using the function line, display the object before and after scaling.
Step13. Stop.

PROGRAM:
#include<iostream.h>
#include<graphics.h>
#include<math.h>
#include<conio.h>
#include<stdlib.h>
class cube
{
public:
void drawcube(int x1[],int y1[])
{
int i;
for(i=0;i<4;i++)
Department of Computer Science and Engineering /Dr.N.G.P IT

{
if(i<3)
line(x1[i],y1[i],x1[i+1],y1[i+1]);
line(x1[0],y1[0],x1[3],y1[3]);
}
for(i=4;i<8;i++)
{
if(i<7)
line(x1[i],y1[i],x1[i+1],y1[i+1]);
line(x1[4],y1[4],x1[7],y1[7]);
}

for(i=0;i<4;i++)
{
line(x1[i],y1[i],x1[i+4],y1[i+4]);
}
}
};
void main()
{
int
i,x1[8],y1[8],x2[8],y2[8],z1[8],x3[8],y3[8],z3[8],x4[8],y4[8],theta,op,ch,tx,ty,tz,sx,sy,sz,xf,yf,zf,x,y,z,size;
int driver=DETECT;
int mode;
initgraph(&driver,&mode,"");
cout<<"enter the points on the cube:";
cin>>x>>y>>z;
cout<<"enter the size of the edge:";
cin>>size;
x1[0]=x1[3]=x;
x1[1]=x1[2]=x+size;
x1[4]=x1[7]=x;
x1[5]=x1[6]=x+size;
y1[0]=y1[1]=y;
y1[2]=y1[3]=y+size;
y1[4]=y1[5]=y;
y1[6]=y1[7]=y+size;
z1[1]=z1[2]=z1[3]=z1[0]=z ;
z1[4]=z1[5]=z1[6]=z1[7]=z-size;
for(i=0;i<8;i++)
{
x2[i]=x1[i]+z1[i]/2;
y2[i]=y1[i]+z1[i]/2;
}
cube c;
getch();
cleardevice();
Department of Computer Science and Engineering /Dr.N.G.P IT

do
{
cout<<"menu"<<endl;
cout<<"\n1.translation\n2.rotation\n3.scaling\n4.exit\n";
cout<<"enter the choice:";
cin>>ch;
switch(ch)
{
case 1:
cout<<"enter the translation vector:";
cin>>tx>>ty>>tz;
for(i=0;i<8;i++)
{
x3[i]=x1[i]+tx;
y3[i]=y1[i]+ty;
z3[i]=z1[i]+tz;
}
for(i=0;i<8;i++)
{
x4[i]=x3[i]+z3[i]/2;
y4[i]=y3[i]+z3[i]/2;
}
cleardevice();
cout<<"before translation";
c.drawcube(x2,y2);
getch();
cleardevice();
cout<<"after translation";
c.drawcube(x4,y4);
getch();
cleardevice();
break;
case 2:
cout<<"enter the rotation angle:";
cin>>theta;
theta=(theta*3.14)/180;
cout<<"enter the direction"<<endl;
cout<<"1.rotation about x axis"<<endl<<"2.rotation about y axis"<<endl<<"3.rotation about z axis";
cin>>op;
if(op==1)
{
for(i=0;i<8;i++)
{
x3[i]=x1[i];
y3[i]=y1[i]*cos(theta)-z1[i]*sin(theta);
z3[i]=y1[i]*sin(theta)+z1[i]*cos(theta);
}
Department of Computer Science and Engineering /Dr.N.G.P IT

}
else
if(op==2)
{
for(i=0;i<8;i++)
{
y3[i]=y1[i];
x3[i]=z1[i]*cos(theta)-x1[i]*sin(theta);
x3[i]=z1[i]*sin(theta)+x1[i]*cos(theta);
}
}
else
if(op==3)
{
for(i=0;i<8;i++)
{
z3[i]=z1[i];
x3[i]=x1[i]*cos(theta)-y1[i]*sin(theta);
y3[i]=x1[i]*sin(theta)+y1[i]*cos(theta);
}
}
else
cout<<"enter correct option";
for(i=0;i<8;i++)
{
x4[i]=x3[i]+z3[i]/2;
y4[i]=y3[i]+z3[i]/2;
}
cleardevice();
cout<<"before rotation";
c.drawcube(x2,y2);
getch();
cleardevice();
cout<<"after rotation";
c.drawcube(x4,y4);
getch();
cleardevice();
break;

case 3:
cout<<"enter the scaling factor:";
cin>>sx>>sy>>sz;
cout<<"enter the reference point:";
cin>>xf>>yf>>zf;
for(i=0;i<8;i++)
{
x3[i]=xf+(x1[i]*sx)+xf*(1-sx);
Department of Computer Science and Engineering /Dr.N.G.P IT

y3[i]=yf+(y1[i]*sy)+yf*(1-sy);
z3[i]=zf+(z1[i]*sz)+zf*(1-sz);
}
for(i=0;i<8;i++)
{
x4[i]=x3[i]+z3[i]/2;
y4[i]=y3[i]+z3[i]/2;
}
cleardevice();
cout<<"before scaling";
c.drawcube(x2,y2);
getch();
cleardevice();
cout<<"after scaling";
c.drawcube(x4,y4);
getch();
cleardevice();
break;
case 4:
exit(0);
break;
}
}
while(op!=4);
getch();}

OUTPUT:
ENTER THE NO OF SIDES & DEPTH OF THE POLYGON : 4 10
ENTER THE CO-ORDINATES :
ENTER THE X1 AND Y1 VALUES : 200 10
ENTER THE X2 AND Y2 VALUES : 250 10
ENTER THE X3 AND Y3 VALUES : 250 60
ENTER THE X4 AND Y4 VALUES : 200 60
Department of Computer Science and Engineering /Dr.N.G.P IT

RESULT:
Thus the program for 3D transformation was executed successfully.

COMPOSITE 3D TRANSFORMATIONS

AIM:
To perform Composite 3D transformations

FUNCTIONS USED:

Line()
The function line() is used to draw a line from(x1,y1)to (x2,y2)
Syntax:
line (x1,y1,x2,y2)

initgraph().
This function takes thee arguments and they are
i).the video driver to be used (gd).
ii).the graphics mode (gm).
iii).the path name.
Syntax:
Initgraph(gd,gm,path)

ALGORITHM:
Step 1. Create a class cube with function draw cube.
Step 2. Use the function draw cube to draw a cube using eight points by means of
functions line.
Step 3. Declare the variables x1, y1, x2, y2, x3, y3, in array type which of data type int.
Step 4.Declare the variables theta,op,ch,tx,ty,sx,sy,sz,lz+xy,zf,i,x,y,z.
Step 5.Initialise graphics functions.
Step 6.Input the first point in the cube.
Step 7.Input the size of the edge.
Step 8.Create an object to call the function.
Step 9.Using switch operation select the operation to perform translation, rotation,scaling.
Step 10.
Department of Computer Science and Engineering /Dr.N.G.P IT

a).input the translation vectortx,ty,tz.
b).calculate points using formula
x3[i]=x1[i]+tx.
y3[i]=y1[i]+ty
z3[i]=z1[i]+tz.
x4[i]=x3[i]+z3[i]/2
y4[i]=y3[i]+z3[i]/2
c).using the function line, display the object before and after translation.
Step11.
a). input the rotation angle
b). using formula theta=(theta*3.14)/180
c).input the direction in x,y,z axis
d). if the direction is along x axis,
x3[i]=x1[i].
y3[i]=y1[i]*cos(theta)-z1[i]*sin(theta),
y3[i]=y1[i]*sin(theta)-z1[i]*cos(theta),
if the direction is along yaxis,
y3[i]=y1[i].
z3[i]=z1[i]*cos(theta)-x1[i]*sin(theta),
x3[i]=z1[i]*sin(theta)-x1[i]*cos(theta),
if the direction is along z axis,
z3[i]=z1[i].
x3[i]=x1[i]*cos(theta)-y1[i]*sin(theta),
y3[i]=x1[i]*sin(theta)-y1[i]*cos(theta),
e).calculate the points using the formula
x4[i]=x3[i]+z3[i]/2
y4[i]=y3[i]+z3[i]/2
f). using the function line,display the object before and after rotation.

Step12.
a).input the scaling factor and reference point
b).calculate coordinate point using formula
x3[i]=xf+(x1[i]*sx+xf*(1-sx),
y3 [i] =yf+ (y1[i]*sy+yf*(1-sy)
z3 [i] =zf+ (z1[i]*sz+zf*(1-sz)
c). calculate the points using the formula
x4[i]=x3[i]+z3[i]/2
y4[i]=y3[i]+z3[i]/2
d). using the function line, display the object before and after scaling.
Step13. Stop.

PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
#include<dos.h>
Department of Computer Science and Engineering /Dr.N.G.P IT

#include<iostream.h>
#include<graphics.h>
int main(void)
{
int gdriver=DETECT,gmode;
int xmax,ymax,tx,ty,tz,i,n,x[20],y[20],z[20],a[20],b[20],choice;
initgraph(&gdriver,&gmode,”h:\cplus”);
xmax=getmaxx();
ymax=getmaxy();
line(xmax/2,0,xmax/2,ymax/2);
line(xmax/2,ymax/2,xmax,ymax/2);
line(xmax/2,ymax/2,0,ymax);
cout<<”\nEnter the no. of sides:”;
cin>>n;
void draw(int x[],int y[],int z[],int n);
cout<<”\n\n\n Enter your choice\n\n 1.Translation \n\n 2.rotation”;
cin>>choice;
switch(choice)
{case 1: cout<<”you have selected translation:\n”;
for(i=0;i<n;i++)
{cin>>x[i]>>y[i]>>z[i];
x[i]+=xmax/2;
y[i]=ymax/2-y[i];
a[i]=x[i]-z[i];
b[i]=y[i]-z[i];
}
for(i=0;i<n;i++)
{line(x[i],y[i],x[(i+1)%n],y[(i+1)%n]);
line(a[i],b[i],x[(i+1)%n]-z[(i+1)%n],y[(i+1)%n]-z[(i+1)%n]);
line(x[i],y[i],a[i],b[i]);}
cout<<”Given the translation vector:”;
cin>>tx>>ty>>tz;
for(i=0;i<n;i++)
{
x[i]=x[i]+tx;
y[i]=y[i]+ty;
z[i]=z[i]+tz;
a[i]=x[i]-z[i];
b[i]=y[i]-z[i];
}
for(i=0;i<n;i++)
{
line(x[i],y[i],x[(i+1)%n],y[(i+1)%n]);
line(a[i],b[i],x[(i+1)%n]-z[(i+1)%n],y[(i+1)%n]-z[(i+1)%n]);
line(x[i],y[i],a[i],b[i]);
}
break;
Department of Computer Science and Engineering /Dr.N.G.P IT

case 2: cout<<”you have chosen rotation:”;
{
int a[10],b[10],th;
cout<<”\n\n Enter the coordinates”;
for(int i=0;i<n;i++)
{
cin>>x[i]>>y[i]>>z[i];
x[i]+=xmax/2;
y[i]=ymax/2-y[i];
a[i]=x[i]-z[i];
b[i]=y[i]-z[i];
}
for(i=0;i<n;i++)
{line(x[i],y[i],x[(i+1)%n],y[(i+1)%n]);
line(x[i]-z[i],y[i]-z[i],x[(i+1)%n]-z[(i+1)%n],y[(i+1)%n]-z[(i+1)%n]);
line(x[i],y[i],x[i]-z[i],y[i]-z[i]);}
cout<<”Enter the angle:”;
cin>>th;
int t=th*3.14/180;
for(int tta=0;tta<t;tta+=3.14)
{
for(i=1;i<n;i++)
{
int xx=x[i];
x[i]=x[0]+(x[i]-x[0])*cos(t)-(y[i]-y[0])*sin(t);
y[i]=y[0]+(xx-x[0])*sin(t)+(y[i]-y[0])*cos(t);
xx=a[i];
a[i]=a[0]+(a[i]-a[0])*cos(t)-(b[i]-b[0])*sin(t);
b[i]=b[0]+(xx-a[0])*sin(t)+(b[i]-b[0])*cos(t);}
for(int i=0;i<n;i++)
{line(x[i],y[i],x[(i+1)%n],y[(i+1)%n]);
line(a[i],b[i],a[(i+1)%n],b[(i+1)%n]);
line(x[i],y[i],a[i],b[i]);}
delay(500);
}break;
}case 3: break;
}
getch();
closegraph();
return 0;
}
OUTPUT:
ENTER THE TRANSLATION FACTORS TX AND TY VALUES : 10 0 0
ENTER THE SCALING FACTORS SX AND SY VALUES : 2 2
Department of Computer Science and Engineering /Dr.N.G.P IT

RESULT:
Thus the program for Composite 3D transformations as executed successfully.

DRAWING THREE DIMENSIONAL OBJECTS AND SCENES

AIM:
To write program to visualize the Drawing three dimensional objects and Scenes

FUNCTIONS USED:

Line()
The function line() is used to draw a line from(x1,y1)to (x2,y2)
Syntax:
line (x1,y1,x2,y2)

initgraph().
This function takes thee arguments and they are
i).the video driver to be used (gd).
ii).the graphics mode (gm).
iii).the path name.
Syntax:
Initgraph(gd,gm,path)

ALGORITHM:
Step 1. Create a class parallel.
Step 2. Create a constructor parallel
i).initialize graphics
Step 3. Create a function initialize
i).draw the x,y,z axis
Step 4. Create a function projection
i).get the reference angle alpha as j.
ii).assign the value of k as 45.
iii).compute the following
Department of Computer Science and Engineering /Dr.N.G.P IT

fi=(3.14/180)*k;
a1=(3.14/180)*j;
z=pz1[1];
i=z/tan(a1);
i1=floor(i*cos(fi));
i2=floor(i*sin(fi));
iv).Calculate
px3[1]=px1[1]+i1;
py3[1]=py1[1]+i2;
px3[2]=px1[2]+i1;
py3[2]=py1[2]+i2;
px3[3]=px1[3]+i1;
py3[3]=py1[3]+i2;
px3[4]=px1[4]+i1;
py3[4]=py1[4]+i2;
v).compute the following
z=pz1[5];
i=z/tan(a1);
i1=floor(i*cos(fi));
i2=floor(i*sin(fi));
vi). calculate
px3[5]=px1[5]+i1;
py3[5]=py1[5]+i2;
px3[6]=px1[6]+i1;
py3[6]=py1[6]+i2;
px3[7]=px1[7]+i1;
py3[7]=py1[7]+i2;
px3[8]=px1[8]+i1;
py3[8]=py1[8]+i2;
vii).compute the values to screen coordinate value.
viii).join the coordinate using line function
ix).display the projected object.

PROGRAM

#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
#include<string.h>
#include<process.h>
class parallel
{
public:
int a,k;
int gd,gm;
int px[8],py[8],pz[8],px1[8],py1[8],pz1[8],px3[8],py3[8];
Department of Computer Science and Engineering /Dr.N.G.P IT

parallel();
void initialize();
void drawobj();
void proj(int);
};
parallel::parallel()
{
gd=DETECT;
initgraph(&gd,&gm,"");
}
void parallel::initialize()
{
px1[1]=100,py1[1]=100,pz1[1]=0;
px1[2]=200,py1[2]=100,pz1[2]=0;
px1[3]=200,py1[3]=200,pz1[3]=0;
px1[4]=100,py1[4]=200,pz1[4]=0;
px1[5]=100,py1[5]=100,pz1[5]=100;
px1[6]=200,py1[6]=100,pz1[6]=100;
px1[7]=200,py1[7]=200,pz1[7]=100;
px1[8]=100,py1[8]=200,pz1[8]=100;
}
void parallel::drawobj()
{
setcolor(WHITE);
line(px1[1],py1[1],px1[2],py1[2]);
line(px1[2],py1[2],px1[3],py1[3]);
line(px1[3],py1[3],px1[4],py1[4]);
line(px1[4],py1[4],px1[8]+50,py1[8]+50);
line(px1[8]+50,py1[8]+50,px1[5]+50,py1[5]+50);
line(px1[5]+50,py1[5]+50,px1[6]+50,py1[6]+50);
line(px1[6]+50,py1[6]+50,px1[7]+50,py1[7]+50);
line(px1[7]+50,py1[7]+50,px1[8]+50,py1[8]+50);
line(px1[1],py1[1],px1[5]+50,py1[5]+50);
line(px1[2],py1[2],px1[6]+50,py1[6]+50);
line(px1[3],py1[3],px1[7]+50,py1[7]+50);
line(px1[4],py1[4],px1[1],py1[1]);
getch();
}
void parallel::proj(int j)
{
cleardevice();
int z,xp,yp;
int dx,dy;
float a1,fi,i,i1,i2;
k=45;
fi=(3.14/180)*k;
a1=(3.14/180)*j;
Department of Computer Science and Engineering /Dr.N.G.P IT

z=pz1[1];
i=z/tan(a1);
i1=floor(i*cos(fi));
i2=floor(i*sin(fi));
px3[1]=px1[1]+i1;
py3[1]=py1[1]+i2;
px3[2]=px1[2]+i1;
py3[2]=py1[2]+i2;
px3[3]=px1[3]+i1;
py3[3]=py1[3]+i2;
px3[4]=px1[4]+i1;
py3[4]=py1[4]+i2;
z=pz1[5];
i=z/tan(a1);
i1=floor(i*cos(fi));
i2=floor(i*sin(fi));
px3[5]=px1[5]+i1;
py3[5]=py1[5]+i2;
px3[6]=px1[6]+i1;
py3[6]=py1[6]+i2;
px3[7]=px1[7]+i1;
py3[7]=py1[7]+i2;
px3[8]=px1[8]+i1;
py3[8]=py1[8]+i2;
cout<<"enter the projected object";
line(px3[1],py3[1],px3[2],py3[2]);
line(px3[2],py3[2],px3[3],py3[3]);
line(px3[3],py3[3],px3[4],py3[4]);
line(px3[4],py3[4],px3[1],py3[1]);
line(px3[5],py3[5],px3[6],py3[6]);
line(px3[6],py3[6],px3[7],py3[7]);
line(px3[7],py3[7],px3[8],py3[8]);
line(px3[8],py3[8],px3[5],py3[5]);
line(px3[1],py3[1],px3[5],py3[5]);
line(px3[2],py3[2],px3[6],py3[6]);
line(px3[3],py3[3],px3[7],py3[7]);
line(px3[4],py3[4],px3[8],py3[8]);
}
void main()
{
parallel p;
int a;
char c='y';
clrscr();
cleardevice();
while(c=='y'||c=='y')
{
Department of Computer Science and Engineering /Dr.N.G.P IT

cleardevice();
p.initialize();
p.drawobj();
cout<<"enter the reference angle\ny";
cin>>a;
p.proj(a);
cout<<"do u want to continue(y/n)";
cin>>c;
}
cleardevice();
}

OUTPUT:
Department of Computer Science and Engineering /Dr.N.G.P IT

RESULT:
Thus the program to visualize the Drawing three dimensional objects and Scenes was executed
successfully.
Department of Computer Science and Engineering /Dr.N.G.P IT

IMAGE EDITING

PHOTOSHOP TOOLS

A sidebar with a variety of tools with multiple image-editing functions appears to the left of the
screen. These tools typically fall under the categories of drawing; painting; measuring and navigation;
selection; typing; and retouching.]Some tools contain a small triangle in the bottom right of the toolbox
icon. These can be expanded to reveal similar tool.While newer versions of Photoshop are updated to
include new tools and features, several recurring tools that exist in most versions are discussed below.

Pen tool

Photoshop includes a few versions of the pen tool. The pen tool creates precise paths that can be
manipulated using anchor points. The free form pen tool allows the user to draw paths freehand, and with
the magnetic pen tool, the drawn path attaches closely to outlines of objects in an image, which is useful for
isolating them from a background.

Shape tools

Photoshop provides an array of shape tools including rectangles, rounded rectangles, ellipses,
polygons and lines. These shapes can be manipulated by the pen tool, direct selection tool etc. to make
vector graphics.

Measuring and navigation

The eyedropper tool selects a color from an area of the image that is clicked, and samples it for
future use. The hand tool navigates an image by moving it in any direction, and the zoom tool enlarges the
part of an image that is clicked on, allowing for a closer view.

Selection tools

Selection tools are used to select all or any part of a picture to perform cut, copy, edit, or retouching
operations.

Cropping

The crop tool can be used to select a particular area of an image and discard the portions outside the
chosen section. This tool assists in creating a focus point on an image and excluding unnecessary or excess
space. Cropping allows enhancement of a photo’s composition while decreasing the file size. The "crop"
tool is in the tools palette, which is located on the right side of the document. By placing the cursor over the
image, the user can drag the cursor to the desired area. Once the Enter key is pressed, the area outside the
rectangle will be cropped. The area outside the rectangle is the discarded data, which allows for the file size
Department of Computer Science and Engineering /Dr.N.G.P IT

to be decreased. The "crop" tool can alternatively be used to extend the canvas size by clicking and
dragging outside the existing image borders.

Slicing

The "slice" and slice select tools, like the crop tool, are used in isolating parts of images. The slice
tool can be used to divide an image into different sections, and these separate parts can be used as pieces of
a web page design once HTML and CSS are applied. The slice select tool allows sliced sections of an
image to be adjusted and shifted. good one

Moving

The move tool can be used to drag the entirety of a single layer or more if they are selected.
Alternatively, once an area of an image is highlighted, the move tool can be used to manually relocate the
selected piece to anywhere on the canvas.

Marquee

The marquee tool can make selections that are single row, single column, rectangular and elliptical.
An area that has been selected can be edited without affecting the rest of the image. This tool can also crop
an image; it allows for better control. In contrast to the crop tool, the "marquee" tool allows for more
adjustments to the selected area before cropping. The only marquee tool that does not allow cropping is the
elliptical. Although the single row and column marquee tools allow for cropping, they are not ideal, because
they only crop a line. The rectangular marquee tool is the preferred option. Once the tool has been selected,
dragging the tool across the desired area will select it. The selected area will be outlined by dotted lines,
referred to as "marching ants". These dotted lines are called "marching ants", because the dashes look like
ants marching around the selected area. To set a specific size or ratio, the tool option bar provides these
settings. Before selecting an area, the desired size or ratio must be set by adjusting the width and height.
Any changes such as color, filters, location, etc. should be made before cropping. To crop the selection, the
user must go to image tab and select crop.

Lasso

The lasso tool is similar to the "marquee" tool, however, the user can make a custom selection by
drawing it freehand. There are three options for the "lasso" tool – regular, polygonal, and magnetic. The
regular "lasso" tool allows the user to have drawing capabilities. Photoshop will complete the selection
once the mouse button is released. The user may also complete the selection by connecting the end point to
the starting point. The "marching ants" will indicate if a selection has been made. The "polygonal lasso"
tool will draw only straight lines, which makes it an ideal choice for images with many straight lines.
Unlike the regular "lasso" tool, the user must continually click around the image to outline the shape. To
complete the selection, the user must connect the end point to the starting point just like the regular lasso
tool. "Magnetic lasso" tool is considered the smart tool. It can do the same as the other two, but it can also
detect the edges of an image once the user selects a starting point. It detects by examining the color pixels
as the cursor move over the desired area. A pixel is the smallest element in an image. Closing the selection
is the same as the other two, which should also should display the "marching ants" once the selection has
been closed.
Department of Computer Science and Engineering /Dr.N.G.P IT

The quick selection tool selects areas based on edges, similarly to the magnetic lasso tool. The
difference between this tool and the lasso tool is that there is no starting and ending point. Since there isn’t
a starting and ending point, the selected area can be added onto as much as possible without starting over.
By dragging the cursor over the desired area, the quick selection tool detects the edges of the image. The
"marching ants" allow the user to know what is currently being selected. Once the user is done, the selected
area can be edited without affecting the rest of the image. One of the features that makes this tool especially
user friendly is that the SHIFT key is not needed to add more to the selection; by default, extra mouse
clicks will be added to the selection rather than creating a new selection.

Magic wand

The magic wand tool selects areas based on pixels of similar values. One click will select all
neighboring pixels of similar value within a tolerance level set by the user. If the eyedropper tool is selected
in the options bar, then the magic wand can determine the value needed to evaluate the pixels; this is based
on the sample size setting in the eyedropper tool. This tool is inferior to the quick selection tool which
works much the same but with much better results and more intuitive controls. The user must decide what
settings to use or if the image is right for this tool.

Eraser

The Eraser tool erases content based on the active layer. If the user is on the text layer, then any text
across which the tool is dragged will be erased. The eraser will convert the pixels to transparent, unless the
background layer is selected. The size and style of the eraser can be selected in the options bar. This tool is
unique in that it can take the form of the paintbrush and pencil tools. In addition to the straight eraser tool,
there are two more available options – background eraser and magic eraser. The background eraser deletes
any part of the image that is on the edge of an object. This tool is often used to extract objects from the
background. The magic eraser tool deletes based on similar colored pixels. It is very similar to the magic
wand tool. This tool is ideal for deleting areas with the same color or tone that contrasts with the rest of the
image.

Video editing

In Adobe CS5 Extended edition, video editing is comprehensive and efficient with a broad
compatibility of video file formats such as MOV, AVI, MPEG-4, and FLV formats and easy workflow.
Using simple combination of keys video layers can easily be modified, with other features such as adding
text and the creation of animations using single images.

3D extrusion

With the Extended version of Photoshop CS5, 2D elements of an artwork can easily become three-
dimensional with the click of a button. Extrusions of texts, an available library of materials for three-
dimensional, and even wrapping two-dimensional images around 3D geometry.
Department of Computer Science and Engineering /Dr.N.G.P IT

Mobile integration

Third-party plugins have also been added to the most recent version of Photoshop where
technologies such as the iPad have integrated the software with different types of applications. Applications
like the Adobe Eazel painting app allows the user to easily create paintings with their fingertips and use an
array of different paint from dry to wet in order to create rich color blending.

Camera Raw

With the Camera Raw plug-in, RAW images can be processed without the use of Adobe Photoshop
Lightroom, along with other image file formats such as JPEGs, TIFFs, or PNGs. The plug-in allows users
to remove noise without the side-effect of over-sharpening, add grain, and even perform post-crop
vignetting.

3D printing tools

Requiring Photoshop version 14.1, users can now create and edit designs for 3D printing. After
downloading 3D photo models from numerous online services, users can add color, adjust the shape or
rotate the angles. Artists can also design 3D models from scratch.

Color replacement tool

The Color Replacement Tool allows you to change the color, while maintaining the highlights and
shadows of the original image, of pieces of the image. By selecting Brushs and right clicking, the Color
Replacement Tool is the third option down. What is important to note with this tool is the foreground color.
The foreground color is what will be applied when painting along the chosen part of the image with the
Color Replacement Tool.

OUTPUT
Department of Computer Science and Engineering /Dr.N.G.P IT
Department of Computer Science and Engineering /Dr.N.G.P IT

RESULT
Thus the image editing has been done with the help of adobe Photoshop software.
Department of Computer Science and Engineering /Dr.N.G.P IT

PARALLEL PROJECTION

AIM:
To write a program for parallel projection and verify it.

ALGORITHM:

Step 1: Start at the pixel for the left-hand endpoint x1
Step 2: Step along the pixels horizontally until we reach the right-hand end of the line, xr
Step 3: For each pixel compute the corresponding y value
Step 4: Parallelly projection is done with the respective coordinates.
Step 5: Terminate the program

PROGRAM:

#include<stdio.h>
#include<graphics.h>
#include<math.h>
void input(int[],int[],int[],int);
void printfFigure(int[],int[],int);
void printfFigure_ortho(int[],int[],int);
void getOblique(int [] , int [],int [] , int , float , int [],int [],int);
int main()
{
int gd=DETECT,gm;
int x[10],y[10],z[10],no,xp[10],yp[10],choice;
float angle1;
clrscr();
printf("Enter Your choice : \n");
printf("1:Point\n");
printf("2 : Line\n");
printf("3 : Triangle\n");
printf("4 : Square\n");
printf("Or Enter no of vertices for any Polygon\n");
scanf("%d",&no);
printf("Enter angle Phi\t");
scanf("%f",&angle1);
input(x,y,z,no);
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("Orthographic Projenction(WHITE)\n");
Department of Computer Science and Engineering /Dr.N.G.P IT

printfFigure_ortho(x,y,no);
printf("Oblique Projenction(YELLOW)\n");
printf("Enter 1. Cavalier Projection\n 2. Cabinet Projection\n");
scanf("%d",&choice);
getOblique(x,y,z,no,angle1,xp,yp,choice);
setcolor(YELLOW);
printfFigure(xp,yp,no);
return 0;
}
void input(int x[],int y[],int z[],int no)
{
int i;
printf("Enter co-ordinate\n");
for (i = 0; i < no; i++)
{
printf("P[%d]\t",i+1);
scanf("%d",&x[i]);
scanf("%d",&y[i]);
scanf("%d",&z[i]);
}
}
void getOblique(int x[],int y[],int z[],int no,float angle1,int xp[],int yp[],int choice)
{
int i;
for (i = 0; i < no; ++i)
{
if(choice==1)
{
xp[i]=x[i]+(z[i]/tan(45))*cos(angle1);
yp[i]=y[i]+(z[i]/tan(45))*sin(angle1);
}
if(choice==2)
{
xp[i]=x[i]+(z[i]/tan(63.43))*cos(angle1);
yp[i]=y[i]+(z[i]/tan(63.43))*sin(angle1);
}
}
}
void printfFigure(int xp[],int yp[],int no)
{
int i;
Department of Computer Science and Engineering /Dr.N.G.P IT

if(no==1)
{
putpixel(xp[0],yp[0],YELLOW);
}
else
{
for (i = 0; i < no-1; ++i)
{
line(xp[i],yp[i],xp[i+1],yp[i+1]);
}
}
if(no>2)
{
line(xp[0],yp[0],xp[no-1],yp[no-1]);
}
}
void printfFigure_ortho(int x[],int y[],int no)
{
int i;
if(no==1)
{
putpixel(x[0],y[0],YELLOW);
}
else
{
for (i = 0; i < no-1; ++i)
{
line(x[i],y[i],x[i+1],y[i+1]);
}
}
if(no>2)
{
line(x[0],y[0],x[no-1],y[no-1]);
}
}
OUTPUT:
Department of Computer Science and Engineering /Dr.N.G.P IT

RESULT:
Thus the program was written and verified successfully.

PERSPECTIVE PROJECTION
AIM:
To write a program for perspective projection and verify it.

ALGORITHM:

Step 1: Start at the pixel for the left-hand endpoint x1
Step 2: Step along the pixels horizontally until we reach the right-hand end of the line, xr
Step 3: For each pixel compute the corresponding y value
Step 4: Perspective projection is done with the respective coordinates.
Step 5: Terminate the program

PROGRAM:

#include<stdio.h>
#include<math.h>
#include<graphics.h>
void main()
{
int x1,y1,x2,y2,gd,gm;
int ymax,a[4][8];
float par[4][4],b[4][8];
int i,j,k,m,n,p;
int xp, yp, zp, x, y, z;
a[0][0] = 100; a[1][0] = 100; a[2][0] = -100;
a[0][1] = 200; a[1][1] = 100; a[2][1] = -100;
a[0][2] = 200; a[1][2] = 200; a[2][2] = -100;
a[0][3] = 100; a[1][3] = 200; a[2][3] = -100;
a[0][4] = 100; a[1][4] = 100; a[2][4] = -200;
a[0][5] = 200; a[1][5] = 100; a[2][5] = -200;
a[0][6] = 200; a[1][6] = 200; a[2][6] = -200;
a[0][7] = 100; a[1][7] = 200; a[2][7] = -200;
detectgraph(&gd,&gm);
initgraph(&gd,&gm, "C:\\TURBOC3\\BGI");
ymax = getmaxy();
xp = 300; yp = 320; zp = 100;
for(j=0; j<8; j++)
Department of Computer Science and Engineering /Dr.N.G.P IT

{
x = a[0][j]; y = a[1][j]; z = a[2][j];
b[0][j] = xp - ( (float)( x - xp )/(z - zp)) * (zp);
b[1][j] = yp - ( (float)( y - yp )/(z - zp)) * (zp);
}
/*- front plane display -*/
for(j=0;j<3;j++)
{
x1=(int) b[0][j]; y1=(int) b[1][j];
x2=(int) b[0][j+1]; y2=(int) b[1][j+1];
line( x1,ymax-y1,x2,ymax-y2);
}
x1=(int) b[0][3]; y1=(int) b[1][3];
x2=(int) b[0][0]; y2=(int) b[1][0];
line( x1, ymax-y1, x2, ymax-y2);
/*- back plane display -*/
setcolor(11);
for(j=4;j<7;j++)
{
x1=(int) b[0][j]; y1=(int) b[1][j];
x2=(int) b[0][j+1]; y2=(int) b[1][j+1];
line( x1, ymax-y1, x2, ymax-y2);
}
x1=(int) b[0][7]; y1=(int) b[1][7];
x2=(int) b[0][4]; y2=(int) b[1][4];
line( x1, ymax-y1, x2, ymax-y2);
setcolor(7);
for(i=0;i<4;i++)
{
x1=(int) b[0][i]; y1=(int) b[1][i];
x2=(int) b[0][4+i]; y2=(int) b[1][4+i];
line( x1, ymax-y1, x2, ymax-y2);
}
getch();
getch();
}
OUTPUT:
Department of Computer Science and Engineering /Dr.N.G.P IT

RESULT:
Thus the program was written and verified successfully.

ANIMATION OF MICKEY MOUSE (content beyond syllabus)
AIM
To implement animation of Mickey Mouse using C programming
ALGORITHM
Step 1: Start the program
Step 2: Initialize the graphics mode and graphic driver
Step 3: Define the functions firstleft() and secondleft()
Step 4: In the main(), define the functions namely arc(), rectangle() and ellipse() along with
the coordinates
Step 5: Define the function setfillstyle() to set the fill pattern and fill color
Step 6: Define the function floodfill() with parameters node, target color and replacement
color to determine the part of the bitmap to fill the color
Step 7: Define the function lineto() with coordinates(x,y) to draw a straight line from
current location(x,y) to the new location
Step 8: Define the function moveto() with coordinates(x,y) to change the current position
Step 9: Display the output
Step 10: Stop the program
PROGRAM
#include<graphics.h>
#include<dos.h>
#include<conio.h>
#include<alloc.h>
void *buf;
void firstleft();
void secondleft();
void main()
{
int gd=DETECT,gm,i=0,x,y,area;
initgraph(&gd,&gm,"tc:\bgi");
rectangle(0,0,getmaxx(),getmaxy());
arc(240,120,40,140,70);
ellipse(165,80,10,280,20,20);
ellipse(315,80,-100,170,20,20);
arc(235,120,163,215,70);
arc(245,120,-35,17,70);
ellipse(193,178,85,280,40,20);
ellipse(283,178,-100,95,40,20);
ellipse(238,199,180,0,39,50);
Department of Computer Science and Engineering /Dr.N.G.P IT

ellipse(213,123,44,240,33,40);
ellipse(262,123,-60,135,33,40);
ellipse(210,123,0,360,13,20);//left eye
ellipse(265,123,0,360,13,20);//right eye
ellipse(210,133,0,360,10,10);//left eye ball
ellipse(265,133,0,360,10,10);//right eye ball
ellipse(210,133,0,360,3,3);//left eye ball
ellipse(265,133,0,360,3,3);//right eye ball
ellipse(238,160,0,360,10,13);//nose
arc(240,125,228,312,68);//mouth
arc(240,120,230,310,72);//mouth
setfillstyle(1,4);
floodfill(238,160,15);//nose
setfillstyle(1,15);
floodfill(210,113,15);
floodfill(265,113,15);
setfillstyle(1,9);
floodfill(210,100,15);
setfillstyle(1,1);
floodfill(315,80,15);
moveto(203,220);
lineto(203,260);
lineto(183,260);
lineto(183,350);
lineto(293,350);
lineto(293,260);
lineto(273,260);
lineto(273,220);
moveto(183,350);
lineto(173,460);
lineto(213,460);
lineto(238,400);
lineto(263,460);
lineto(303,460);
lineto(293,350);
moveto(173,460);
lineto(143,478);
lineto(213,478);
lineto(213,460);
moveto(263,460);
lineto(263,478);
lineto(333,478);
lineto(303,460);
line(238,400,238,350);
//right hand
moveto(183,260);
lineto(113,310);
Department of Computer Science and Engineering /Dr.N.G.P IT

lineto(183,375);
moveto(183,280);
lineto(137,310);
lineto(181,353);
setfillstyle(2,13);
floodfill(190,300,15);
setfillstyle(1,5);
floodfill(223,400,15);
setfillstyle(1,5);
floodfill(253,400,15);
setfillstyle(1,6);
floodfill(173,470,15);
floodfill(303,470,15);
//fingers
secondleft();
ellipse(413.5,228,0,180,3.5,3.5);
line(420,240,433,240);
line(423,247,440,247);
line(413,240,410,228);
line(417,228,420,240);
ellipse(433,243.5,-90,90,3.5,3.5);
line(423,254,440,254);
ellipse(440,250.5,-90,90,3.5,3.5);
ellipse(430,257,-90,90,3,3);
line(413,260,430,260);
area=imagesize(409,224,444,261);
buf=malloc(area);
getimage(409,224,444,261,buf);
while(!kbhit())
{
if(i==0)
{
setfillstyle(1,15);
setcolor(15);
ellipse(210,133,0,360,10,10);//left eye ball
ellipse(265,133,0,360,10,10);//right eye ball
setcolor(0);
ellipse(210,133,0,360,3,3);//left eye ball
ellipse(265,133,0,360,3,3);//right eye ball
floodfill(210,133,15);
floodfill(265,133,15);
setcolor(0);
putimage(391,209,buf,1);
firstleft();
setcolor(15);
secondleft();
putimage(409,224,buf,0);
Department of Computer Science and Engineering /Dr.N.G.P IT

i=1;
}
else
{
setfillstyle(1,0);
setcolor(0);
ellipse(210,133,0,360,10,10);//left eye ball
ellipse(265,133,0,360,10,10);//right eye ball
floodfill(210,133,0);
floodfill(265,133,0);
setcolor(15);
ellipse(210,133,0,360,3,3);//left eye ball
ellipse(265,133,0,360,3,3);//right eye ball
floodfill(210,133,15);
floodfill(265,133,15);
setcolor(0);
putimage(409,224,buf,1);
secondleft();
setcolor(15);
firstleft();
putimage(391,209,buf,0);
i=0;
}
delay(300);
}
getch();
}
void firstleft()
{
moveto(293,260);
lineto(353,276);
lineto(395,223);
moveto(293,280);
lineto(355,296);
lineto(395,245);
}
void secondleft()
{
moveto(293,260);
lineto(363,280);
lineto(413,240);
moveto(293,280);
lineto(363,300);
lineto(413,260);
}
Department of Computer Science and Engineering /Dr.N.G.P IT

OUTPUT

RESULT
Thus C program to implement animated Mickey mouse was written, executed and output is verified
successfully.

TRAFFIC LIGHT ANIMATION

AIM
To implement animation of Traffic light using C programming
ALGORITHM
Step 1: Start the program
Step 2: Initialize the graphics mode and graphic driver.
Step 3: Calculate the value for midx, midy using getmaxx() and getmaxy() functions
respectively.
Step 4: Define the function settextstyle() with three arguments font,direction and size to
Department of Computer Science and Engineering /Dr.N.G.P IT

change the appearance of the text.
Step 5: Define the function settextjustify() to justify the current position either
horizontally or vertically as specified.

Step 6: Define the function outtextxy() to display text or string at a specified point(x,y)
on the screen.
Step 7: The functions rectangle() and circle() are used to draw the traffic light.
Step 8: Define the function setfillstyle() to set the fill pattern and fill color
Step 9: Define the function floodfill() with parameters node, target color and
replacement color to determine the part of the bitmap to fill the color
Step 9: The function setcolor() is to set the color for each circle.
Step 10: Display the output.
Step 11: Stop the program.
PROGRAM

#include<graphics.h>
#include<conio.h>
#include<dos.h>
#include<stdlib.h>
main()
{
int gd = DETECT, gm, midx, midy;
initgraph(&gd, &gm, "C:\\TC\\BGI");
midx = getmaxx()/2;
midy = getmaxy()/2;
setcolor(RED);
settextstyle(SCRIPT_FONT, HORIZ_DIR, 3);
settextjustify(CENTER_TEXT, CENTER_TEXT);
outtextxy(midx, midy-10, "Traffic Light Simulation");
outtextxy(midx, midy+10, "Press any key to start");
getch();
cleardevice();
setcolor(WHITE);
settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);
rectangle(midx-30,midy-80,midx+30,midy+80);
circle(midx, midy-50, 22);
setfillstyle(SOLID_FILL,RED);
floodfill(midx, midy-50,WHITE);
setcolor(BLUE);
outtextxy(midx,midy-50,"STOP");
delay(2000);
graphdefaults();
cleardevice();
setcolor(WHITE);
rectangle(midx-30,midy-80,midx+30,midy+80);
Department of Computer Science and Engineering /Dr.N.G.P IT

circle(midx, midy, 20);
setfillstyle(SOLID_FILL,YELLOW);
floodfill(midx, midy,WHITE);
setcolor(BLUE);
outtextxy(midx-18,midy-3,"READY");
delay(2000);
cleardevice();
setcolor(WHITE);
rectangle(midx-30,midy-80,midx+30,midy+80);
circle(midx, midy+50, 22);
setfillstyle(SOLID_FILL,GREEN);
floodfill(midx, midy+50,WHITE);
setcolor(BLUE);
outtextxy(midx-7,midy+48,"GO");
setcolor(RED);
settextstyle(SCRIPT_FONT, HORIZ_DIR, 4);
outtextxy(midx-150, midy+100, "Press any key to exit...");
getch();
closegraph();
return 0;
}

OUTPUT
Department of Computer Science and Engineering /Dr.N.G.P IT
Department of Computer Science and Engineering /Dr.N.G.P IT

RESULT
Thus C program to implement animation of traffic light was written, executed and output is verified
successfully.

SIMPLE CALCULATOR (MINI PROJECT)
AIM
To design a calculator using c program in 2D view

Program:
#include<stdio.h>

#include<math.h>

#include<unistd.h>

#include <GL/glut.h>

#define CONST_PI 3.141592654
Department of Computer Science and Engineering /Dr.N.G.P IT

#define CONST_E 2.718281828

#define MAXIP 47

void set_properties();

void mouse(int,int,int,int);

void keyboard(unsigned char,int,int);

void displaySolid (void);

void calculator_matrix();

void button_clicked(double,double,double);

int isbutton(int,int);

void which_button(int);

//void points(double,double,double);

//void printxy(double,double,double,double);

void numerical_output(double);

int isPrecedence(int);

double calculate(int *);

double getno(int *);

int fact(int);

void iperr();

int ip=0,key_no=0,error_flag=0,b_count=0,deg_rad=0;

GLfloat xx,yy=1.0;
char input[MAXIP],tmp[20],answer[MAXIP],keypad[41][5]={" (-)"," 1"," 4"," 7","Off"," 0"," 2","
5"," 8"," ("," ."," 3"," 6"," 9"," )"," ="," +"," -"," *"," /","
CE","Sqrt","Root","x^2","x^y","Log","Sin","Cos"," pi","<-","Ln","Sinh","Cosh"," e","Ans"," %","Mod"," |
x|"," !","Deg","Rad"};
inline void unassign_input(){answer[ip]=input[ip]='\0';}
void assign_char(char);
double call_calculate(){int q=0; return calculate(&q);}
Department of Computer Science and Engineering /Dr.N.G.P IT

double global_answer=0;
int i=0,clicked_button=-1;//i is been used to diplay buttons if
//int f=1;//f is a flag at mouse button to print middle points
GLfloat green[]={.0f,.9f,.0f,0.0f},mat_ambient[]={0.9f, 0.0f, 0.0f, 1.0f},black[]={0.0f, 0.0f, 0.0f,
0.6f},white[]={20.0f,20.0f,20.0f,1.0f},blue[]={0.0f,0.0f,0.9f,0.4f},sshine[]={2.0f},transparent[]={1.0f,
1.0f, 1.0f, 0.1f};
typedef int buttons[2][2];

buttons b0={{47,525},{128,479}};
buttons b1={{47,476},{128,430}};
buttons b2={{47,426},{128,380}};
buttons b3={{47,377},{128,331}};
buttons b4={{47,328},{128,281}};
buttons b5={{166,525},{243,479}};

buttons b6={{166,476},{243,430}};

buttons b7={{166,426},{243,380}};

buttons b8={{166,377},{243,331}};

buttons b9={{166,328},{243,281}};

buttons b10={{282,525},{358,479}};

buttons b11={{282,476},{358,430}};

buttons b12={{282,426},{358,380}};

buttons b13={{282,377},{358,331}};
buttons b14={{282,328},{358,281}};

buttons b15={{397,525},{474,479}};

buttons b16={{397,476},{474,430}};

buttons b17={{397,426},{474,380}};

buttons b18={{397,377},{474,331}};

buttons b19={{397,328},{474,281}};

buttons b20={{512,525},{589,479}};

buttons b21={{512,476},{589,430}};

buttons b22={{512,426},{589,380}};
Department of Computer Science and Engineering /Dr.N.G.P IT

buttons b23={{512,377},{589,331}};

buttons b24={{512,328},{589,281}};

buttons b25={{627,525},{704,479}};

buttons b26={{627,476},{704,430}};

buttons b27={{627,426},{704,380}};

buttons b28={{627,377},{704,331}};

buttons b29={{627,328},{704,281}};

buttons b30={{742,525},{819,479}};

buttons b31={{742,476},{819,430}};

buttons b32={{742,426},{819,380}};

buttons b33={{742,377},{819,331}};

buttons b34={{742,328},{819,281}};

buttons b35={{858,525},{934,479}};

buttons b36={{858,476},{934,430}};

buttons b37={{858,426},{934,380}};

buttons b38={{858,377},{934,331}};

buttons b39={{858,328},{934,281}};

/*void printxy(double xmin,double ymin,double xmax,double ymax)
{

double winX,winY,winZ,zero=0.0;

GLdouble model[16];

GLdouble proj[16];

GLint view[4];
Department of Computer Science and Engineering /Dr.N.G.P IT

glGetDoublev(GL_MODELVIEW_MATRIX,model);

glGetDoublev(GL_PROJECTION_MATRIX,proj);

glGetIntegerv(GL_VIEWPORT,view);

gluProject(zero,xmin,ymin,model,proj,view,&winX,&winY,&winZ);

printf("buttons b%d={{%.0f,%.0f},",i++,winX,576-winY);

gluProject(zero,xmax,ymax,model,proj,view,&winX,&winY,&winZ);

printf("{%.0f,%.0f}};\n",winX,576-winY);

glPushMatrix();
glRasterPos3f(0.0,(xmin+xmax)/2-0.01,(ymin+ymax)/2);

glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,'1');

glPopMatrix();
}
void points(double x,double y,double z)
{
double t_b,l_r;
glPushMatrix();
glPointSize(3.0);

glBegin(GL_POINTS);

glVertex3d(0,y,z);

glEnd();

glPointSize(2.0);

glBegin(GL_POINTS);

glVertex3d(0,y+0.14,z-0.1);

for(t_b=y+0.14;t_b>=y-0.135;t_b-=0.02);//ymin -> ymax (top-bottom)

for(l_r=z-0.1;l_r<=z+0.1;l_r+=0.01);//xmin -> xmax (left-right)

glVertex3d(0,t_b,l_r);
glEnd();

glPopMatrix();
Department of Computer Science and Engineering /Dr.N.G.P IT

//printxy(t_b,l_r,y+0.14,z-0.1);

//printf("\t\tcase %d:\tclicked_button=%d;\n\t\t\tbreak;\n",i,i);

//printf("\tif(x>=b%d[0][0]&&x<=b%d[1][0])\n\tif(y<=b%d[0][1]&&y>=b%d[1][1])\n\t\treturn
%d;\n",i,i,i,i,i);

//i++;
}*/
void calculator_matrix(double x,double y,double z)

{

for(z=1.1;z>=-1.2;z-=0.3)

for(y=-1.3;y<=0.0;y+=0.3){

//if(f){

glPushMatrix();

if((z<=0.3&&z>-0.2)||(y>=-0.3&&z>0.2))

glMaterialfv (GL_FRONT, GL_AMBIENT, green);

else if(z>0.2&&y<=-0.3)

glMaterialfv (GL_FRONT, GL_AMBIENT, mat_ambient);

else{

glMaterialfv (GL_FRONT, GL_AMBIENT, blue);

}

if(i!=clicked_button)

glTranslated (0.0,y,z);

else

glTranslated (x*1.5,y-x/2,z+x-x*0.5);

glRotated(20,10,0,0);//(angle,x,y,z);

glScaled (0.15, 0.03, 0.22);//(x scaling,y scaling,z scaling);
Department of Computer Science and Engineering /Dr.N.G.P IT

glutSolidCube (1.0);

glPopMatrix();

glMaterialfv(GL_FRONT, GL_AMBIENT,black);

if(i++!=clicked_button)

glRasterPos3d(0.0,y-0.057,z+0.042);

else

glRasterPos3d(x*1.5,y-0.057-x/2,z+0.042+x-x*0.5);

glutBitmapString(GLUT_BITMAP_HELVETICA_18 ,keypad[key_no++]);

if(key_no==39&&deg_rad) key_no++;
//}
//else
//points(0,y,z);
}

//numerical screen
glPushMatrix();
glMaterialfv (GL_FRONT, GL_AMBIENT, white);

glMaterialfv (GL_FRONT, GL_SHININESS, sshine);
glMaterialfv (GL_FRONT, GL_DIFFUSE, blue);

glTranslated (0.0,1.15,0.0); //pos at (0.0,y,x);

glRotated(7,10,10,10000);//(degree,x-percentage,y-percentage,z-percentage);

glScaled (0.30, 0.07, 1.6); //(x size-percentage,y size-percentage,z size-percentage);

glutSolidCube (1.0);
glPopMatrix();
//Numbers on numerical screen: input & answer
glMaterialfv(GL_FRONT, GL_AMBIENT,black);
glMaterialfv (GL_FRONT, GL_DIFFUSE, black);
glRasterPos3f(0.0,yy,xx);
glutBitmapString(GLUT_BITMAP_TIMES_ROMAN_24,input);
glRasterPos3f(0.0,yy-0.25,1);
glutBitmapString(GLUT_BITMAP_TIMES_ROMAN_24,answer);
glRasterPos3f(0.1,yy+0.10,0.75);
if(deg_rad)
glutBitmapString(GLUT_BITMAP_8_BY_13,"Deg");
Department of Computer Science and Engineering /Dr.N.G.P IT

else
glutBitmapString(GLUT_BITMAP_8_BY_13,"Rad");
//credits
glMaterialfv(GL_FRONT, GL_AMBIENT,black);
glMaterialfv (GL_FRONT, GL_DIFFUSE, white);
glRasterPos3d(0.0,-1.7,-0.05);
glutBitmapString(GLUT_BITMAP_8_BY_13,"Developed for CGV mini-Project by Shinu
M. and Alexandre R. L.");
key_no=i=0;
}
void assign_char(char ch)
{
if(!ip){
xx=-0.75;
answer[ip]='\0';
}
if(ip<MAXIP)
{
input[ip++]=ch;
unassign_input();
xx+=0.03126;
}
else
snprintf(answer,20,"Too long input...");
}
void put_answer()
{
int int_tmp=0;
snprintf(tmp,20,"%f",global_answer);
while(tmp[int_tmp]!='\0'){
assign_char(tmp[int_tmp++]);

}
}
int isbutton(int x,int y)

{
if(x>=b0[0][0]&&x<=b0[1][0])

if(y<=b0[0][1]&&y>=b0[1][1])

return 0;

if(x>=b1[0][0]&&x<=b1[1][0])

if(y<=b1[0][1]&&y>=b1[1][1])
Department of Computer Science and Engineering /Dr.N.G.P IT

return 1;

if(x>=b2[0][0]&&x<=b2[1][0])

if(y<=b2[0][1]&&y>=b2[1][1])

return 2;

if(x>=b3[0][0]&&x<=b3[1][0])

if(y<=b3[0][1]&&y>=b3[1][1])

return 3;

if(x>=b4[0][0]&&x<=b4[1][0])

if(y<=b4[0][1]&&y>=b4[1][1])

return 4;

if(x>=b5[0][0]&&x<=b5[1][0])

if(y<=b5[0][1]&&y>=b5[1][1])

return 5;

if(x>=b6[0][0]&&x<=b6[1][0])

if(y<=b6[0][1]&&y>=b6[1][1])

return 6;

if(x>=b7[0][0]&&x<=b7[1][0])

if(y<=b7[0][1]&&y>=b7[1][1])

return 7;

if(x>=b8[0][0]&&x<=b8[1][0])

if(y<=b8[0][1]&&y>=b8[1][1])

return 8;

if(x>=b9[0][0]&&x<=b9[1][0])

if(y<=b9[0][1]&&y>=b9[1][1])
Department of Computer Science and Engineering /Dr.N.G.P IT

return 9;

if(x>=b10[0][0]&&x<=b10[1][0])

if(y<=b10[0][1]&&y>=b10[1][1])

return 10;

if(x>=b11[0][0]&&x<=b11[1][0])

if(y<=b11[0][1]&&y>=b11[1][1])

return 11;

if(x>=b12[0][0]&&x<=b12[1][0])

if(y<=b12[0][1]&&y>=b12[1][1])

return 12;

if(x>=b13[0][0]&&x<=b13[1][0])

if(y<=b13[0][1]&&y>=b13[1][1])

return 13;

if(x>=b14[0][0]&&x<=b14[1][0])

if(y<=b14[0][1]&&y>=b14[1][1])

return 14;

if(x>=b15[0][0]&&x<=b15[1][0])

if(y<=b15[0][1]&&y>=b15[1][1])

return 15;

if(x>=b16[0][0]&&x<=b16[1][0])

if(y<=b16[0][1]&&y>=b16[1][1])

return 16;

if(x>=b17[0][0]&&x<=b17[1][0])
Department of Computer Science and Engineering /Dr.N.G.P IT

if(y<=b17[0][1]&&y>=b17[1][1])

return 17;

if(x>=b18[0][0]&&x<=b18[1][0])

if(y<=b18[0][1]&&y>=b18[1][1])

return 18;

if(x>=b19[0][0]&&x<=b19[1][0])

if(y<=b19[0][1]&&y>=b19[1][1])

return 19;

if(x>=b20[0][0]&&x<=b20[1][0])

if(y<=b20[0][1]&&y>=b20[1][1])

return 20;

if(x>=b21[0][0]&&x<=b21[1][0])

if(y<=b21[0][1]&&y>=b21[1][1])

return 21;

if(x>=b22[0][0]&&x<=b22[1][0])

if(y<=b22[0][1]&&y>=b22[1][1])

return 22;

if(x>=b23[0][0]&&x<=b23[1][0])

if(y<=b23[0][1]&&y>=b23[1][1])

return 23;

if(x>=b24[0][0]&&x<=b24[1][0])

if(y<=b24[0][1]&&y>=b24[1][1])

return 24;
if(x>=b25[0][0]&&x<=b25[1][0])
Department of Computer Science and Engineering /Dr.N.G.P IT

if(y<=b25[0][1]&&y>=b25[1][1])

return 25;
if(x>=b26[0][0]&&x<=b26[1][0])

if(y<=b26[0][1]&&y>=b26[1][1])
return 26;

if(x>=b27[0][0]&&x<=b27[1][0])
if(y<=b27[0][1]&&y>=b27[1][1])

return 27;
if(x>=b28[0][0]&&x<=b28[1][0])
if(y<=b28[0][1]&&y>=b28[1][1])

return 28;
if(x>=b29[0][0]&&x<=b29[1][0])
if(y<=b29[0][1]&&y>=b29[1][1])

return 29;
if(x>=b30[0][0]&&x<=b30[1][0])
if(y<=b30[0][1]&&y>=b30[1][1])

return 30;
if(x>=b31[0][0]&&x<=b31[1][0])
if(y<=b31[0][1]&&y>=b31[1][1])

return 31;
if(x>=b32[0][0]&&x<=b32[1][0])
if(y<=b32[0][1]&&y>=b32[1][1])

return 32;
if(x>=b33[0][0]&&x<=b33[1][0])
if(y<=b33[0][1]&&y>=b33[1][1])

return 33;
if(x>=b34[0][0]&&x<=b34[1][0])
if(y<=b34[0][1]&&y>=b34[1][1])

return 34;
if(x>=b35[0][0]&&x<=b35[1][0])
if(y<=b35[0][1]&&y>=b35[1][1])

return 35;
if(x>=b36[0][0]&&x<=b36[1][0])
if(y<=b36[0][1]&&y>=b36[1][1])
Department of Computer Science and Engineering /Dr.N.G.P IT

return 36;
if(x>=b37[0][0]&&x<=b37[1][0])
if(y<=b37[0][1]&&y>=b37[1][1])

return 37;
if(x>=b38[0][0]&&x<=b38[1][0])
if(y<=b38[0][1]&&y>=b38[1][1])

return 38;
if(x>=b39[0][0]&&x<=b39[1][0])
if(y<=b39[0][1]&&y>=b39[1][1])

return 39;

return -1;
}
void button_clicked(double x,double y,double z)

{
if(x>=0.0&&x<=0.03)

{
key_no=i=0;

set_properties();
//start drawing

calculator_matrix(x,y,z);

usleep(200);

glFlush();

glutSwapBuffers();

//Recursion
button_clicked(x+0.006,0.0,0.0);

key_no=i=0;
set_properties();

calculator_matrix(x,y,z);

usleep(200);
Department of Computer Science and Engineering /Dr.N.G.P IT

glFlush();

glutSwapBuffers();
}

return;
}
void which_button(int select)
{
switch(select)
{

case 0: clicked_button=0;

assign_char('(');

assign_char('-');

assign_char('1');

assign_char(')');

assign_char('*');

break;

case 1: clicked_button=1;

assign_char('1');

break;
case 2: clicked_button=2;

assign_char('4');

break;
case 3: clicked_button=3;

assign_char('7');

break;
case 4: clicked_button=4; //Off=exit(0);
Department of Computer Science and Engineering /Dr.N.G.P IT

break;
case 5: clicked_button=5;
assign_char('0');

break;
case 6: clicked_button=6;
assign_char('2');

break;
case 7: clicked_button=7;
assign_char('5');

break;
case 8: clicked_button=8;
assign_char('8');

break;
case 9: clicked_button=9;
assign_char('(');

break;
case 10: clicked_button=10;
assign_char('.');

break;
case 11: clicked_button=11;
assign_char('3');

break;
case 12: clicked_button=12;
assign_char('6');

break;
case 13: clicked_button=13;
assign_char('9');

break;
case 14: clicked_button=14;
assign_char(')');

break;
case 15: clicked_button=15;//= ->Equal
numerical_output(call_calculate());
break;
case 16: clicked_button=16;
if(!ip&&global_answer)
Department of Computer Science and Engineering /Dr.N.G.P IT

put_answer();
assign_char('+');

break;
case 17: clicked_button=17;
if(!ip&&global_answer)
put_answer();
assign_char('-');

break;
case 18: clicked_button=18;
if(!ip&&global_answer)
put_answer();
assign_char('*');
break;
case 19: clicked_button=19;
if(!ip&&global_answer)
put_answer();
assign_char('/');
break;
case 20: clicked_button=20; // CE
do{
unassign_input();
}while(ip--);
ip++;
xx=-0.75;
break;
case 21: clicked_button=21; //sqrt
assign_char('2');
assign_char('r');
assign_char('o');
assign_char('o');
assign_char('t');
assign_char('(');
break;
case 22: clicked_button=22; //root
if(!ip&&global_answer)
put_answer();
assign_char('r');
assign_char('o');
assign_char('o');
assign_char('t');
assign_char('(');
break;
case 23: clicked_button=23;
if(!ip&&global_answer)
put_answer();
Department of Computer Science and Engineering /Dr.N.G.P IT

assign_char('^');
assign_char('2');
break;
case 24: clicked_button=24;

if(!ip&&global_answer)

put_answer();

assign_char('^');

break;

case 25:
clicked_button=25;

assign_char('l');

assign_char('o');

assign_char('g');

assign_char('(');

break;

case 26:
clicked_button=26;

assign_char('s');

assign_char('i');

assign_char('n');

assign_char('(');

break;

case 27:
clicked_button=27;

assign_char('c');

assign_char('o');

assign_char('s');
Department of Computer Science and Engineering /Dr.N.G.P IT

assign_char('(');

break;

case 28:
clicked_button=28;//CONST_PI

select=0;

snprintf(tmp,20,"%f",CONST_PI);

while(tmp[select]!='\0'){

assign_char(tmp[select++]);

}

break;

case 29:
clicked_button=29;// <-

if(ip>0){

if(!isdigit(input[ip-1]))

do{input[--ip]='\0';xx-=0.03126;}
while(!isdigit(input[ip-1])&&input[ip-1]!='+'&&input[ip-1]!='*'&&input[ip-1]!='/'&&input[ip-1]!
='-'&&input[ip-1]!='('&&input[ip-1]!=')'&&input[ip-1]!='|'&&ip>0);
else
input[--ip]='\0';
xx-=0.03126;
}
break;
case 30: clicked_button=30;//ln
assign_char('l');
assign_char('n');
assign_char('(');

break;

case 31:
clicked_button=31;

assign_char('s');
Department of Computer Science and Engineering /Dr.N.G.P IT

assign_char('i');

assign_char('n');

assign_char('h');

assign_char('(');

break;

case 32:
clicked_button=32;

assign_char('c');

assign_char('o');

assign_char('s');

assign_char('h');

assign_char('(');

break;

case 33:
clicked_button=33;//CONST_E

select=0;

snprintf(tmp,20,"%f",CONST_E);

while(tmp[select]!='\0')
{

assign_char(tmp[select++]);

}

break;

case 34:
clicked_button=34;//Answer

put_answer
break;
Department of Computer Science and Engineering /Dr.N.G.P IT

case 35:
clicked_button=35;//percentage

if(!ip&&global_answer)

put_answer();

assign_char('%');

break;

case 36:

clicked_button=36;//mod

if(!ip&&global_answer)

put_answer();

assign_char('m');

assign_char('o');

assign_char('d');

assign_char('(');

break;

case 37:
clicked_button=37;//|x|

assign_char('|');//??

//??assign_char('|');

break;

case 38:
clicked_button=38;

if(!ip&&global_answer)

put_answer();

assign_char('!');
Department of Computer Science and Engineering /Dr.N.G.P IT

break;

case 39:
clicked_button=39;//degree || radians

if(deg_rad)

numerical_output(call_calculate()*(CONST_PI/180));

else

numerical_output(call_calculate()*(180/CONST_PI));

deg_rad=deg_rad?0:1;

break;

case 40:
clicked_button=40;

break;
}
}
void set_properties()

{

//set properties of the surface material

//GLfloat mat_ambient[] = {0.9f, 0.2f, 0.2f, 1.0f};

// gray
GLfloat mat_diffuse[] = {.5f, .5f, .5f, 0.5f};

GLfloat mat_specular[] = {1.0f, 1.0f, 1.0f, 1.0f};

GLfloat mat_shininess[] = {1.0f};

glMaterialfv (GL_FRONT, GL_AMBIENT, mat_ambient);

glMaterialfv (GL_FRONT, GL_DIFFUSE, mat_diffuse);

glMaterialfv (GL_FRONT, GL_SPECULAR, mat_specular);

glMaterialfv (GL_FRONT, GL_SHININESS, mat_shininess);
Department of Computer Science and Engineering /Dr.N.G.P IT

//set the light source properties

GLfloat lightIntensity[] = {0.5f, 0.5f, 0.5f, 1.0f};

GLfloat light_position[] = {2.3f, -1.7f, 0.0f, 0.0f};//{2.0f, 6.0f, 3.0f, 0.0f};

glLightfv (GL_LIGHT0, GL_POSITION, light_position);

glLightfv (GL_LIGHT0, GL_DIFFUSE, lightIntensity);

//set the camera

glMatrixMode (GL_PROJECTION);
glLoadIdentity();

double winHt = 1.0; //half-height of window
glOrtho (-winHt * 64/48.0, winHt*64/48.0, -winHt, winHt, 0.1, 100.0);

glMatrixMode (GL_MODELVIEW);

glLoadIdentity();

gluLookAt (2.3,-3.3,0 ,0.0,0.00,0.0,0.9,-0.09,0.0);//(2.3,-3.3,0 ,0.0,0.00,0.0,0.9,-0.09,0.0);
//
-X---Y---Z-|-X---Y----Z-|-X---Y---Z-|
// --Viewer---|---Object---|Inclination|

glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}

void display(void)
{
if(clicked_button!=-1)
{
button_clicked(0.0,0.0,0.0);

if(clicked_button==4)

exit(0);

clicked_button=-1;
//key_no=0;
}
Department of Computer Science and Engineering /Dr.N.G.P IT

set_properties();
//start drawing
calculator_matrix(0.0,0.0,0.0);

glFlush();

glutSwapBuffers();
}

void mouse(int button, int state,int x, int y)

{
if(button==GLUT_LEFT_BUTTON&&state==GLUT_DOWN)
{
which_button(isbutton(x,y));
glutPostRedisplay();
}

/*if(button==GLUT_RIGHT_BUTTON){
f=f?0:1;
glutPostRedisplay();

}*/
}
void keyboard(unsigned char ch, int x, int y)

{
if(ch=='0')

which_button(5);

if(ch>='1'&&ch<='3')

which_button((int)((ch-'0'-1)%3*5+1));
if(ch>='4'&&ch<='6')

which_button((int)((ch-'0'-1)%3*5+2));
if(ch>='7'&&ch<='9')

which_button((int)((ch-'0'-1)%3*5+3));
if(ch=='+')

which_button(16);
if(ch=='-')

which_button(17);
if(ch=='*')
Department of Computer Science and Engineering /Dr.N.G.P IT

which_button(18);
if(ch=='/')

which_button(19);
if(ch=='.')

which_button(10);
if(ch=='(')

which_button(9);
if(ch==')')

which_button(14);
if(ch=='\b')

which_button(29);
if(ch=='^')

which_button(24);
if(ch=='c')

which_button(20);
if(ch==13||ch=='=')
which_button(15);

if(ch==27||ch=='q')
which_button(4); /* escape */
/*if(ch=='a')
f=f?0:1;*/

glutPostRedisplay();
}
void main (int argc, char ** argv)

{
glutInit (&argc, argv);

glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA|GLUT_DEPTH|GLUT_ALPHA);

glutInitWindowSize (1024,576);

glutInitWindowPosition (0,0);

glutCreateWindow ("Scientific Calculator Version 1.0.0");

glutDisplayFunc (display);
Department of Computer Science and Engineering /Dr.N.G.P IT

glutMouseFunc (mouse);

glutKeyboardFunc (keyboard);

glBlendFunc (GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);

glEnable (GL_BLEND);
glEnable (GL_LIGHTING);
glEnable (GL_LIGHT0);

glShadeModel (GL_SMOOTH);
glEnable (GL_DEPTH_TEST);

glEnable (GL_NORMALIZE);
glClearColor (0.85, 0.85, 0.85, 0.0);

glViewport (0, 0, 640, 480);
glutMainLoop();
}
void numerical_output(double ans)

{
if(!error_flag&&!b_count){
snprintf(answer,20,"Ans= %f",ans);

global_answer=ans;//to reuse output in calculation
}
else

snprintf(answer,20,"Invalid input");

ip=error_flag=0;

return;
}
int isNextPreceding(int p)

{
if(input[p]=='c'||input[p]=='s'||input[p]=='l'||input[p]=='m')

return 1;

if(input[p]=='(')

return 3;
Department of Computer Science and Engineering /Dr.N.G.P IT

if(getno(&p))

if(input[p]=='^'||input[p]=='r')

return 2;

else

return 0;

iperr();
}
double powFunc(int *p)
{

double res=getno(p);

switch(input[(*p)++])

{
case '^': if(isdigit(input[*p]))

res=pow(res,getno(p));

else

res=pow(res,calculate(p));

break;

case 'r': *p+=4;

if(isdigit(input[*p])){

res=pow(getno(p),1/res);

if(input[*p]!=')')

iperr();

}
else

res=pow(calculate(p),1/res);
Department of Computer Science and Engineering /Dr.N.G.P IT

break;

}

return res;
}

double calculate(int *q)
{

double ans;

printf("Entered calculate at the string pos %d\n",*q);
if(*q<=ip)

{if(input[*q]=='('||input[*q]=='c'||input[*q]=='s'||input[*q]=='l'||input[*q]=='|');

else if(isdigit(input[*q]))

ans=getno(q);

else if(input[*q]=='-'&&isdigit(input[++(*q)]))

ans=getno(q)*(-1);
else

iperr();
sw: switch(input[(*q)++])

{
case '+':
printf("Gone to plus!\n");

ans=ans+calculate(q);//given precedence

break;

case '-': printf("Gone to minus!\n");

ans=ans-calculate(q);//given precedence

break;

/*Middle Priority*/
Department of Computer Science and Engineering /Dr.N.G.P IT

case '/': switch(isNextPreceding(*q))
{
case 0:
ans=ans/getno(q);//calculate here

break;
case 1:
ans=ans/calculate(q);//given precedence

break;
case 2:
ans=ans/powFunc(q);//calculate power and calculate

break;
case 3: (*q)++;//'('

b_count++;

ans=ans/calculate(q);
}

goto sw;//continue reading input

case '*': switch(isNextPreceding(*q))
{
case 0:
ans=ans*getno(q);//calculate here

break;
case 1:
ans=ans*calculate(q);//given precedence

//printf("Gone right switch, ans is=%f\n",ans);

break;
case 2:
ans=ans*powFunc(q);//calculate power

break;
case 3: (*q)++;//'('

b_count++;

ans=ans*calculate(q);
}

goto sw;//continue reading input
Department of Computer Science and Engineering /Dr.N.G.P IT

case '(': b_count++;

ans=calculate(q);//given precedence

goto sw;//continue reading input

case ')': b_count--;
break;

case 'c': (*q)+=3;//cos(
b_count++;
if(input[*q]=='('){
(*q)++;
ans=coshf(getno(q));
}
else
ans=cos(getno(q));
goto sw;//continue reading input

case 's': (*q)+=3;//sin()
b_count++;
if(input[*q]=='('){
(*q)++;
ans=sinhf(getno(q));
}
else
ans=sin(getno(q));
goto sw;//continue reading input

case 'l': b_count++;
if(input[*q]=='n'){
*q+=2;//ln()

ans=log10(getno(q))/log10(CONST_E);
}
else{
*q+=3;//log()
ans=log10(getno(q));
}
goto sw;//continue reading input

case 'm': *q+=3;//mod()
b_count++;
ans=((int)ans)%((int)getno(q));
goto sw;//continue reading input
Department of Computer Science and Engineering /Dr.N.G.P IT

case '%': ans=ans/100;

goto sw;//continue reading input

case '!': ans=fact(ans);
goto sw;//continue reading input

case '|': if(isdigit(input[(*q)]))//Working for single digit
ans=getno(q);
else
if(input[*q]=='-'&&isdigit(input[(*q)+1])){
(*q)++;
ans=getno(q);
}
else{
iperr();
break;
}
if(input[(*q)++]!='|')
iperr();
goto sw;

/*Higher priority*/

case '^': if(isdigit(input[*q]))
ans=pow(ans,getno(q));
else

ans=pow(ans,calculate(q));
goto sw;

case 'r': *q+=4;//root(
b_count++;
ans=pow(calculate(q),1/ans);
goto sw;
/*default statements*/
case '\0': break;
default : iperr();
}
printf("Returned value=%f at i=%d\n",ans,*q);
}
return ans;
}
double getno(int *q)
{
int count=0,flag=0;
double no=0.0;
Department of Computer Science and Engineering /Dr.N.G.P IT

while(isdigit(input[*q]))
{
printf("reading no. at i=%d\n",*q);
if(!flag)
flag=no=(double)(input[(*q)++]-'0');

else

no=no*10+(double)(input[(*q)++]-'0');

if(input[*q]=='.')

{

(*q)++;

if(!isdigit(input[*q]))

iperr();

else

while(isdigit(input[*q]))

no+=((double)(input[(*q)++]-'0'))/pow(10,++count);

}
}
printf("number %f was read.\n",no);

return no;
}
int fact(int int_fact)
{
if(int_fact)

return int_fact*fact(int_fact-1);
return 1;
}

void iperr()
{
printf("Wrong Input.\n");
do{

unassign_input();
}
Department of Computer Science and Engineering /Dr.N.G.P IT

while(ip--);

xx=-0.75;

error_flag=1;
}

OUTPUT:
Department of Computer Science and Engineering /Dr.N.G.P IT

RESULT:
Thus the simple calculator using c program was executed successfully.