You are on page 1of 45

UNIVERSITY SCHOOL OF INFORMATION AND

COMMUNICATION TECHNOLOGY
GURU GOBIND SINGH INDRAPRASTHA
UNIVERSITY, NEW DELHI-110078
(2020-2024)

PRACTICAL FILE
OF
COMPUTER GRAPHICS LAB
IT -257
Submitted in partial fulfilment of the requirements
for the award of the degree of

BACHELOR OF TECHNOLOGY
IN
INFORMATION TECHNOLOGY

Submitted to: - NAME: AKSHAT KUMAR


Dr. ANURADHA CHUG
IT-257 ENROLLMENT NO: 00716401520

B.Tech IT 3rd Sem


INDEX

S. No Name of the Programs

1. Draw a Line using DDA Line algorithm

2. Draw a Line using Bresenham’s Line algorithm

3. Draw a Circle using Midpoint circle algorithm

4. Draw a Circle using Bresenham’s circle algorithm

5. Draw a polygon with n number of vertices

6. Perform all three 2D transformations i.e. Translation,


rotation and scaling on a given object

7. Draw an ellipse using Midpoint ellipse theorem

8. Cohen Sutherland Line Clipping Algorithm

9. Polygon Clipping

10. Program to draw Bezier Curve using 4 control points


PROGRAM 1:
DDA Line Algorithm.

#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
float x,y,x1,y1,x2,y2,dx,dy,steps,xinc,yinc;
cout<<"Enter the x1 coordinate:";
cin>>x1;
cout<<"Enter the y1 coordinate:";
cin>>y1;
cout<<"Enter the x2 coordinate:";
cin>>x2;
cout<<"Enter the y2 coordinate:";
cin>>y2;
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy))
{
steps=abs(dx);
}
else
{
steps=abs(dy);
}
xinc=dx/steps;
yinc=dy/steps;
x=x1;
y=y1;
for(int i=1;i<=steps;i++)
{
putpixel(x,y,5);
x=x+xinc;
y=y+yinc;
}
getch();
closegraph();
}

OUTPUT:
PROGRAM 2:
Bresenham’s Line Algorithm.

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
int x,y,x1,y1,x2,y2,dx,dy,p;
cout<<"Enter the x1 coordinate:";
cin>>x1;
cout<<"Enter the y1 coordinate:";
cin>>y1;
cout<<"Enter the x2 coordinate:";
cin>>x2;
cout<<"Enter the y2 coordinate:";
cin>>y2;
x=x1;
y=y1;
dx=x2-x1;
dy=y2-y1;
p=2*dy-dx;
for(x=x1;x<=x2;x++)
{
if(p<0)
{
putpixel(x,y,3);
p=p+2*dy;
}
else
{
putpixel(x,y,3);
p=p+2*dy-2*dx;
y++;
}
}
getch();
closegraph();
}

OUTPUT:
PROGRAM 3:
Draw a circle using mid-point circle algorithm.

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void plotcircle(int x,int y,int xc,int yc)
{
putpixel(x+xc,y+yc,7);
putpixel(x+xc,-y+yc,7);
putpixel(-x+xc,-y+yc,7);
putpixel(-x+xc,y+yc,7);
putpixel(y+xc,x+yc,7);
putpixel(y+xc,-x+yc,7);
putpixel(-y+xc,-x+yc,7);
putpixel(-y+xc,x+yc,7);
}
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
int r,xc,yc,p,x=0,y;
cout<<"Enter the radius of the circle:";
cin>>r;
y=r;
p=1-r;
cout<<"Enter the x coordinate of center:";
cin>>xc;
cout<<"Enter the y coordinate of center:";
cin>>yc;
plotcircle(x,y,xc,yc);
while(x<=y)
{
if(p<0)
{
x=x+1;
plotcircle(x,y,xc,yc);
p=p+2*x+1;
}
else
{
x=x+1;
y=y-1;
p=p+2*x+1-2*y;
}
plotcircle(x,y,xc,yc);
}
getch();
closegraph();
}

OUTPUT:
PROGRAM 4:
Draw a circle using Bresennham’s circle algorithm.

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void plotcircle(int x,int y,int xc,int yc)
{
putpixel(x+xc,y+yc,7);
putpixel(x+xc,-y+yc,7);
putpixel(-x+xc,-y+yc,7);
putpixel(-x+xc,y+yc,7);
putpixel(y+xc,x+yc,7);
putpixel(y+xc,-x+yc,7);
putpixel(-y+xc,-x+yc,7);
putpixel(-y+xc,x+yc,7);
}
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
int r,xc,yc,p,x=0,y;
cout<<"Enter the radius of the circle:";
cin>>r;
y=r;
p=3-2*r;
cout<<"Enter the x coordinate of centre:";
cin>>xc;
cout<<"Enter the y coordinate of centre:";
cin>>yc;
plotcircle(x,y,xc,yc);
while(x<=y)
{
if(p<0)
{
x=x+1;
p=p+4*x+6;
}
else
{
x=x+1;
y=y-1;
p=p+4*(x-y)+10;
}
plotcircle(x,y,xc,yc);
}
getch();
closegraph();
}

