0% found this document useful (0 votes)
5 views6 pages

CG Ex-4

This document outlines Experiment 4 from the Computer Graphics Lab, focusing on developing programs to draw circles using the Circle Generator and Midpoint Circle algorithms. It includes the aim, objectives, algorithms, implementation code, and expected learning outcomes. The experiment aims to enhance understanding of circle drawing techniques and their performance comparison.

Uploaded by

Jain Aman
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)
5 views6 pages

CG Ex-4

This document outlines Experiment 4 from the Computer Graphics Lab, focusing on developing programs to draw circles using the Circle Generator and Midpoint Circle algorithms. It includes the aim, objectives, algorithms, implementation code, and expected learning outcomes. The experiment aims to enhance understanding of circle drawing techniques and their performance comparison.

Uploaded by

Jain Aman
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/ 6

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment 4
Student Name: Jain Aman UID:22BCS14831
Branch: CSE Section/Group:637/B
Semester: 6th Date of Performance:19/02/25
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 implement a circle drawing algorithm using a circle generator technique for
a given center and radius.
• To develop a program using the Midpoint Circle Algorithm to efficiently draw
a circle.
• To understand and compare the performance and accuracy of different circle
drawing algorithms.

3. Algorithm:
a) Using the Circle Generator Algorithm:
1. Input: Center of the circle (xc,yc) and radius r.

2. Initialize: Set angle θ=0\theta = 0θ=0.

3. Repeat until θ reaches 360°:


• Compute x=xc+rcos(θ)
• Compute y=yc+rsin(θ)
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

• Plot the pixel at (x,y).


• Increment θ by a small step (e.g., 1 degree)

4. End.

b) Using the Midpoint Circle Algorithm:


1. Input: Center (xc,yc) and radius r.

2. Initialize:
• Set initial point (x,y)=(0,r).
• Compute the decision parameter: p=1−r

3. Plot the initial 8 symmetric points around the center.

4. Repeat until x≤y


• If p<0: Move horizontally right (x+1) and update p.
• Else: Move diagonally (x+1,y−1) and update p.
• Plot all 8 symmetric points.

5. End.

4. Implementation/Code:

a) Circle Generator Algorithm:


#include <graphics.h>
#include <conio.h>
#include <math.h>
#include <dos.h>
#include <iostream.h> // Correct header for Turbo C++
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

#define PI 3.14159

void drawCircle(int xc, int yc, int r) {


for (int theta = 0; theta <= 360; theta++) {
int x = xc + r * cos(theta * PI / 180); // Convert degrees to radians
int y = yc + r * sin(theta * PI / 180);
putpixel(x, y, WHITE); // Plot the pixel
delay(5); // Small delay to visualize the drawing process
}
}

int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI"); // Initialize graphics mode

int xc, yc, r;

cout << "Enter center (xc, yc): "; // Corrected cout


cin >> xc >> yc; // Corrected cin
cout << "Enter radius: ";
cin >> r;

drawCircle(xc, yc, r);

getch(); // Wait for key press


closegraph(); // Close graphics mode
return 0;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Fig 1: Circle Generator Algorithm

b) Midpoint Circle Algorithm:

#include <graphics.h>
#include <conio.h>
#include <iostream.h>
#include <dos.h> // Required for delay()

void plotCirclePoints(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 drawCircle(int xc, int yc, int r) {


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

int x = 0, y = r;
int p = 1 - r; // Initial decision parameter

plotCirclePoints(xc, yc, x, y);

while (x < y) {
x++;
if (p < 0) {
p += 2 * x + 1;
} else {
y--;
p += 2 * (x - y) + 1;
}
plotCirclePoints(xc, yc, x, y);
delay(5); // Small delay for visualization
}
}

int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI"); // Initialize graphics mode

setbkcolor(BLACK); // Set background to black


cleardevice(); // Apply background color
setcolor(WHITE); // Set drawing color to white

int xc, yc, r;

clrscr(); // Clear screen for input visibility


cout << "Enter center (xc, yc): ";
cin >> xc >> yc;
cout << "Enter radius: ";
cin >> r;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

drawCircle(xc, yc, r);

getch(); // Wait for user input before closing


closegraph(); // Close graphics mode
return 0;
}

Fig 2: Midpoint Circle Algorithm

5. Learning Outcome:
• Understand the mathematical foundation of circle drawing algorithms.
• Learn how to efficiently plot pixels to represent a smooth circular shape.
• Compare the accuracy and computational efficiency of different algorithms.
• Gain practical experience in implementing geometric algorithms in a
programming language.
• Develop skills to optimize graphical rendering techniques for digital displays.

You might also like