CG Practicals
CG Practicals
-87175101
PRACTICAL FILE OF
COMPUTER GRAPHICS AND
ANIMATION
(CSE-403N)
INDEX:-
S.NO PRACTICAL DATE SIGN
1. Write a program to implement DDA line drawing
algorithm.
2. Write a program to implement Bresenham’s line
drawing algorithm.
3. Implement the Bresenham’s circle drawing
algorithm.
4. Write a program to draw a decagon whose all
vertices are connected with every other vertex
using lines.
5. Write a program to move an object using the
concepts of 2-D transformations.
6. Write a program to implement the midpoint circle
drawing algorithm using any Object Oriented
Programming Language like Python, C++,Java.
7. Implement the line clipping algorithm using any
Object Oriented Programming Language like
Python, C++, Java.
8. Implement boundary fill algorithm using any
Object Oriented Programming Language like
Python, C++, Java.
9. Draw HUT using C.
OUTPUT :- PRACTICAL NO 1
ROLL NO.-87175101
PRACTICAL NO: 1
AIM :- Write a program to implement DDA line drawing algorithm.
Algorithm :-
dx = x1-x0;
dy = y1-y0;
X = x0;
Y = y0;
putpixel ( X , Y , WHITE )
X += Xinc ;
Y += Yinc ;
}
ROLL NO.-87175101
Program Code
#include<stdio.h>
#include<graphics.h>
#include<math.h>
float round(float a);
void main()
{
int gd=DETECT,gm;
int x1,y1,x2,y2,steps,k;
float xincr,yincr,x,y,dx,dy;
printf("enter x1,y1");
scanf("%d%d",&x1,&y1);
printf("enter x2,y2");
scanf("%d%d",&x2,&y2);
initgraph(&gd,&gm,"c:\\turboc3\\BGI");
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy))
{
steps=abs(dx);
}
else
{
steps=abs(dy);
}
xincr=dx/steps;
yincr=dy/steps;
x=x1;
y=y1;
for(k=1;k<=steps;k++)
{
delay(100);
x+=xincr;
y+=yincr;
putpixel(round(x),round(y),WHITE);
}
outtextxy(200,20,"DDA");
outtextxy(x1+5,y1-5,"(x1,y1)");
outtextxy(x2+5,y2+5,"(x2,y2)");
getch();
closegraph();
}
float round(float a)
{ int b=a+0.5;
return b;
}
ROLL NO.-87175101
ROLL NO.-87175101
OUTPUT :PRACTICAL NO 2
ROLL NO.-87175101
PRACTICAL NO: 2
AIM :- Write a program to implement Bresenham’s line drawing
algorithm.
Algorithm :-
Step 1: Declare variable x1, x2, y1, y2, d, i1, i2, dx, dy
Step 2: Enter value of x1, y1, x2, y2
Where x1, y1 coordinates of starting point and x2, y2 coordinates of ending point
Step 3: Calculate dx = x2 – x1
dy = y2 – y1
i1 = 2 * dy;
i2 = 2 * (dy - dx)
d = i1 – dx
Step 4: Consider (x,y) as starting point and xendas maximum possible value of x.
If dx < 0
Then x = x2
y = y2
xend= x1
If dx > 0
Then x = x1
y = y1
xend= x2
Step 5: Generate points at (x,y) coordinates.
Step 6: Check if whole line is generated
If x >= xend
Stop
Step 7: Calculate coordinates of the next pixel
If d < 0
Then d = d + i1
If d >= 0
Then d = d + i2
ROLL NO.-87175101
Increment y = y + 1
Increment x = x + 1
Step 8: Draw a point of latest (x,y) coordinates
Step 9: Go to step 6
Step 10: End of algorithm
Program code :-
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int x,y,x1,y1,x2,y2,p,dx,dy;
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
printf("\nEnter the x-coordinate of the first point ::");
scanf("%d",&x1);
printf("\nEnter the y-coordinate of the first point ::");
scanf("%d",&y1);
printf("\nEnter the x-coordinate of the second point ::");
scanf("%d",&x2);
printf("\nEnter the y-coordinate of the second point ::");
scanf("%d",&y2);
x=x1;
y=y1;
dx=x2-x1;
dy=y2-y1;
putpixel(x,y,2);
p=(2*dy-dx);
while(x<=x2)
{
if(p<0)
{
x=x+1;
p=p+2*dy;
}
else
{
x=x+1;
ROLL NO.-87175101
y=y+1;
p=p+(2*dy)-(2*dx);
}
putpixel(x,y,7);
getch();
closegraph();
}
ROLL NO.-87175101
OUTPUT:- PRACTICAL NO 3
ROLL NO.-87175101
PRACTICAL NO: 3
AIM :- Implement Bresenham’s Circle drawing algorithm.
Algorithm :-
Step 1: Set initial values of (xc,yc) and (x,y)
Step 2: Set decision parameter d to d = 3 – ( 2 * r )
Step 3: Call drawCircle(int xc, intyc, int x, int y) function
Step 4: Repeat steps 5 to 8 until x <= y
Step 5: Increment value of x
Step 6: If d < 0, set d = d + ( 4 * x ) + 6
Step 7: Else, set d = d + 4 * ( x – y ) + 10 and decrement y by 1
Step 8: Call drawCircle(int xc, intyc, int x, int y) function
}
ROLL NO.-87175101
Program code :-
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
void main()
{
int gd=DETECT,gm;
int d,r,x,y,xc,yc;
clrscr();
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
printf("Enter Radius\n");
scanf("%d",&r);
printf("Enter Center of circle\n");
scanf("%d",&xc);
scanf("%d",&yc);
d=3-2*r;
x=0;
y=r;
while(x<=y)
{
putpixel(xc+x,yc+y,RED);
putpixel(xc-y,yc-x,RED);
putpixel(xc+y,yc-x,RED);
putpixel(xc-y,yc+x,RED);
putpixel(xc+y,yc+x,RED);
putpixel(xc-x,yc-y,RED);
putpixel(xc+x,yc-y,RED);
putpixel(xc-x,yc+y,RED);
if(d<=0)
{
d=d+4*x+6;
}
else
{
d=d+4*x-4*y+10;
y=y-1;
}
x=x+1;
}
getch();
}
ROLL NO.-87175101
OUTPUT : PRACTICAL 4
ROLL NO.-87175101
PRACTICAL NO: 4
AIM :- Write a program to draw a decagon whose all vertices are
connected with every other vertices using lines.
Theory :-
A polygon in a computer graphics system is a two-dimensional shape that is modelled and
stored within its database (memory). A decagon is a polygon with 10 sides, angles and
vertices. Here are some examples of a decagon.
line() is the only function used in the code of this program. line() is used to draw a line from a
point (x1,y1) to point (x2,y2) i.e. (x1,y1) and (x2, y2) are the end points of the line. The code
is like :
void line( int x1,int x2,int y1,int y2)
Program Code :-
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int main()
{
int gd=DETECT, gm;
clrscr();
initgraph(&gd,&gm, "C:\\TURBOC3\\BGI");
line(100, 200, 150, 250);
line(150, 250, 200, 200);
line(200, 200, 250, 250);
line(250, 250, 300, 200);
line(300, 200, 300, 400);
line(300, 400, 250, 300);
line(250, 300, 200, 400);
ROLL NO.-87175101
OUTPUT : PRACTICAL 5
TRANSLATION :-
ROTATION :-
ROLL NO.-87175101
PRACTICAL NO: 5
AIM :- Write a program to move an object using the concept of 2-d
transformations.
Theory :-
Operations:-
Here, P (x,y) is the original point and P’ (x’,y’) is the transformed point.
Program Code :-
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include<math.h>
void main()
{
int gm;
ROLL NO.-87175101
SCALING :-
ROLL NO.-87175101
int gd=DETECT;
int x1,x2,x3,y1,y2,y3,nx1,nx2,nx3,ny1,ny2,ny3,c;
int sx,sy,xt,yt,r;
float t;
initgraph(&gd,&gm,"C:\\turboc3\\bgi");
printf("\t Program for basic transactions");
printf("\n\t Enter the points of triangle");
setcolor(1);
scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
getch();
printf("\n 1.Translation\n 2.Rotation\n 3.Scalling\n 4.exit");
printf("Enter your choice:");
scanf("%d",&c);
switch(c)
{
case 1:
printf("\n Enter the translation factor");
scanf("%d%d",&xt,&yt);
nx1=x1+xt;
ny1=y1+yt;
nx2=x2+xt;
ny2=y2+yt;
nx3=x3+xt;
ny3=y3+yt;
line(nx1,ny1,nx2,ny2);
line(nx2,ny2,nx3,ny3);
ROLL NO.-87175101
line(nx3,ny3,nx1,ny1);
getch();
break;
case 2:
printf("\n Enter the angle of rotation");
scanf("%d",&r);
t=3.14*r/180;
nx1=abs(x1*cos(t)-y1*sin(t));
ny1=abs(x1*sin(t)+y1*cos(t));
nx2=abs(x2*cos(t)-y2*sin(t));
ny2=abs(x2*sin(t)+y2*cos(t));
nx3=abs(x3*cos(t)-y3*sin(t));
ny3=abs(x3*sin(t)+y3*cos(t));
line(nx1,ny1,nx2,ny2);
line(nx2,ny2,nx3,ny3);
line(nx3,ny3,nx1,ny1);
getch();
break;
case 3:
printf("\n Enter the scalling factor");
scanf("%d%d",&sx,&sy);
nx1=x1*sx;
ny1=y2*sy;
nx2=x2*sx;
ny2=y2*sy;
nx3=x3*sx;
ny3=y3*sy;
line(nx1,ny1,nx2,ny2);
ROLL NO.-87175101
line(nx2,ny2,nx3,ny3);
line(nx3,ny3,nx1,ny1);
getch();
break;
case 4:
break;
default:
printf("Enter the correct choice");
}
closegraph();
}
ROLL NO.-87175101
OUTPUT : PRACTICAL 6
ROLL NO.-87175101
PRACTICAL NO: 6
AIM :- Write a program to implement Mid Point Circle drawing
algorithm any object oriented programming language like
python,C++,JAVA.
Algorithm :-
int x=0,y=r,p=1-r;
while (x<=y)
{
set pixel(x,y);
if(p<0)
p=p+2(x-y)+5;
else
p=p+2(x-y)+5;
y--;
x++;
}
Program Code :-
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void drawCircle(int x, int y, int xc, int yc);
void main()
{
int gd = DETECT, gm;
int r, xc, yc, pk, x, y;
initgraph(&gd, &gm, "C:\\TC\\BGI");
cout<<"Enter the center co-ordinates\n";
cin>>xc>>yc;
cout<<"Enter the radius of circle\n";
cin>>r;
pk = 1 - r;
x = 0;
y = r;
while(x < y)
{
ROLL NO.-87175101
drawCircle(x,y,xc,yc);
++x;
if(pk < 0)
{
pk = pk + (2*x) + 1;
}
else
{
--y;
pk = pk + (2*x) + 1 - (2*y);
}
}
getch();
closegraph();
}
OUTPUT : PRACTICAL 7
ROLL NO.-87175101
PRACTICAL NO: 7
AIM :- write a program to implement the line clipping algorithm.
Theory:-
This algorithm uses the clipping window as shown in the following figure. The minimum
coordinate for the clipping region is (XWmin,YWmin) (XWmin,YWmin) and the maximum
coordinate for the clipping region is (XWmax,YWmax) (XWmax,YWmax).
We will use 4-bits to divide the entire region. These 4 bits represent the Top, Bottom, Right,
and Left of the region as shown in the following figure. Here, the TOP and LEFT bit is set to
1 because it is the TOP-LEFT corner.
Line can be partially inside the window- We will find intersection point and draw
only that portion of line that is inside region.
Algorithm:-
Step 1 : Assign a region code for two endpoints of given line.
Step 2 : If both endpoints have a region code 0000
then given line is completely inside.
Step 3 : Else, perform the logical AND operation for both region codes.
Step 3.1 : If the result is not 0000, then given line is completely outside.
Step 3.2 : Else line is partially inside.
Step 3.2.1 : Choose an endpoint of the line that is outside the given rectangle.
Step 3.2.2 : Find the intersection point of the rectangular boundary (based on region
code).
Step 3.2.3 : Replace endpoint with the intersection point and update the region code.
Step 3.2.4 : Repeat step 2 until we find a clipped line either trivially accepted or trivially
rejected.
Step 4 : Repeat step 1 for other line
Program Code :-
#include <iostream.h>
code |= LEFT;
code |= RIGHT;
code |= BOTTOM;
code |= TOP;
return code;
int accept = 0;
while (true)
accept = 1;
break;
break;
ROLL NO.-87175101
else
int code_out;
double x, y;
if (code1 != 0)
code_out = code1;
else
code_out = code2;
y = y_max;
y = y_min;
x = x_max;
x = x_min;
}
ROLL NO.-87175101
if (code_out == code1)
{ x1 = x;
y1 = y;
else
{ x2 = x;
y2 = y;
if (accept==1)
{ cout <<"Line accepted from " << x1 << ", " << y1 << " to "<< x2 << ", " << y2 << endl;
// Here the user can add code to display the rectangle, along with the accepted (portion of) line
else
// Driver code
int main()
cohenSutherlandClip(5, 5, 7, 7);
cohenSutherlandClip(1, 5, 4, 1);
return 0; }
ROLL NO.-87175101
OUTPUT : PRACTICAL 8
ROLL NO.-87175101
PRACTICAL NO: 8
Aim:- write a program to implement boundary filled algorithm.
Algorithm:-
void boundaryFill4(int x, int y, int fill_color,int boundary_color)
{
if(getpixel(x, y) != boundary_color &&
getpixel(x, y) != fill_color)
{
putpixel(x, y, fill_color);
boundaryFill4(x + 1, y, fill_color, boundary_color);
boundaryFill4(x, y + 1, fill_color, boundary_color);
boundaryFill4(x - 1, y, fill_color, boundary_color);
boundaryFill4(x, y - 1, fill_color, boundary_color);
}
}
Program Code :-
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<graphics.h>
{ int current;
current=getpixel(x,y);
if((current!=boundary)&&(current!=fill))
{ setcolor(fill);
putpixel(x,y,fill);
delay(5);
boundaryfill(x+1,y,fill,boundary);
boundaryfill(x-1,y,fill,boundary);
boundaryfill(x,y+1,fill,boundary);
ROLL NO.-87175101
boundaryfill(x,y-1,fill,boundary);
void main()
{ int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\Turboc3\\BGI");
setcolor(10);
rectangle(250,200,310,260);
boundaryfill(280,250,12,10);
getch();
}
ROLL NO.-87175101
OUTPUT : PRACTICAL 9
ROLL NO.-87175101
PRACTICAL NO: 9
AIM :- Draw Hut using C.
Theory:-
Setcolor():- The header file graphics.h contains setcolor() function which is used to
set the current drawing color to the new color.
Syntax :- void setcolor(int color);
rectangle():- Rectangle draws a rectangle in the current line style, thickness, and
drawing color. (left,top) is the upper left corner of the rectangle, and (right,bottom)
is its lower right corner.
Syntax:- void rectangle(int left, int top, int right, int bottom);
Program Code :-
#include<graphics.h>
#include<conio.h>
ROLL NO.-87175101
int main()
{ int gd = DETECT,gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
/* Draw Hut */
setcolor(WHITE);
rectangle(150,180,250,300);
rectangle(250,180,420,300);
rectangle(180,250,220,300);
line(200,100,150,180);
line(200,100,250,180);
line(200,100,370,100);
line(370,100,420,180);
/* Fill colours */
setfillstyle(SOLID_FILL, BROWN);
floodfill(152, 182, WHITE);
floodfill(252, 182, WHITE);
setfillstyle(SLASH_FILL, BLUE);
floodfill(182, 252, WHITE);
setfillstyle(HATCH_FILL, GREEN);
floodfill(200, 105, WHITE);
floodfill(210, 105, WHITE);
getch();
closegraph();
return 0;
}
ROLL NO.-87175101
OUTPUT : PRACTICAL 10
ROLL NO.-87175101
PRACTICAL NO: 10
Aim :- write a program to perform the polygon clipping algorithm.
Theory :-
A polygon boundary processed with a line clipper may be displayed as a series of
unconnected line segments, depending on the orientation of the polygon to the cIipping
window. What we reaIly want to display is a bounded area after clipping. For polygon
clipping, we require an algorithm that wiIl generate one or more closed areas that are then
scan converted for the appropriate area fill. The output of a polygon clipper should be a
sequence of vertices that defines the clipped polygon boundaries.
Program Code :-
#include <stdio.h>
#include <graphics.h>
#include <conio.h>
#include <math.h>
#include <process.h>
#define TRUE 1
#define FALSE 0
BOTTOM = 0x2,
RIGHT = 0x4,
LEFT = 0x8
};
ROLL NO.-87175101
ROLL NO.-87175101
float xmin,xmax,ymin,ymax;
{ outcode outcode0,outcode1,outcodeOut;
outcode0 = CompOutCode(x0,y0);
outcode1 = CompOutCode(x1,y1);
do
{ if(!(outcode0|outcode1))
{ accept = TRUE;
done = TRUE;
else
done = TRUE;
else
{ float x,y;
outcodeOut = outcode0?outcode0:outcode1;
{ x = x0+(x1-x0)*(ymax-y0)/(y1-y0);
y = ymax;
else
{ x = x0+(x1-x0)*(ymin-y0)/(y1-y0);
y = ymin;
else
{ y = y0+(y1-y0)*(xmax-x0)/(x1-x0);
x = xmax;
}
ROLL NO.-87175101
else
{ y = y0+(y1-y0)*(xmin-x0)/(x1-x0);
x = xmin;
if(outcodeOut==outcode0)
x0 = x;
y0 = y;
outcode0 = CompOutCode(x0,y0);
else
{ x1 = x;
y1 = y;
outcode1 = CompOutCode(x1,y1);
}while(done==FALSE);
if(accept)
line(x0,y0,x1,y1);
rectangle(xmin,ymin,xmax,ymax);
{ outcode code = 0;
if(y>ymax)
code|=TOP;
else
if(y<ymin)
code|=BOTTOM;
if(x>xmax)
code|=RIGHT;
ROLL NO.-87175101
else
if(x<xmin)
code|=LEFT;
return code;
void main( )
{ float x1,y1,x2,y2;
clrscr( );
scanf("%d",&n);
for(i=0;i<2*n;i++)
{ scanf("%d",&poly[i]);
poly[2*n]=poly[0];
poly[2*n+1]=poly[1];
scanf("%f%f%f%f",&xmin,&ymin,&xmax,&ymax);
drawpoly(n+1,poly);
rectangle(xmin,ymin,xmax,ymax);
getch( );
cleardevice( );
for(i=0;i<n;i++)
clip(poly[2*i],poly[(2*i)+1],poly[(2*i)+2],poly[(2*i)+3]);
getch( );
restorecrtmode( );}
ROLL NO.-87175101
#include <stdio.h>
#include <conio.h>
#include <math.h>
int dx,dy,m,s;
float xi,yi,x,y;
dx = x2 - x1;
dy = y2 - y1;
s = abs(dx);
else
s = abs(dy);
xi = dx / (float) s;
yi = dy / (float) s;
x = x1;
y = y1;
x += xi;
y += yi;
putpixel(x, y, WHITE);
}
ROLL NO.-87175101
void main()
clrscr();
drawline(150,450,450,450);
drawline(450,450,450,150);
drawline(450,150,150,150);
drawline(150,150,150,450);
getch();
}
ROLL NO.-87175101
int x,y;
x=x1;
y=y1;
if(x1==x2)
{while(y!=y2)
{ if(y2-y1>0)
++y;
else
--y;
putpixel(x,y,2);
}
}
else
{ m=(float)(y2-y1)/(x2-x1);
error=0;
putpixel(x,y,2);
while(x!=x2)
{ error+=m;
if(error>.5)
{
if(x2-x1>0)
y+=1;
else
y-=1;
--error;
}
if(x2-x1>0)
++x;
else
--x;
putpixel(x,y,2);
}} }
ROLL NO.-87175101
ROLL NO.-87175101
ROLL NO.-87175101