0% found this document useful (0 votes)
22 views5 pages

CG Exp-4

The document outlines an experiment in a Computer Graphics Lab aimed at developing programs to draw circles using two algorithms: the Circle Generator Algorithm and the Midpoint Circle Algorithm. It includes the algorithms, implementation code in C++, and expected outputs from both methods. Additionally, it highlights learning outcomes such as using the graphics.h library and understanding coordinate systems.

Uploaded by

Pradeep Sahu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views5 pages

CG Exp-4

The document outlines an experiment in a Computer Graphics Lab aimed at developing programs to draw circles using two algorithms: the Circle Generator Algorithm and the Midpoint Circle Algorithm. It includes the algorithms, implementation code in C++, and expected outputs from both methods. Additionally, it highlights learning outcomes such as using the graphics.h library and understanding coordinate systems.

Uploaded by

Pradeep Sahu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment 4
Student Name: MAX UID:
Branch: CSE Section/Group:
Semester: 6th Date of Performance:
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.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
• Increment x.
• 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;
DEPARTMENT 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;
}
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
}
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:

Fig 1: Circle Using Circle generator


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Fig 2: Circle using Mid-Point Algorithm

6. Learning Outcome:
• 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.

You might also like