DA6210 Computer Graphics Lab Manual
DA6210 Computer Graphics Lab Manual
DA6210 Computer Graphics Lab Manual
: Date:
Rev No.: Nil Rev. Date: Nil
DA6210-Computer Graphics Lab Clause: Nil Page: 1
Manual
Table of Contents
1 System Requirements 3
2 Lab Objectives 4
Experiment Manual
System Requirements
1. Intel based desktop PC of 2GHz or faster processor with at least 256 MB RAM
2. C or C++ compiler.
Lab Objectives
2. To provide an understanding to draw basic objects like line, polygon, curve etc.
Practical 1
Objective
Introduction of basic graphics functions in C language.
1. getmaxx():
It returns the maximum x co-ordinate value(screen-relative) for the current graphics
driver and mode.
6. setcolor(int color) :
Sets the current drawing color to specified color (which can range from 0 to getmaxcolor
).
10. closegraph(void):
It deallocates all memory allocated by the graphics system.It then restores the screen to
the mode it was before calling initgraph.
Source Code
#include<conio.h>
#include<stdio.h>
#include<graphics.h>
int main()
{
int gd=DETECT,gm,X,Y;
initgraph(&gd,&gm,"");
X=getmaxx();
Y=getmaxy();
printf("%d %d",X,Y);
setcolor(WHITE);
//Quadrant Lines
line(X/2,0,X/2,Y);
line(0,Y/2,X,Y/2);
//Quadrant 1 Smiley
setcolor(YELLOW);
circle(3*X/4,Y/4,X/6); //Main Circle
line((3*X/4),(Y/4)-10,(3*X/4),(Y/4)+10); //Nose
circle((3*X/4)-50,(Y/4)-50,10); //Eye
circle((3*X/4)+50,(Y/4)-50,10); //Eye
ellipse((3*X/4),(Y/4+60),0,360,50,25); //Mouth
setfillstyle(SOLID_FILL,WHITE);
floodfill((3*X/4)-50,(Y/4)-50,YELLOW);//Eye Color
floodfill((3*X/4)+50,(Y/4)-50,YELLOW);//Eye Color
setfillstyle(SLASH_FILL,MAGENTA);
floodfill((3*X/4),(Y/4+60),YELLOW);//Mouth Color
//Quadrant 2 Smiley
setcolor(YELLOW);
circle(X/4,Y/4,X/6); //Main Circle
line(X/4,(Y/4)-10,X/4,(Y/4)+10); //Nose
circle((X/4)-50,(Y/4)-50,10); //Eye
circle((X/4)+50,(Y/4)-50,10); //Eye
arc(X/4,Y/4,232,307,60);//Mouth
ellipse((X/4),(Y/4),215,325,46,80); //Mouth
setfillstyle(XHATCH_FILL,WHITE);
floodfill((X/4)-50,(Y/4)-50,YELLOW);//Eye Color
floodfill((X/4)+50,(Y/4)-50,YELLOW);//Eye Color
setfillstyle(HATCH_FILL,CYAN);
floodfill((X/4),(Y/4+60),YELLOW);//Mouth Color
//Quadrant 3 Smiley
setcolor(YELLOW);
circle(X/4,3*(Y/4),X/6); //Main Circle
line(X/4,3*(Y/4)-10,X/4,3*(Y/4)+10); //Nose
circle((X/4)-50,3*(Y/4)-50,10); //Eye
circle((X/4)+50,3*(Y/4)-50,10); //Eye
line((X/4)-40,3*(Y/4)+40,(X/4)+40,3*(Y/4)+40);//Mouth
ellipse((X/4)+20,3*(Y/4)+40,180,360,20,30);//Mouth
setfillstyle(WIDE_DOT_FILL,WHITE);
floodfill((X/4)-50,3*(Y/4)-50,YELLOW);//Eye Color
floodfill((X/4)+50,3*(Y/4)-50,YELLOW);//Eye Color
setfillstyle(INTERLEAVE_FILL,GREEN);
floodfill((X/4)+20,3*(Y/4)+45,YELLOW);//Mouth Color
//Quadrant 4 Smiley
Prepared by: Reviewed by: Approved by:
Kushal Gupta
Issue No.: Date:
Rev No.: Nil Rev. Date: Nil
DA6210-Computer Graphics Lab Clause: Nil Page: 8
Manual
setcolor(YELLOW);
circle(3*(X/4),3*(Y/4),X/6); //Main Circle
line(3*(X/4),3*(Y/4)-10,3*(X/4),3*(Y/4)+10); //Nose
circle(3*(X/4)-50,3*(Y/4)-50,10); //Eye
circle(3*(X/4)+50,3*(Y/4)-50,10); //Eye
circle(3*(X/4),3*(Y/4)+50,20);//Mouth
setfillstyle(LINE_FILL,WHITE);
floodfill(3*(X/4)-50,3*(Y/4)-50,YELLOW);//Eye Color
floodfill(3*(X/4)+50,3*(Y/4)-50,YELLOW);//Eye Color
setfillstyle(LTSLASH_FILL,LIGHTBLUE);
floodfill(3*(X/4),3*(Y/4)+50,YELLOW);//Mouth Color
getch();
closegraph();
return 0;
}
Output
Practical 2
Objective
Construction of a line using the equation y=mx+c.
Source Code
#include<conio.h>
#include<stdio.h>
#include<graphics.h>
int main()
{
int x1,y1,x2,y2,x,y,i;
float m,c;
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"");
m=(float)(y2-y1)/(x2-x1);
c=(float)y1-(m*x1);
if(m>1)
{
for(i=min(y1,y2);i<=max(y1,y2);i++ )
{
x=(int)((i-c)/m) ;
putpixel(x,i,YELLOW);
}
}
else
{
for(i=min(x1,x2);i<=max(x1,x2);i++)
{
y=(int)((m*i)+c);
putpixel(i,y,CYAN);
}
}
line(x1+10,y1,x2+10,y2);
getch();
closegraph();
return 0;
}
Output
Practical 3
Objective
Construction of lines using Digital Differential Analyzer algorithm.
Source Code
#include<conio.h>
#include<stdio.h>
#include<graphics.h>
if(dx<0)
{
i=y1;
y1=y2;
y2=i;
i=x1;
x1=x2;x2=i;
dy=dy;
dx=-dx;
}
if(dx>0.0)
{
if(m<=1 && m>=-1)
{
for(i=x1;i<=x2;i++)
{
putpixel(i,(int)y1,RED);
y1=y1+m;
}
}
else
{
if(m<-1)
{
x1=x2;
i=y1;
y1=y2;
y2=i;
}
for(i=y1;i<=y2;i++)
{
putpixel((int)x1,i,BLUE);
x1=x1+(1/m);
}
}
}
}
int main()
{
int x1,y1,x2,y2,i;
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"");
for(i=0;i<4;i++)
{
printf("Enter the coordinates of a line \n");
scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
line(x1+5,y1,x2+5,y2);
lineDDA(x1,y1,x2,y2);
}
getch();
closegraph();
return 0;
}
Output
Practical 4
Objective
Construction of a line using Bresenham’s algorithm.
Source Code
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
X=X1;
Y=Y1;
DY=Y2-Y1;
DX=X2-X1;
D=2*DY-DX;
DE=2*DY;
DNE=2*(DY-DX);
putpixel(X,Y,WHITE);
while(X<X2)
{
if(D<=0)
{
D=D+DE;
}
else
{
D=D+DNE;
Y=Y+1;
}
X=X+1;
putpixel(X,Y,WHITE);
}
}
int main()
{
int gdriver=DETECT,gmode,X,Y,X1,Y1,X2,Y2;
initgraph(&gdriver,&gmode,"");
printf("Enter X1 : ");
scanf("%d",&X1);
printf("Enter Y1 : ");
scanf("%d",&Y1);
printf("Enter X2 : ");
scanf("%d",&X2);
printf("Enter Y2 : ");
scanf("%d",&Y2);
Draw_Line(X1,Y1,X2,Y2);
getch();
closegraph();
return 0;
}
Output
Practical 5
Objective
To Construct a circle using Bresenham's algorithm (Version-2).
Source Code
#include<conio.h>
#include<stdio.h>
#include<graphics.h>
void dcircle(int x,int y,int h,int k)
{
putpixel(x+h,y+k,1); putpixel (y+h,x+k,5); putpixel(-x+h,y+k,2); putpixel (-y+h,x+k,6);
putpixel(x+h,-y+k,3); putpixel (y+h,-x+k,7); putpixel(-x+h,-y+k,4); putpixel (-y+h,-x+k,8);
}
void main()
{ int d,de,dse,h,k,x,y,r,gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"");
printf("Enter the radius\n"); scanf("%d",&r);
printf("Enter the co ordinates of center\n"); scanf("%d%d",&h,&k);
x=0; y=r; d=1-r; de=3; dse=5-(2*r);
dcircle(x,y,h,k);
while(y>x)
{ if(d<0)
{d=d+de;
de=de+2;
dse=dse+2;}
else
{d=d+dse;
de=de+2;
dse=dse+4;
y--;
}
x++;
dcircle(x,y,h,k);
}
settextstyle(3,0,3);
outtextxy(100,300,"Program Executed By: Gaurav Soni");
getch();
closegraph();
}
Output
Practical 6
Objective
To draw an image of a hut and perform scaling and translation on it.
Source Code
//#include<conio.h>
#include<graphics.h>
#include<stdio.h>
#include<math.h>
int main()
{
int i,tx,ty;
float sx,sy;
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"");
printf("Program to implement translation and scaling on an image.\n\n");
printf("Enter Translation Factor (X & Y)\t: "); scanf("%d%d",&tx,&ty);
printf("Enter Scaling Factor (X & Y)\t\t: "); scanf("%f%f",&sx,&sy);
settextstyle(3,0,3);
outtextxy(100,420,"Program executed by Gaurav Soni");
for(i=0;i<11;i++)
{
setcolor(WHITE); // Drawing Hut
line(h[i][0],h[i][1],h[i][2],h[i][3]);
getch();
closegraph();
}
Output
Practical 7
Objective
To perform rotation of an image.
Source Code
#include<conio.h>
#include<graphics.h>
#include<stdio.h>
#include<math.h>
int main()
{
int i;float r;
settextstyle(3,0,3);
outtextxy(250,400,"Program executed by : Gaurav Soni");
r=(3.14159265/180.0)*r;
for(i=0;i<13;i++)
{
setcolor(WHITE); // Drawing Hut
line(h[0][i],h[1][i],h[2][i],h[3][i]);
// Hut Rotation
h[0][i]=h[0][i]* cos (r) -h[1][i]*sin(r) ;
h[1][i]=h[0][i]*sin(r) +h[1][i]*cos(r) ;
h[2][i]=h[2][i]* cos(r) -h[3][i]*sin(r) ;
h[3][i]=h[2][i]*sin(r) +h[3][i]*cos(r) ;
Output
Practical 8
Objective
To implement Boundary Filling algorithm.
Source Code
#include<conio.h>
#include<stdio.h>
#include<graphics.h>
}
}
bound(x+1,y,n,o); bound(x,y+1,n,o);
bound(x-1,y,n,o); bound(x,y-1,n,o);
}
}
int main()
{
int x,y,gd=DETECT,gm;
initgraph(&gd,&gm,"");
printf("Program to implement Boundary Filling algorithm.\n\n");
printf("Enter seed point [Range (150,150) -> (200,200)]\t: ");
settextstyle(3,0,3);
outtextxy(100,200,"Program executed by : Gaurav Soni");
scanf("%d %d",&x,&y);
setbkcolor(8);
setcolor(14); rectangle(150,150,200,200);
setcolor(3);line(155,155,155,175);
setcolor(2); line(175,155,175,175);
bound(x,y,1,14);
getch();
closegraph();
}
Output
Practical 9
Objective
To implement the Flood Fill algorithm.
Source Code
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int main()
{
int x,y,gd=DETECT,gmode;
initgraph(&gd,&gmode,"");
printf("Program to implement Flood Filling algorithm.\n\n");
printf("Enter seed point [Range (100,100) -> (160,160)]\t: ");
scanf("%d %d",&x,&y);
settextstyle(3,0,3);
outtextxy(100,200,"Program executed by : Gaurav Soni");
setbkcolor(8);
setcolor(RED); rectangle(100,100,160,160);
setcolor(GREEN); line(115,115,115,155);
setcolor(BLUE); line(155,115,155,155);
setcolor(YELLOW); flood(x,y,0);
getch();
closegraph();
}
Output
Practical 10
Objective
Line Clipping: Cohen Sutherland Algorithm.
Source Code
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
float i,xmax,ymax,xmin,ymin,x1,y1,x2,y2,m;
float start[4],end[4],code[4];
clrscr();
initgraph(&gd,&gm,"");
scanf("%f %f",&xmin,&ymin);
scanf("%f %f",&xmax,&ymax);
scanf("%f %f",&x1,&y1);
scanf("%f %f",&x2,&y2);
for(i=0;i <4;i++)
start[i]=0;
end[i]=0;
m=(y2-y1)/(x2-x1);
for(i=0;i <4;i++)
code[i]=start[i]&&end[i];
if((code[0]==0)&&(code[1]==0)&&(code[2]==0)&&(code[3]==0))
if((start[0]==0)&&(start[1]==0)&&(start[2]==0)&&(start[3]==0)&&(end[0]==0)&&(end[1]==0)&&(end
[2]==0)&&(end[3]==0))
cleardevice();
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);
getch();
else
cleardevice();
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);
getch();
if((start[2]==0)&&(start[3]==1))
x1=x1+(ymin-y1)/m;
y1=ymin;
if((end[2]==0)&&(end[3]==1))
x2=x2+(ymin-y2)/m;
y2=ymin;
if((start[2]==1)&&(start[3]==0))
x1=x1+(ymax-y1)/m;
y1=ymax;
if((end[2]==1)&&(end[3]==0))
x2=x2+(ymax-y2)/m;
y2=ymax;
if((start[1]==0)&&(start[0]==1))
y1=y1+m*(xmin-x1);
x1=xmin;
if((end[1]==0)&&(end[0]==1))
y2=y2+m*(xmin-x2);
x2=xmin;
if((start[1]==1)&&(start[0]==0))
y1=y1+m*(xmax-x1);
x1=xmax;
if((end[1]==1)&&(end[0]==0))
y2=y2+m*(xmax-x2);
x2=xmax;
clrscr();
cleardevice();
printf("\n\t\tAfter clippling:");
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);
getch();
else
clrscr();
cleardevice();
printf("\nLine is invisible");
rectangle(xmin,ymin,xmax,ymax);
getch();
closegraph();
Output