DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Experiment 4
Student Name: Mohd Talib Siddiqi UID: 22BCS13509
Branch: Be-CSE Section/Group: 22BCS _IOT_620
Semester: 6th Date of Performance:19/2/25
Subject Name: Computer Graphics Subject Code: 22CSH-352
with Lab
1. Aim:
a) Develop a program to draw a circle using the circle generator algorithm for a given
center and radius.
b) Develop a program to draw a circle using the midpoint circle algorithm for a given
center and radius..
2. Objective: To develop and implement the circle generator and midpoint circle
generator algorithm to draw a circle with a given center and radius..
3. Implementation/Code:
a) Mid point circle Algorithm:
Code:
#include<iostream.h>
#include<conio.h>
# include<graphics.h>
#include<math.h>
#include<dos.h>
#define round(a) ((int)a+0.5)
void putcircle(int xc,int yc,int x,int y)
{
putpixel(xc+x,yc+y,1);
putpixel(xc-x,yc+y,2);
putpixel(xc+x,yc-y,3);
putpixel(xc-x,yc-y,4);
putpixel(xc+y,yc+x,5);
putpixel(xc-y,yc+x,6);
putpixel(xc+y,yc-x,7);
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
putpixel(xc-y,yc-x,8);
}
void circlemid(int xc,int yc,float r)
{
float x=0,y=r;
int p=1-r;
while(x<y)
{
x++;
if(p<0)
p=p+(2*x)+1;
else
{
y--;
p=p+(2*(x-y)+1);
}
putcircle(xc,yc,round(x),round );
delay(10);
}
}
void main()
{
clrscr();
int gd=DETECT,gm;
initgraph(&gd,&gm," ");
int xc,yc,r;
cout<<"Enter centre co-ordinates:";
cin>>xc>>yc;
cout<<"Enter radius:";
cin>>r;
circlemid(xc,yc,r);
setcolor(10);
circle(xc,yc,r);
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
getch();
closegraph();
}
b) Circle Generator algorithm:
Code:
#include<iostream.h>
#include<conio.h>
#include<dos.h>
#include<graphics.h>
#include<math.h>
#define round(a) ((int)(a+0.5))
void main()
{
clrscr();
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
int xc=100,yc=150,r=50;
float x=0,y=0;
for(int i=0;i<=45;i++)
{
double ang=double(i)*(3.142/180);
x = r*cos(ang);
y = r*sin(ang);
putpixel(xc+round(x),yc+roundYes,15);
putpixel(xc-round(x),yc+roundYes,15);
putpixel(xc+round(x),yc-roundYes,15);
putpixel(xc-round(x),yc-roundYes,15);
putpixel(xc+roundYes,yc+round(x),15);
putpixel(xc-roundYes,yc+round(x),15);
putpixel(xc+roundYes,yc-round(x),15);
putpixel(xc-roundYes,yc-round(x),15);
delay(100);
//METHOD2
x=0;y=r;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
xc=300,yc=150;
for(x=0;x<y;x++)
{
double temp=((r*r)-(x*x));
y=sqrt(temp);
putpixel(xc+round(x),yc+roundYes,15);
putpixel(xc-round(x),yc+roundYes,15);
putpixel(xc+round(x),yc-roundYes,15);
putpixel(xc-round(x),yc-roundYes,15);
putpixel(xc+roundYes,yc+round(x),15);
putpixel(xc-roundYes,yc+round(x),15);
putpixel(xc+roundYes,yc-round(x),15);
putpixel(xc-roundYes,yc-round(x),15);
delay(100);
}
getch();
closegraph();
}
4. Output:
Figure No. 1
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Figure No .2
5. Learning Outcome:
Learned how to implement Simple DDA, Symmetrical DDA, and Bresenham’s
Line Drawing Algorithm.
Understood the differences in computational efficiency and accuracy of these
algorithms for lines with both positive and negative slopes.
Gained insights into the practical applications of these algorithms in computer
graphics and raster displays.
Observed how integer arithmetic in Bresenham’s algorithm optimizes
performance compared to the floating-point operations in DDA.