COMPUTER GRAPHICS
Course Code: BTCS2401
Lab File
For
BACHELOR OF COMPUTER SCIENCE AND
ENGINEERING
Engineering & Technology
SCHOOL OF COMPUTING SCIENCE AND ENGINEERING
GALGOTIAS UNIVERSITY, GREATER NOIDA
UTTAR PRADESH
Student Name: CHAITANYA SAXENA
Admission No: 20SCSE1290081
Semester : IV / WINTER 2020/21
1|Page
EXP. NO. : 2 DATE: 28/1/2022
TITLE OF EPERIMENT: Print a circle using Midpoint circle drawing
algorithm.
AIM:- Write a program to generate a complete circle using Midpoint circle
drawing algorithm.
PROGRAM :
1. DESCRIPTION (INTRODUCTION)
The mid-point circle drawing algorithm is an algorithm used to determine
the points needed for rasterizing a circle.
We use the mid-point algorithm to calculate all the perimeter points of the
circle in the first octant and then print them along with their mirror points in
the other octants. This will work because a circle is symmetric about it’s
centre.
The algorithm is very similar to the Mid-Point Line Generation Algorithm. Here, only
the boundary condition is different.
For any given pixel (x, y), the next pixel to be plotted is either (x, y+1) or (x-1, y+1).
This can be decided by following the steps below.
2|Page
1)Find the mid-point p of the two possible pixels i.e (x-0.5, y+1)
2)If p lies inside or on the circle perimeter, we plot the pixel (x, y+1), otherwise
if it’s outside we plot the pixel (x-1, y+1)
Boundary Condition : Whether the mid-point lies inside or outside the circle
can be decided by using the formula:-
Given a circle centered at (0,0) and radius r and a point p(x,y) F(p) =
x2 + y2 – r2
if F(p)<0, the point is inside the circle F(p)=0,
the point is on the perimeter
F(p)>0, the point is outside the circle
2. SOURCE CODE –
#include<stdio.h>
#include<graphics.h>
void drawcircle(int x0, int y0, int radius)
{ int x =
radius; int y =
0; int err = 0;
while (x >= y)
{
putpixel(x0 + x, y0 + y, 7);
putpixel(x0 + y, y0 + x, 7);
putpixel(x0 - y, y0 + x, 7); putpixel(x0
- x, y0 + y, 7); putpixel(x0 - x, y0 - y,
7); putpixel(x0 - y, y0 - x, 7);
putpixel(x0 + y, y0 - x, 7); putpixel(x0
+ x, y0 - y, 7);
3|Page
if (err <= 0) { y
+= 1; err +=
2*y + 1;
}
if (err > 0) { x
-= 1; err -=
2*x + 1;
}
}
}
int main() { int gdriver=DETECT, gmode, error,
x, y, r; initgraph(&gdriver, &gmode, "c:\\
turboc3\\bgi"); r =100;
// printf("Enter radius of circle: ");
// scanf("%d", &r);
x =200; y =300;
// printf("Enter co-ordinates of center(x and y): ");
// scanf("%d%d", &x, &y); drawcircle(x,
y, r);
return 0;
}
3. OUTPUT -
4|Page
5|Page