Documente Academic
Documente Profesional
Documente Cultură
REGULATION – 2013
LABORATORY MANUAL
YEAR/SEM: III/V
1. Implementation of Algorithms for drawing 2D Primitives – Line (DDA, Bresenham) – all slopes Circle
(Midpoint)
3. Composite 2D Transformations
4. Line Clipping
7. Creating 3D Scenes.
8. Image Editing and Manipulation - Basic Operations on image using any image editing software,
Creating gif animated images, Image optimization.
TOTAL: 45 PERIODS
INDEX
AIM
ALGORITHM
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int x,y,x1,y1,x2,y2,gx,gy,dx,dy,m,i,p;
clrscr();
printf("Enter the starting point of line\n");
scanf("%d%d",&x1,&y1);
printf("Enter the end point of the line\n");
scanf("%d%d",&x2,&y2);
detectgraph(&gx,&gy);
initgraph(&gx,&gy,"c:\\tc\\bgi");
dx=x2-x1;
dy=y2-y1;
p=(2*dy)-dx;
for(i=1;i<dx;i++)
{
if(p<0)
{
x=x1++;
y=y1;
putpixel(x,y,30);
p=p+(2*dy);
}
else
{
x=x1++;
y=y1++;
putpixel(x,y,30);
p=p+(2*dy)-(2*dx);
}
}
getch();
}
OUTPUT:
Enter the starting point of line
100
100
Enter the end point of the line
200
200
RESULT
Thus the C program to draw a line using Bresenham’s line drawing algorithm was created
and executed successfully.
EX NO 1(ii) MIDPOINT ELLIPSE DRAWING ALGORITHM
AIM
To write a C program to draw an ellipse using Bresenham’s ellipse drawing algorithm.
ALGORITHM
Step 1: Input radius rx, ry and ellipse center (Xc, Yc) and obtain the first point on the
circumference of a circle centered on the origin as (X0, Y0) = (0, ry)
Step 2: Calculate the initial values of the decision parameter in region 1 as
P10 = r2y – r2x ry + 1/4 r2x
Step 3: At each Xk position in region 1, starting at k = 0, perform the following test:
If P1k< 0, the next point to plot is (Xk+1, Yk) and
P1k+1 = P1k+2 r2yXk+1 + r2y
Otherwise the next point is (Xk+1, Yk-1) and
P1k+1 = P1k+2 r2yXk+1 - 2r2xYk+1 + r2y
With
2 r2yXk+1=2 r2yXk+ 2r2y
2r2xYk+1=2r2xYk- 2r2x
Step 4: Calculate the initial values of the decision parameter in region 2 as
P20 = r2y(X0+1/2)2+ r2x(Y0 – 1)2- r2x r2y
Step 5: At each position starting at Yk position in region 2, starting at k = 0,perform the
following test:
If P2k> 0, the next point to plot is (Xk, Yk-1) and
P2k+1 = P2k - 2 r2yYk+1 + r2x
Otherwise the next point is (Xk+1, Yk-1) and
P2k+1 = P2k - 2 r2yXk+1 - 2r2xYk+1 + r2x
Step 6: Determine symmetry points in the other three octants
Step 7: Move each pixel position(X, Y) onto the circular path centered on
(Xc, Yc) and plot the coordinate values as
X = X + Xc Y = Y + Yc
Step 8: Repeat steps for region 1 until 2 r2yX>=2 r2xY
CODING
#include<stdio.h>
#include<graphics.h>
#include<math.h>
void main()
{
long d1,d2;
int i,gd,gm,x,y,x0,y0;
long rx,ry,rxsq,rysq,tworxsq,tworysq,dx,dy;
clrscr();printf("Enter the X radius and Y radius of the ellipse:\n");
scanf("%ld%ld",&rx,&ry);
printf("\nEnter the center (x,y) of the ellipse:\n");
scanf("%d%d",&x0,&y0);
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\BGI");
cleardevice();
rxsq=rx*rx;
rysq=ry*ry;
tworxsq=2*rxsq;
tworysq=2*rysq;
x=0;
y=ry;
d1=rysq-rxsq*ry+(0.25*rxsq);
dx=tworysq*x;
dy=tworxsq*y;
do
{
putpixel(x0+x,y0+y,15);
putpixel(x0-x,y0-y,15);
putpixel(x0+x,y0-y,15);
putpixel(x0-x,y0+y,15);
if(d1<0)
{
x=x+1;
y=y;
dx=dx+tworysq;
d1=d1+dx+rysq;
}
else
{
x=x+1;
y=y-1;
dx=dx+tworysq;
dy=dy-tworxsq;
d1=d1+dx-dy+rysq;
}
delay(10);
}
while(dx<dy);
d2=rysq*(x+0.5)*(x+0.5)+rxsq*(y-1)*(y-1)-(rxsq*rysq);
do
{
putpixel(x0+x,y0+y,15);
putpixel(x0-x,y0-y,15);
putpixel(x0+x,y0-y,15);
putpixel(x0-x,y0+y,15);
if(d2>0)
{
x=x;
y=y-1;
dy=dy-tworxsq;
d2=d2-dy+rxsq;
}
else
{
x=x+1;
y=y-1;
dx=dx+tworysq;
dy=dy-tworxsq;
d2=d2+dx-dy+rxsq;
}
}
while(y>0);
getch(); closegraph();
}
OUTPUT
Enter the X radius and Y radius of the ellipse:
150
100
Enter the center (x,y) of the ellipse:
200
200
RESULT
Thus the C program to draw a line using Bresenham’s Ellipse drawing algorithm was
created and executed successfully.
AIM
To write a C program to draw a circle using 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 a
circle centered on the origin as (X0, Y0) = (0, r)
Step 2: Calculate the initial values of the decision parameter as
P0 = 5/4 – r
Step 3: At each position starting at k perform the following test:
If Pk< 0, the next point to plot is (Xk+1, Yk) and
Pk+1 = Pk+2 Xk+1 + 1
Otherwise the next point is (Xk+1, Yk-1) and
Pk+1 = Pk+2 Xk+1 + 1- 2Yk+1
where 2Xk+1=2Xk+2 and 2Yk+1=2Yk-2
Step 4: Determine symmetry points in the other seven octants
Step 5: Move each pixel position(X, Y) onto the circular path centered on(Xc, Yc) and plot the
coordinate values as
X = X + Xc Y = Y + Yc
Step 6: Repeat steps 3 through until X>=Y
CODING
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
int x,y,r;
void cir(int,int,int);
printf("Enter the Mid points and Radius:");
scanf("%d%d%d",&x,&y,&r);
initgraph(&gd,&gm,"c:\\tc\\BGI ");
cir(x,y,r);
getch();
closegraph();
}
void cir(int x1,int y1,int r)
{
int x=0,y=r,p=1-r;
void cliplot(int,int,int,int);
cliplot(x1,y1,x,y);
while(x<y)
{
x++;
if(p<0)
p+=2*x+1;
else
{
y--;
p+=2*(x-y)+1;
}
cliplot(x1,y1,x,y);
}
}
void cliplot(int xctr,int yctr,int x,int y)
{
putpixel(xctr +x,yctr +y,1);
putpixel(xctr -x,yctr +y,1);
putpixel(xctr +x,yctr -y,1);
putpixel(xctr -x,yctr -y,1);
putpixel(xctr +y,yctr +x,1);
putpixel(xctr -y,yctr +x,1);
putpixel(xctr +y,yctr -x,1);
putpixel(xctr -y,yctr -x,1);
getch();
}
OUTPUT
Enter the Mid points and Radius:
200
50
50
RESULT
Thus the C program to draw a line using bresenham’s line drawing algorithm was created
and executed successfully.
AIM
To write a C program to draw a line using Digital Differential Analyzer line drawing algorithm.
ALGORITHM
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int x,y,x1,y1,x2,y2,xinc,yinc,gx,gy,dx,dy,i,k;
clrscr();
printf("Enter the starting point of line\n");
scanf("%d%d",&x1,&y1);
printf("Enter the end point of the line\n");
scanf("%d%d",&x2,&y2);
detectgraph(&gx,&gy);
initgraph(&gx,&gy,"c:\\tc\\bgi");
dx=x2-x1;
dy=y2-y1;
if(dx>dy)
k=dx;
else
k=dy;
xinc=dx/k;
yinc=dy/k;
x=x1;
y=y1;
putpixel(x,y,30);
for(i=0;i<k;i++)
{
x=x+xinc;
y=y+yinc
putpixel(x,y,30);
}
getch();
}
OUTPUT:
EX NO 2 IMPLEMENTATION OF 2D TRANSFORMATION
AIM
ALGORITHM
CODING
#include<stdio.h>
#include<conio.h>
#include<process.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++)
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;
b[2][2]=1;
mul(n,b,c,a);
setcolor(RED);
disp(n,a);
}
void reflection(int n,float c[][3])
{
float b[10][3],a[10][3];
int i=0,ch,j;
cleardevice();
printf("\n\t**MENU**");
printf("\n\t1.About X-axis");
printf("\n\t2.About Y-axis");
printf("\n\t3.About Origin");
printf("\n\t4.About x=y");
printf("\n\t5.About -x=y");
printf("\n\t6.Exit");
printf("\n\tEnter your choice:");
scanf("%d",&ch);
clrscr();
cleardevice();
disp(n,c);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
b[i][j]=0;
if(i==j)
b[i][j]=1;
}
switch(ch)
{
case 1:b[1][1]=-1;
break;
case 2:b[0][0]=-1;
break;
case 3:b[0][0]=-1;b[1][1]=-1;
break;
case 4:b[0][0]=0;b[1][1]=0;b[0][1]=1;b[1][0]=1;
break;
case 5:b[0][0]=0;b[1][1]=0;b[0][1]=-1;b[1][0]=-1;
break;
case 6:break;
default:printf("\n\tInvalid choice");
break;
}
mul(n,b,c,a);
setcolor(RED);
disp(n,a);
}
void shearing(int n,float c[][3])
{
float b[10][3],sh,a[10][3];
int i=0,ch,j;
cleardevice();
printf("\n\t**MENU**");
printf("\n\t1.X-SHEAR");
printf("\n\t2.Y-SHEAR");
printf("\n\t3.EXIT");
printf("\n\tENTER UR CHOICE:");
scanf("%d",&ch);
if(ch==3)
return;
printf("\n\tENTER VALUE FOR SHEAR:");
scanf("%f",&sh);
clrscr();
cleardevice();
for(i=0;i<3;i++)
for(j=0;j<3;j++)
b[i][j]=0;
for(i=0;i<3;i++)
b[i][i]=1;
switch(ch)
{
case 1:b[1][0]=sh;break;
case 2:b[0][1]=sh;break;
case 3:break;
default:printf("\n\tINVALID CHOICE!");
break;
}
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,"c:\\tc\\bgi");
printf("\nEnter no. of vertices:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEter the co-ordinates of %d vertex:",i+1);
scanf("%f %f",&c[i][0],&c[i][1]);
c[i][2]=1;
}
do
{
clrscr();
cleardevice();
printf("\n\t**MENU**");
printf("\n\t1.TRANSLATION");
printf("\n\t2.SCALING");
printf("\n\t3.ROTATION");
printf("\n\t4.REFLECTION");
printf("\n\t5.SHEARING");
printf("\n\t6.EXIT");
printf("\n\tENTER UR CHOICE:");
scanf("%d",&cho);
switch(cho)
{
case 1:printf("\n\tEnter translation factor for x and y axis:\t");
scanf("%f %f",&tx,&ty);
clrscr();
cleardevice();
setcolor(BLUE);
disp(n,c);
translation(n,c,tx,ty);
setcolor(RED);
disp(n,c);
getch();
break;
case 2:printf("\n\tEnter scaling factor for x and y axis:\t");
scanf("%f %f",&sx,&sy);
clrscr();
cleardevice();
setcolor(BLUE);
disp(n,c);
scaling(n,c,sx,sy);
getch();
break;
case 3:printf("\n\tENter angle of rotation:\t");
scanf("%f",&ra);
clrscr();
cleardevice();
setcolor(BLUE);
disp(n,c);
rotation(n,c,ra);getch();
break;
case 4:clrscr();
cleardevice();
setcolor(BLUE);
disp(n,c);
reflection(n,c);
getch();
break;
case 5:clrscr();
cleardevice();
setcolor(BLUE);
disp(n,c);
shearing(n,c);
getch();
break;
case 6:exit(0);
break;
default:printf("\n\tInvalid choice!");
break;
}
}while(cho!=7);
getch();
closegraph();
}
OUTPUT
RESULT
Thus the C Program to perform the various 2-dimensional transformations such as translation,
rotation, scaling, reflection and shearing has been created and executed successfully.
EX NO 3 IMPLEMENTATION OF 2D COMPOSITE TRANSFORMATION
AIM
To write a C program to perform the various composite 2-dimensional transformations
such as translation, scaling and rotation.
ALGORITHM
CODING
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
int ch,x,y,az,i,w,ch1,ch2,xf,yf,ra,a[10],b[10],n,da,db;
float
x1,y1,az1,w1,tx,ty,theta,shx,shy,sx,sy,a1[10],b1[10],c[10][3],d[10][3],e[10][3],f[10][3],g[10][3],
h[10][3],j[10][3],k[10][3],l[10][3];
void disp(int n,float c[][3])
{
int i;
for(i=0;i<n-1;i++)
{
line(c[i][0]+200,c[i][1]+200,c[i+1][0]+200,c[i+1][1]+200);
}
i=n-1;
line(c[i][0]+200,c[i][1]+200,c[0][0]+200,c[0][1]+200);
}
void main()
{
int gd=DETECT,gm;
clrscr();
initgraph(&gd,&gm,"c:\\tc\\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;
}
disp(n,c);
delay(100);
cleardevice();
while(1)
{
printf("******COMPOSITE 2D TRANSFORMATION*******\n");
printf("1.Translation and Scaling\n2.Scaling and rotation\n3.Translation and
ratation\n4.Exit\nEnter your choice:\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("*******Translation and scaling*******\n\n");
printf("Enter the value of translation factor and Scaling:\n");
scanf("%f%f%f%f",&tx,&ty,&sx,&sy);
for(i=0;i<n;i++)
{
d[i][0]=c[i][0]+tx;
d[i][1]=c[i][1]+ty;
}
for(i=0;i<n;i++)
{
f[i][0]=d[i][0]*sx;
f[i][1]=d[i][1]*sy;
}
disp(n,d);
disp(n,f);
delay(5000);
cleardevice();
break;
case 2:
disp(n,d);
printf("*******Scaling and Rotation*******\n\n");
printf("Enter the values :\n");
scanf("%f%f%d%d%d",&sx,&sy,&xf,&yf,&ra);
theta=(float)(ra*(3.14/180));
for(i=0;i<n;i++)
{
f[i][0]=d[i][0]*sx;
f[i][1]=d[i][1]*sy;
e[i][0]=(xf+((f[i][0]-xf)*cos(theta)-(f[i][1]-yf)*sin(theta)));
e[i][1]=(yf+((f[i][0]-xf)*sin(theta)+(f[i][1]-yf)*cos(theta)));
}
disp(n,f);
disp(n,e);
delay(5000);
cleardevice();
break;
case 3:
disp(n,d);
printf("********Translation and Rotation*******\n\n");
printf("Enter the values ");
scanf("%f%f%d%d%d",&tx,&ty,&xf,&yf,&ra);
setcolor(RED);
for(i=0;i<n;i++)
{
d[i][0]=c[i][0]+tx;
d[i][1]=c[i][1]+ty;
e[i][0]=(xf+((d[i][0]-xf)*cos(theta)-(d[i][1]-yf)*sin(theta)));
e[i][1]=(yf+((d[i][0]-xf)*sin(theta)+(d[i][1]-yf)*cos(theta)));
}
disp(n,d);
disp(n,e);
break;
case 4:
exit(0);
}}
getch();
}
OUTPUT
RESULT
Thus the C program to perform various composite 2 dimensional transformations was
created and executed successfully.
EX NO 5 IMPLEMENTATION OF 3D TRANSFORMATIONS
AIM
To write a C program to perform the various 3-dimensional transformations such as
translation, scaling, rotation.
ALGORITHM
CODING
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
int ch,x,y,az,i,j,w,ch1,ch2,xf,yf,zf,ra,a[10],b[10],n,da,db;
float
x1,y1,az1,w1,tx,ty,tz,sz,theta,shx,shy,sx,sy,a1[10],b1[10],c[10][3],d[10][3],e[10][3],f[10][3],g[1
0][3],h[10][3],k[10][3],l[10][3];
void disp(int n,float c[][3])
{
int i;
for(i=0;i<n;i++)
{
line(c[i][0]+200,c[i][1]+200,c[i+1][0]+200,c[i+1][1]+200);
}
for (j=0;j<n-1;j++)
{
line(c[j][0]+200,c[j][1]+200,c[n][0]+200,c[n][1]+200);
}
line(c[2][0]+200,c[2][1]+200,c[0][0]+200,c[0][1]+200);
}
void main()
{
int gd=DETECT,gm;
clrscr();
initgraph(&gd,&gm,"c:\\tc\\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%f",&c[i][0],&c[i][1],&c[i][2]);
c[i][3]=1;
}
disp(n,c);
while(1)
{
printf("******3D Transformations*******\n");
printf("1.Translation\n2.Rotation\n3.Scaling\n4.Exit\nEnter your choice:\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
cleardevice();
disp(n,c);
printf("*******Translation*******\n\n");
printf("Enter the value of translation factor:\n");
scanf("%f%f%f",&tx,&ty,&tz);
for(i=0;i<n;i++)
{
d[i][0]=c[i][0]+tx;
d[i][1]=c[i][1]+ty;
d[i][2]=c[i][2]+tz;
}
cleardevice();
disp(n,d);
break;
case 2:
cleardevice();
disp(n,c);
printf("*******Rotation*******\n\n");
printf("Enter the value of fixed point and angle of rotation:Enter the value of fixed point and
angle of rotation:\n");
scanf("%d%d%d%d",&xf,&yf,&zf,&ra);
theta=(float)(ra*(3.14/180));
for(i=0;i<n;i++)
{
e[i][0]=(xf+((c[i][0]-xf)*cos(theta)-(c[i][1]-yf)*sin(theta)));
e[i][1]=(yf+((c[i][0]-xf)*sin(theta)+(c[i][1]-yf)*cos(theta)));
e[i][2]=zf+c[i][2];
}
cleardevice();
disp(n,e);
break;
cleardevice();
case 3:
disp(n,c);
printf("********Scaling*******\n\n");
printf("Enter the value of scaling factor:\n");
scanf("%f%f%f",&sx,&sy,&sz);
for(i=0;i<n;i++)
{
f[i][0]=c[i][0]*sx;
f[i][1]=c[i][1]*sy;
f[i][2]=c[i][2]*sz;
}
cleardevice();
disp(n,f);
break;
case 4:
exit(0);
}
}
getch();
}
OUTPUT
RESULT
Thus C program to perform the various 3-dimensional transformations such as
translation, scaling, rotation was created and successfully executed.
EX NO 5(i) IMPLEMENTATION OF 3D COMPOSITE TRANSFORMATION
AIM
To perform the various composite 3-dimensional transformations such as translation,
scaling, rotation.
ALGORITHM
CODING
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
int ch,x,y,az,i,j,w,ch1,ch2,xf,yf,zf,ra,a[10],b[10],n,da,db;
float
x1,y1,az1,w1,tx,ty,tz,theta,shx,shy,shz,sx,sy,sz,a1[10],b1[10],c[10][3],d[10][3],e[10][3],f[10][3],
g[10][3],h[10][3],k[10][3],l[10][3];
void disp(int n,float c[][3])
{
int i;
for(i=0;i<n;i++)
{
line(c[i][0]+200,c[i][1]+200,c[i+1][0]+200,c[i+1][1]+200);
}
for(j=0;j<n-1;j++)
{
line(c[j][0]+200,c[j][1]+200,c[n][0]+200,c[n][1]+200);
}
line(c[2][0]+200,c[2][1]+200,c[0][0]+200,c[0][1]+200);
}
void main()
{
int gd=DETECT,gm;
clrscr();
initgraph(&gd,&gm,"c:\\tc\\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%f",&c[i][0],&c[i][1],&c[i][2]);
c[i][3]=1;
}
disp(n,c);
delay(100);
cleardevice();
while(1)
{
printf("******3D Composite Transformations*******\n");
printf("1.Translation and scaling\n2.Scaling and Rotation\n3.Translation and
Rotation\n4.Exit\nEnter your choice:\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
disp(n,c);
printf("*******Translation and Scaling*******\n\n");
printf("Enter the value of translation factor & scaling factor:\n");
scanf("%f%f%f%f%f%f",&tx,&ty,&tz,&sx,&sy,&sz);
for(i=0;i<n;i++)
{
d[i][0]=c[i][0]+tx;
d[i][1]=c[i][1]+ty;
d[i][2]=c[i][2]+tz;
}
for(i=0;i<n;i++)
{
f[i][0]=d[i][0]*sx;
f[i][1]=d[i][1]*sy;
f[i][2]=d[i][2]*sz;
}
disp(n,d);
disp(n,f);
delay(5000);
cleardevice();
break;
case 2:
disp(n,d);
printf("*******Scaling & Rotation*******\n\n");
printf("Enter the values :\n");
scanf("%f%f%f%d%d%d%d",&sx,&sy,&sz,&xf,&yf,&zf,&ra);
theta=(float)(ra*(3.14/180));
for(i=0;i<n;i++)
{
f[i][0]=d[i][0]*sx;
f[i][1]=d[i][1]*sy;
f[i][2]=d[i][2]*sz;
e[i][0]=(xf+((f[i][0]-xf)*cos(theta)-(f[i][1]-yf)*sin(theta)));
e[i][1]=(yf+((f[i][0]-xf)*sin(theta)+(f[i][1]-yf)*cos(theta)));
e[i][2]=zf+f[i][2];
}
disp(n,f);
disp(n,e);
delay(5000);
cleardevice();
break;
case 3:
disp(n,c);
printf("********Translation & Rotation*******\n\n");
printf("Enter the values:\n");
scanf("%f%f%f%d%d%d%d",&tx,&ty,&tz,&xf,&yf,&zf,&ra);
setcolor(RED);
for(i=0;i<n;i++)
{
d[i][0]=c[i][0]+tx;
d[i][1]=c[i][1]+ty;
d[i][2]=c[i][2]+tz;
e[i][0]=(xf+((d[i][0]-xf)*cos(theta)-(d[i][1]-yf)*sin(theta)));
e[i][1]=(yf+((d[i][0]-xf)*sin(theta)+(d[i][1]-yf)*cos(theta)));
e[i][2]=zf+d[i][2];
}
disp(n,d);
disp(n,e);
//cleardevice();
break;
case 4:
exit(0);
}
}
getch();
}
OUTPUT
RESULT
Thus the C program to perform the various composite 3-dimensional transformations
such as translation, scaling, rotation was created and executed successfully.
EX NO 4 IMPLEMENTATION OF COHEN SUTHERLAND LINE CLIPPING
AIM
To write a C program to clip a line using Cohen-Sutherland clipping algorithm.
ALGORITHM
Step 1Every line endpoint is assigned a four digit binary code, called region code, that identifies
the location of the point relative to the boundaries of the clipping rectangle.
Step 2Each bit position in the region code is used to indicate one of the four relative coordinate
positions of the point with respect to the clip window.
Bit 1: left
Bit 2: right
Bit 3: below
Bit 4: above
Step 3Bit values in the region code are determined by comparing endpoint coordinates values
(x, y) with respect to the clip boundaries. eg.Bit 1 is set to 1 if x<xwmin
Step 4Once we have established region codes for all line endpoints, we can quickly determine
which lines are completely outside or inside the clip window.
Step 5Lines that cannot be identified as completely inside or outside a clip window are checked
for intersection with boundaries.
Step 6Intersection points with a clipping boundary can be calculated using the slope-intercept
form of the line equation.
m ( y2 y1 ) ( x2 x1 )
Step 7The y coordinate of the intersection point at vertical line
y y1 m( x x1 )
Step 8The x coordinate of the intersection point at horizontal line
x x1
y y1 y yw min
m
y yw max
CODING
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
#include<dos.h>
#include<process.h>
int pixels[2][4];
float xn1,xn2,xn3,yn1,yn2,x3,y3,m;
int xmin,ymin,xmax,ymax,x1,y1,x2,y2;
int choice,ed[20],num;
void su_co(int x1,int y1,int x2,int y2,int xmin,int ymin,int xmax,int ymax)
{
int i,j,fl;
for(i=0;i<2;i++)
for(j=0;j<4;j++)
pixels[i][j]=0;
if(y1>ymax)
pixels[0][0]=1;
if(y1<ymin)
pixels[0][1]=1;
if(x1>xmax)
pixels[0][2]=1;
if(x1<xmin)
pixels[0][3]=1;
if(y2>ymax)
pixels[1][0]=1;
if(y2<ymin)
pixels[1][1]=1;
if(x2>xmax)
pixels[1][2]=1;
if(x2<xmin)
pixels[1][3]=1;
for(j=0;j<4;j++)
{
if(pixels[0][j]==0&&pixels[1][j]==0)
continue;
if(pixels[0][j]==1&&pixels[1][j]==1)
{
fl=3;
break;
}
fl=2;
}
switch(fl)
{
case 1:
line(320+x1,240-y1,320+x2,240-y2);
break;
case 3:
printf("\n\n\t line is not visble.......");
break;
case 2:
m=(y2-y1)/(x2-x1);
xn1=x1;
yn1=y1;
xn2=x2;
yn2=y2;
if(pixels[0][0]==1)
{
xn1=x1+(ymax-y1)/m;
yn1=ymax;
}
if(pixels[0][1]==1)
{
xn1=x1+(ymin-y1)/m;
yn1=ymin;
}
if(pixels[0][2]==1)
{
yn1=y1+(xmax-x1)*m;
xn1=xmax;
}
if(pixels[0][3]==1)
{
yn1=y1+(xmin-x1)*m;
xn1=xmin;
}
if(pixels[1][0]==1)
{
xn2=x2+(ymax-y2)/m;
yn2=ymax;
}
if(pixels[1][1]==1)
{
xn2=x2+(ymin-y2)/m;
yn2=ymin;
}
if(pixels[1][2]==1)
{
yn2=y2+(xmax-x2)*m;
xn2=xmax;
}
if(pixels[1][3]==1)
{
yn2=y2+(xmin-x2)*m;
xn2=xmin;
}
line(320+xn1,240-yn1,320+xn2,240-yn2);
break;
}
}
void cohen()
{
clearviewport();
line(320+xmin,240-ymin,320+xmin,240-ymax);
line(320+xmin,240-ymax,320+xmax,240-ymax);
line(320+xmax,240-ymax,320+xmax,240-ymin);
line(320+xmax,240-ymin,320+xmin,240-ymin);
line(320+x1,240-y1,320+x2,240-y2);
getch();
cleardevice();
line(320+xmin,240-ymin,320+xmin,240-ymax);
line(320+xmin,240-ymax,320+xmax,240-ymax);
line(320+xmax,240-ymax,320+xmax,240-ymin);
line(320+xmax,240-ymin,320+xmin,240-ymin);
su_co(x1,y1,x2,y2,xmin,ymin,xmax,ymax);
getch();
}
void main()
{
int gd=DETECT,gm,i,j;
initgraph(&gd,&gm,"c:\\tc\\bgi");
printf("\n\n\t\t Enter the co-ordinates of the cliping window");
printf("\n\n\t\tEnter X(min)&Y(min)\n:=");
scanf("%d%d",&xmin,&ymin);
printf("\n\t\tenter X(max)&Y(max)\":=");
scanf("%d%d",&xmax,&ymax);
printf("\n\t\t Enter the co-ordinates of the line:");
printf("\n\n\t\tEnter X(1)&Y(1)\n:=");
scanf("%d%d",&x1,&y1);
printf("\n\t\t\n Enter X(2)&Y(2)\n:=");
scanf("%d%d",&x2,&y2);
clrscr();
cohen();
}
OUTPUT
RESULT
Thus the C program to clip a line using Cohen Sutherland line clipping Algorithm was created
and executed successfully.
EX NO 7 (a) DRAWING A TEAPOT-OPENGL
AIM
To write a C++ program to draw a teapot using open graphics library (OPENGL).
ALGORITHM
Step 1 Include the various header files.
Step 2 Define the width and height of the window.
Step 3Define the projection matrix and model view matrix using glMatrixMode.
Step 4 Draw solid tea pot using glutSolidTeapot () and wire teapot using glutwireTeapot ()
Step 5 Perform various effects using glEnable(GL_LIGHTING),glEnable(GL_LIGHT0)
glShadeModel(GL_SMOOTH), glEnable(GL_DEPTH_TEST) etc
Step 6 Stop
CODING
#include<GL/glut.h>
#include<math.h>
#define W 600
#define H 600
void displaySolid(void)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-W,W,-H,H,-W,W);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0,0,10,0,0,0,0.0,1.0,0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//glTranslatef(100.0,0.0,0.0);
//glScalef(1.5,0.5,1.0);
//glRotatef(9.0,0.0,0.0,1.0);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
glutSolidTeapot(100);
glutWireTeapot(200);
//glutSolidTorus(200,200,300,300);
glFlush();
glutSwapBuffers();
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitWindowSize(W,H);
glutInitWindowPosition(100,100);
glutCreateWindow("My Teaspot");
glutDisplayFunc(displaySolid);
glClearColor(0.1f,0.1f,0.1f,0.0f);
glViewport(0,0,W,H);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
return 1;
}
OUTPUT
RESULT
Thus the C++ program to draw a teapot using OPENGL has been created and executed
successfully.
EX NO 7(b) DRAWING 3D ANIMATED CUBE IN OPENGL
AIM
To write a C++ program to draw an animated cube using open graphics library (OPENGL).
PROGRAM
#include <GL\glut.h>
GLfloat xRotated, yRotated, zRotated;
void init(void)
{
glClearColor(0,0,0,0);
void DrawCube(void)
{
glMatrixMode(GL_MODELVIEW);
// clear the drawing buffer.
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0,0.0,-10.5);
glRotatef(xRotated,1.0,0.0,0.0);
// rotation about Y axis
glRotatef(yRotated,0.0,1.0,0.0);
// rotation about Z axis
glRotatef(zRotated,0.0,0.0,1.0);
glBegin(GL_QUADS); // Draw The Cube Using quads
glColor3f(0.0f,1.0f,0.0f); // Color Blue
glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Top)
glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Top)
glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Quad (Top)
glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right Of The Quad (Top)
glColor3f(1.0f,0.5f,0.0f); // Color Orange
glVertex3f( 1.0f,-1.0f, 1.0f); // Top Right Of The Quad (Bottom)
glVertex3f(-1.0f,-1.0f, 1.0f); // Top Left Of The Quad (Bottom)
glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Bottom)
glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Bottom)
glColor3f(1.0f,0.0f,0.0f); // Color Red
glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Front)
glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Front)
glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Front)
glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad (Front)
glColor3f(1.0f,1.0f,0.0f); // Color Yellow
glVertex3f( 1.0f,-1.0f,-1.0f); // Top Right Of The Quad (Back)
glVertex3f(-1.0f,-1.0f,-1.0f); // Top Left Of The Quad (Back)
glVertex3f(-1.0f, 1.0f,-1.0f); // Bottom Left Of The Quad (Back)
glVertex3f( 1.0f, 1.0f,-1.0f); // Bottom Right Of The Quad (Back)
glColor3f(0.0f,0.0f,1.0f); // Color Blue
glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Left)
glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Left)
glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Left)
glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad (Left)
glColor3f(1.0f,0.0f,1.0f); // Color Violet
glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Right)
glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Right)
glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Right)
glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Right)
glEnd(); // End Drawing The Cube
glFlush();
}
void animation(void)
{
yRotated += 0.01;
xRotated += 0.02;
DrawCube();
}
gluPerspective(40.0,(GLdouble)x/(GLdouble)y,0.5,20.0);
glMatrixMode(GL_MODELVIEW);
glViewport(0,0,x,y); //Use the whole window for rendering
}
glutInit(&argc, argv);
//we initizlilze the glut. functions
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowPosition(100, 100);
glutCreateWindow("Animated 3D Cube Using OpenGL");
init();
glutDisplayFunc(DrawCube);
glutReshapeFunc(reshape);
//Set the function for the animation.
glutIdleFunc(animation);
glutMainLoop();
return 0;
}
OUTPUT:
RESULT
Thus the C++ program to draw an animated 3D cube using OPENGL has been created
and executed successfully.
EX NO 10 (a) DRAWING TORUS USING OPENGL
AIM
To draw a torus using visual c++ with OPENGL.
ALGORITHM
Step 1: Start the program.
Step 2: Configure all the Opengl files.
Step 3: Initialize matrix mode function and convert it to identity function.
Step 4: Set the window size and its objects.
Step 5: Set the position and orientation of the object using the function gluLookAt().
Step6: Display the teapot in solid form and wire form using glutSolidTorus() and
glutWireTorus() functions.
Step 7: Display the object using glutDisplayFunc() .
CODING
#include<GL/glut.h>
#include<math.h>
#define W 600
#define H 600
void displaySolid(void)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-W,W,-H,H,-W,W);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0,0,10,0,0,0,0.0,1.0,0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glTranslatef(100.0,0.0,0.0);
glScalef(1.5,0.5,1.0);
glRotatef(9.0,0.0,0.0,1.0);
glutWireTorus(100,200,300,400);
glFlush();
glutSwapBuffers();
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(W,H);
glutInitWindowPosition(100,100);
glutCreateWindow("My Torus");
glutDisplayFunc(displaySolid);
glClearColor(0.1f,0.1f,0.1f,0.0f);
glViewport(0,0,W,H);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
return 1;
}
RESULT
Thus the torus using visual C++ with OPENGL was drawn and the output was verified.
EX NO 8 COMPOSITE 3-D OBJECT TRANSFORMATION USING OPENGL
AIM
To draw a 3D object and perform transformations using Visual C++ with OPENGL.
PROGRAM
#include <GL/glut.h>
glPopMatrix();
glutSwapBuffers();
}
void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective(50.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef (0.0, 0.0, -4.0);
}
void keyboard (unsigned char key, int x, int y)
{
switch (key) {
case 's':
shoulder = (shoulder + 5) % 360;
glutPostRedisplay();
break;
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, 200);
glutInitWindowPosition (50, 100);
glutCreateWindow ("Composite 3D Transformation Using OpenGL");
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
OUTPUT:
RESULT
Thus the program to draw a 3D object and perform transformations using visual C++
with OPENGL has been created successfully.