PARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Experiment 4
Student Name: Harsh Vardhan Singh UID: 22BCS15981
Branch: CSE Section/Group: 901 A TPP
th
Semester: 6 Date: 04/02/2025
Subject Name: Computer Graphics Lab Subject Code: 22CSH-352
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. Algorithm:
a) Algorithm for Circle Generation:
• Start
• Input the center (xc, yc) and radius r.
• Loop x from -r to r and compute y as: y=yc±r2−(x−xc)2y = yc \pm \sqrt{r^2
- (x - xc)^2}y=yc±r2−(x−xc)2
• Plot the points (x, y).
• End
b) Midpoint Circle Drawing Algorithm:
• Start
• Input center (xc, yc) and radius r.
• Set x = 0, y = r, and decision parameter p = 1 - r.
• Repeat while x ≤ y:
• Plot the points using symmetry.
• If p < 0, update p = p + 2x + 1.
• Else, update p = p + 2x - 2y + 1 and decrement y.
• Increment x.
PARTMENT OF
COMPUTER SCIENCE & ENGINEERING
• End
4. Implementation/Code:
• Circle using the Circle Generator
#include <graphics.h>
#include <cmath>
void drawCircle(int xc, int yc, int r) {
for (int x = -r; x <= r; x++) { int
y = round(sqrt(r * r - x * x));
putpixel(xc + x, yc + y, WHITE);
putpixel(xc + x, yc - y, WHITE);
}
}
int main() { int gd =
DETECT, gm;
initgraph(&gd, &gm, "");
setbkcolor(GREEN); cleardevice(); int xc =
270, yc = 240, r = 100; // Center and radius
drawCircle(xc, yc, r); outtextxy(150,380,"Circle
Using Circle Generator");
getch();
closegraph();
return 0;
}
PARTMENT OF
COMPUTER SCIENCE & ENGINEERING
• Circle using mid-point algorithm:
#include <graphics.h> #include <iostream> void
drawSymmetricPoints(int xc, int yc, int x, int y) {
putpixel(xc + x, yc + y, WHITE); putpixel(xc - x,
yc + y, WHITE); putpixel(xc + x, yc - y, WHITE);
putpixel(xc - x, yc - y, WHITE); putpixel(xc + y, yc
+ x, WHITE); putpixel(xc - y, yc + x, WHITE);
putpixel(xc + y, yc - x, WHITE); putpixel(xc - y, yc
- x, WHITE);
}
void midpointCircle(int xc, int yc, int r)
{ int x = 0, y = r; int p = 1 - r;
while (x <= y) {
drawSymmetricPoints(xc, yc, x, y);
x++; if (p < 0) p += 2 * x +
1; else { y--; p += 2
* x - 2 * y + 1;
}
}
} int main() { int gd = DETECT, gm; initgraph(&gd,
&gm, ""); setbkcolor(GREEN); cleardevice(); int
xc = 260, yc = 240, r = 100; midpointCircle(xc, yc, r);
outtextxy(150,380,"Circle Using Mid-Point Algorithm");
delay(1000); getch(); closegraph(); return 0;
}
5. Output:
PARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Fig 1: Circle Using Circle generator
Fig 2: Circle using Mid-Point Algorithm
6. Learning Outcome:
PARTMENT OF
COMPUTER SCIENCE & ENGINEERING
• Learn how to use the graphics.h library for drawing basic shapes and setting up
a graphical environment in Dev-C++.
• Gain hands-on experience with two different methods: the Circle Generator
Algorithm (direct computation) and the Midpoint Circle Algorithm.
• Understood the concept of coordinating system.
• Learn about various command that is used in drawing a circle.