OUTPUT:
PROGRAM 5:
Perform all 2D transformations i.e, Translation, rotation,
reflection, sheering and scaling on a given object

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd=DETECT,gm;

initgraph(&gd,&gm,"C:\\TurboC3\\BGI");

int choice;
cout<<"1. Translation"<<endl;
cout<<"2. Rotation"<<endl;
cout<<"3. Reflection"<<endl;
cout<<"4. Scaling"<<endl;
cout<<"5. Shearing"<<endl;
cout<<"Enter your choice:";
cin>>choice;
switch(choice)
{
case 1:
{
int x1,y1,x2,y2,tx,ty;
cout<<endl<<"Enter the coordinates of vertix
of polygon:";
cin>>x1>>y1>>x2>>y2;
setcolor(3);
rectangle(x1,y1,x2,y2);
setcolor(5);
cout<<"Enter translation point about x-axis:";
cin>>tx;
cout<<"Enter translation point about y-axis:";
cin>>ty;
rectangle(x1+tx,y1+ty,x2+tx,y2+ty);
break;
}
case 2:
{
int x1,y1,x2,y2;
cout<<endl<<"Enter the end points:";
cin>>x1>>y1>>x2>>y2;
double a;
setcolor(3);
line(x1,y1,x2,y2);
cout<<"Enter the Angle of rotation:";
cin>>a;
a=(a*3.14)/180;
long xr=((x2)*cos(a)-(y2)*sin(a));
long yr=((x2)*sin(a)+(y2)*cos(a));
setcolor(5);
line(x1,y1,xr,yr);
break;
}
case 3:
{
int x1,y1,x2,y2,x3,y3;
cout<<endl<<"Enter the end points:";
cin>>x1>>y1>>x2>>y2>>x3>>y3;
setcolor(3);
line(x1,y1,x2,y2);
line(x1,y1,x3,y3);
line(x2,y2,x3,y3);
setcolor(5);
line(x1,-y1+500,x2,-y2+500);
line(x1,-y1+500,x3,-y3+500);
line(x2,-y2+500,x3,-y3+500);
break;
}
case 4:
{
int x1,y1,x2,y2,x,y;
cout<<endl<<"Enter the coordinates of vertix
of rectangle:";
cin>>x1>>y1>>x2>>y2;
setcolor(3);
rectangle(x1,y1,x2,y2);
cout<<"Enter scaling factor along x and y:";
cin>>x>>y;
setcolor(5);
rectangle(x1*x,y1*y,x2*x,y2*y);
break;
}
case 5:
{
int x1,y1,x2,y2,x3,y3,x4,y4,sh;
cout<<endl<<"Enter the end points:";
cin>>x1>>y1>>x2>>y2>>x3>>y3>>x4>>y4;
setcolor(3);
line(x1,y1,x2,y2);
line(x1,y1,x3,y3);
line(x3,y3,x4,y4);
line(x2,y2,x4,y4);
cout<<"Enter the shearing factor:";
cin>>sh;
x1=x1+sh*y1;
x2=x2+sh*y2;
x3=x3+sh*y3;
x4=x4+sh*y4;
setcolor(5);
line(x1,y1,x2,y2);
line(x1,y1,x3,y3);
line(x3,y3,x4,y4);
line(x2,y2,x4,y4);
break;
}
default:
{
cout<<"Please enter valid choice"<<endl;
break;
}
}
getch();
closegraph();
}
OUTPUTS:
PROGRAM 6:
Draw a polygon with n number of vertices.

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
float x[100],y[100],temp;
int n,i,flag;
cout<<"Enter the number of vertices of polygon:";
cin>>n;
cout<<endl;
for(i=0;i<n;i++)
{
cout<<"Enter the x and y coordinates for vertix
"<<i+1<<":";
cin>>x[i]>>y[i];
}
for(i=0;i<n-1;i++)
{
flag=0;
if(x[i+1]<x[i])
{
flag=1;
temp=x[i+1];
x[i+1]=x[i];
x[i]=temp;
temp=y[i+1];
y[i+1]=y[i];
y[i]=temp;
}
setcolor(3);
line(x[i],y[i],x[i+1],y[i+1]);
if(flag==1)
{
temp=x[i+1];
x[i+1]=x[i];
x[i]=temp;
temp=y[i+1];
y[i]=temp;
}
}
if(x[0]>x[n-1])
{
temp=x[0];
x[0]=x[n-1];
x[n-1]=temp;
temp=y[n-1];
y[n-1]=y[0];
y[0]=temp;
}
setcolor(3);
line(x[0],y[0],x[n-1],y[n-1]);
getch();
closegraph();
}
OUTPUT:
PROGRAM 7:
Draw an Ellipse using midpoint ellipse theorem.

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
float xc,yc,x,y,a,b;
float fx,fy,d,temp1,temp2;
cout<<"Enter the x coordinate of centre:";
cin>>xc;
cout<<"Enter the y coordinate of centre:";
cin>>yc;
cout<<"Enter the constants:";
cin>>a>>b;
x=0;
y=b;
fx=2*b*b*x;
fy=2*a*a*y;
d=b*b-(a*a*b)+(a*a*0.25);
while(fx<fy)
{
putpixel(xc+x,yc+y,3);
putpixel(xc-x,yc-y,3);
putpixel(xc+x,yc-y,3);
putpixel(xc-x,yc+y,3);
if(d>=0)
{
y=y-1;
d=d+fx-fy+(b*b);
fy=fy-(2*a*a);
}
else
{
d=d+fx+(b*b);
}
x=x+1;
fx=fx+(b*b*2);
}
temp1=(x*x)-0.25;
temp2=(y-1)*(y-1);
d=(b*b*temp1)+(a*a*temp2)-(a*a*b*b);
do
{
putpixel(xc+x,yc+y,3);
putpixel(xc-x,yc-y,3);
putpixel(xc+x,yc-y,3);
putpixel(xc-x,yc+y,3);
if(d>=0)
{
d=d-fy+(a*a);
}
else
{
x=x+1;
d=d+fx-fy+(a*a);
fx=fx+(2*b*b);
}
y=y-1;
fy=fy-(2*a*a);
}while(y>0);
getch();
closegraph();
}
OUTPUT:
PROGRAM 8:
Cohen Sutherland Line Clipping Algorithm.

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
typedef struct coordinates
{
int x,y;
char code[4];
}PT;
void drawwindow();
void drawline(PT p1,PT p2);
PT setcode(PT p);
int visibility(PT p1,PT p2);
PT resetendpt(PT p1,PT p2);
void main()
{
int gd=DETECT,gm;
PT p1,p2,p3,p4,ptemp;
cout<<"Enter x1:";
cin>>p1.x;
cout<<"Enter y1:";
cin>>p1.y;
cout<<"Enter x2:";
cin>>p2.x;
cout<<"Enter y2:";
cin>>p2.y;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
drawwindow();
delay(500);
drawline(p1,p2);
delay(500);
cleardevice();
delay(500);
p1=setcode(p1);
p2=setcode(p2);
int v=visibility(p1,p2);
delay(500);
switch(v)
{
case 0:
{
drawwindow();
delay(500);
drawline(p1,p2);
break;
}
case 1:
{
drawwindow();
delay(500);
break;
}
case 2:
{
p3=resetendpt(p1,p2);
p4=resetendpt(p2,p1);
drawwindow();
delay(500);
drawline(p3,p4);
break;
}
}
delay(5000);
closegraph();
getch();
}
void drawwindow()
{
line(150,100,450,100);
line(450,100,450,350);
line(450,350,150,350);
line(150,350,150,100);
}
void drawline(PT p1,PT p2)
{
line(p1.x,p1.y,p2.x,p2.y);
}
PT 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.x>450)
{
ptemp.code[2]='1';
}
else
{
ptemp.code[2]='0';
}
if(p.x<150)
{
ptemp.code[3]='1';
}
else
{
ptemp.code[3]='0';
}
ptemp.x=p.x;
ptemp.y=p.y;
return(ptemp);
}
int 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';
}
if(flag==0)
{
return(1);
}
return(2);
}
PT 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];
}
return (temp);
}
else
{
return (p1);
}
}
OUTPUT:
PROGRAM 9:
Polygon Clipping.

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#define round(a) ((int)(a+0.5))
int k;
float xmin,ymin,xmax,ymax,arr[20],m;
void clipl(float x1,float y1,float x2,float y2)
{
if(x2-x1)
{
m=(y2-y1)/(x2-x1);
}
else
{
m=100000;
}
if(x1 >= xmin && x2 >= xmin)
{
arr[k]=x2;
arr[k+1]=y2;
k+=2;
}
if(x1 < xmin && x2 >= xmin)
{
arr[k]=xmin;
arr[k+1]=y1+m*(xmin-x1);
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
}
if(x1 >= xmin && x2 < xmin)
{
arr[k]=xmin;
arr[k+1]=y1+m*(xmin-x1);
k+=2;
}
}
void clipt(float x1,float y1,float x2,float y2)
{
if(y2-y1)
{
m=(x2-x1)/(y2-y1);
}
else
{
m=100000;
}
if(y1 <= ymax && y2 <= ymax)
{
arr[k]=x2;
arr[k+1]=y2;
k+=2;
}
if(y1 > ymax && y2 <= ymax)
{
arr[k]=x1+m*(ymax-y1);
arr[k+1]=ymax;
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
}
if(y1 <= ymax && y2 > ymax)
{
arr[k]=x1+m*(ymax-y1);
arr[k+1]=ymax;
k+=2;
}
}
void clipr(float x1,float y1,float x2,float y2)
{
if(x2-x1)
{
m=(y2-y1)/(x2-x1);
}
else
{
m=100000;
}
if(x1 <= xmax && x2 <= xmax)
{
arr[k]=x2;
arr[k+1]=y2;
k+=2;
}
if(x1 > xmax && x2 <= xmax)
{
arr[k]=xmax;
arr[k+1]=y1+m*(xmax-x1);
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
}
if(x1 <= xmax && x2 > xmax)
{
arr[k]=xmax;
arr[k+1]=y1+m*(xmax-x1);
k+=2;
}
}
void clipb(float x1,float y1,float x2,float y2)
{
if(y2-y1)
{
m=(x2-x1)/(y2-y1);
}
else
{
m=100000;
}
if(y1 >= ymin && y2 >= ymin)
{
arr[k]=x2;
arr[k+1]=y2;
k+=2;
}
if(y1 < ymin && y2 >= ymin)
{
arr[k]=x1+m*(ymin-y1);
arr[k+1]=ymin;
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
}
if(y1 >= ymin && y2 < ymin)
{
arr[k]=x1+m*(ymin-y1);
arr[k+1]=ymin;
k+=2;
}
}
void main()
{
int gd=DETECT,gm;
int n,poly[20];
float xi,yi,xf,yf,polyy[20];
clrscr();
cout<<"Coordinates of rectangular clip window:\n";
cout<<"Enter xmin:";
cin>>xmin;
cout<<"Enter ymin:";
cin>>ymin;
cout<<"\nEnter xmax:";
cin>>xmax;
cout<<"Enter ymax:";
cin>>ymax;
cout<<"\n\nPolygon to be clipped \nEnter the Number of
sides:";
cin>>n;
cout<<"Enter the coordinates:";
for(int i=0;i<2*n;i++)
{
cin>>polyy[i];
}
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
for(i=0;i < 2*n+2;i++)
{
poly[i]=round(polyy[i]);
}
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
setcolor(GREEN);
rectangle(xmin,ymax,xmax,ymin);
cout<<"\t\tUNCLIPPED POLYGON";
setcolor(WHITE);
fillpoly(n,poly);
getch();
cleardevice();
k=0;
for(i=0;i < 2*n;i+=2)
{
clipl(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
}
n=k/2;
for(i=0;i < k;i++)
{
polyy[i]=arr[i];
}
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
k=0;
for(i=0;i < 2*n;i+=2)
{
clipt(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
}
n=k/2;
for(i=0;i < k;i++)
{
polyy[i]=arr[i];
}
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
k=0;
for(i=0;i < 2*n;i+=2)
{
clipr(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
}
n=k/2;
for(i=0;i < k;i++)
{
polyy[i]=arr[i];
}
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
k=0;
for(i=0;i < 2*n;i+=2)
{
clipb(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
}
for(i=0;i < k;i++)
{
poly[i]=round(arr[i]);
}
if(k)
fillpoly(k/2,poly);
setcolor(GREEN);
rectangle(xmin,ymax,xmax,ymin);
cout<<"\tCLIPPED POLYGON";
getch();
closegraph();
}

OUTPUT:
PROGRAM 10:
Program to draw Bezier Curve using 4 control points.
#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
void main()
{
int i,x[4],y[4];
float n,xn,yn;
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
cout<<"Enter the x and y coordinates of 4 control
points:"<<endl;
for(i=0;i<4;i++)
{
cin>>x[i]>>y[i];
}
for(n=0.0;n<1.0;n+=0.001)
{
xn=x[0]*pow(1-n,3)+3*n*pow(1-
n,2)*x[1]+3*pow(n,2)*(1-n)*x[2]+pow(n,3)*x[3];

yn=y[0]*pow(1-n,3)+3*n*pow(1-
n,2)*y[1]+3*pow(n,2)*(1-n)*y[2]+pow(n,3)*x[3];

putpixel(xn,yn,YELLOW);
}
getch();
closegraph();
}
OUTPUT:

You might also like