20th August, 2023
592

0

# Bresenham’s and Midpoint Circle Drawing Algorithm in C Graphics: Implementation and Explanation

Drawing circles is a common task in computer graphics, and Bresenham's and Midpoint circle drawing algorithms are two efficient methods to achieve this. In this blog post, we'll walk you through the implementation of both algorithms in C graphics and explain how they work. By the end of this post, you'll have a clear understanding of how to use these algorithms to draw circles in your graphics applications.

Table of Contents:

- Understanding Bresenham’s Circle Drawing Algorithm
- Exploring Midpoint Circle Drawing Algorithm
- Setting up the Environment
- Implementing Bresenham’s Algorithm for Circle Drawing
- Implementing Midpoint Algorithm for Circle Drawing
- Comparing the Algorithms
- Conclusion

1. Understanding Bresenham’s Circle Drawing Algorithm

Bresenham's Circle Drawing Algorithm is an extension of the Bresenham line algorithm. It generates points for one-eighth of the circle and uses symmetry to plot the entire circle.

2. Exploring Midpoint Circle Drawing Algorithm

The Midpoint Circle Drawing Algorithm also utilizes symmetry to draw circles efficiently. It calculates a decision parameter to determine the next point to be plotted.

3. Setting up the Environment

Before we begin, make sure you have a C graphics library installed on your system. You can use libraries like "graphics.h" for this purpose.

4. Implementing Bresenham’s Algorithm for Circle Drawing

Here's the C code to implement Bresenham’s Circle Drawing Algorithm:

#include <stdio.h>
#include <graphics.h>
void drawCircleBresenham(int xc, int yc, int r) {
int x = 0, y = r;
int d = 3 - 2 * r;
while (x <= 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);
if (d < 0)
d = d + 4 * x + 6;
else {
d = d + 4 * (x - y) + 10;
y--;
}
x++;
}
}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
int xc = 250, yc = 250, r = 100;
drawCircleBresenham(xc, yc, r);
delay(5000);
closegraph();
return 0;
}

**5. Implementing Midpoint Algorithm for Circle Drawing**

Below is the C code to implement the Midpoint Circle Drawing Algorithm:

#include <stdio.h>
#include <graphics.h>
void drawCircleMidpoint(int xc, int yc, int r) {
int x = r, y = 0;
int p = 1 - r;
while (x >= 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);
y++;
if (p <= 0)
p = p + 2 * y + 1;
else {
x--;
p = p + 2 * y - 2 * x + 1;
}
}
}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
int xc = 250, yc = 250, r = 100;
drawCircleMidpoint(xc, yc, r);
delay(5000);
closegraph();
return 0;
}

**6. Comparing the Algorithms**

Both algorithms achieve efficient circle drawing by exploiting symmetry and integer arithmetic. The Bresenham algorithm uses fewer multiplication operations compared to the Midpoint algorithm, but the Midpoint algorithm involves fewer additions and subtractions. Both are excellent choices for drawing circles in different scenarios.

7. Conclusion

In this blog post, we've explored the Bresenham and Midpoint Circle Drawing Algorithms and provided implementations for both in C graphics. These algorithms are essential tools for anyone working in graphics programming, enabling the efficient rendering of circles on digital displays. By following this guide, you can confidently incorporate these algorithms into your graphics projects to create visually appealing circular shapes.