Midpoint Circle Algorithm Using C Programming
🎯 Introduction
The "Midpoint Circle Algorithm" is a classic computer graphics algorithm used to draw circles on a pixel grid. It is an efficient and elegant way to generate the points lying on the circumference of a circle centered at (x, y) with a given radius 'r'. This algorithm is particularly useful in graphics applications where direct access to pixels is available.
In this blog post, we will explore the Midpoint Circle Algorithm implemented using the C programming language. We'll provide a detailed explanation of the concept and the step-by-step execution of the algorithm. Additionally, we'll optimize and update the code to fit modern standards and remove outdated dependencies.
🎯 Concept and Explanation
The Midpoint Circle Algorithm works by plotting only a limited set of points lying on the circumference of the circle rather than plotting all points. The algorithm takes advantage of the symmetry of circles to determine the next points to plot, thus reducing the computational effort required.
Step-by-step execution of the Midpoint Circle Algorithm:
The algorithm takes the center coordinates (x, y) and the radius 'r' as input.
It initializes the starting point (0, r) and computes the initial decision parameter 'p'.
The algorithm plots the points in the first octant and then uses symmetry to plot the points in the other octants.
The decision parameter 'p' determines whether the next point lies inside or outside the circle and guides the algorithm to decide which pixel to plot.
The algorithm continues plotting until it reaches the eighth octant and the circle is complete.
🎯 Midpoint Circle Algorithm Code
Below is the Midpoint Circle Algorithm using the C programming language:
#include <stdio.h>
#include <graphics.h>
void drawCircle(int x1, int y1, int r);
int main()
{
int gd = DETECT, gm;
int x, y, r;
printf("Enter the Midpoint and Radius: ");
scanf("%d%d%d", &x, &y, &r);
initgraph(&gd, &gm, "");
drawCircle(x, y, r);
getch();
closegraph();
return 0;
}
void drawCircle(int x1, int y1, int r)
{
int x = 0, y = r;
int p = 1 - r;
void plotPoints(int, int, int, int);
plotPoints(x1, y1, x, y);
while (x < y)
{
x++;
if (p < 0)
p += 2 * x + 1;
else
{
y--;
p += 2 * (x - y) + 1;
}
plotPoints(x1, y1, x, y);
}
}
void plotPoints(int xctr, int yctr, int x, int y)
{
putpixel(xctr + x, yctr + y, 1);
putpixel(xctr - x, yctr + y, 1);
putpixel(xctr + x, yctr - y, 1);
putpixel(xctr - x, yctr - y, 1);
putpixel(xctr + y, yctr + x, 1);
putpixel(xctr - y, yctr + x, 1);
putpixel(xctr + y, yctr - x, 1);
putpixel(xctr - y, yctr - x, 1);
}
🎯 Explanation of the Source Code
We have removed the <conio.h> header as it's outdated and not required for modern compilers.
We now use a separate function drawCircle() to encapsulate the drawing logic, which makes the code more organized and easier to understand.
The plotPoints() function is created to handle the pixel plotting, which simplifies the main circle drawing loop and enhances code readability.
🎯 Summary
In this blog post, we learned about the "Midpoint Circle Algorithm" and its implementation using the C programming language. The algorithm efficiently draws circles by utilizing the concept of symmetry and plotting only specific points on the circumference. We explored the step-by-step execution of the algorithm and provided an updated and optimized C code for drawing circles. This algorithm is valuable in computer graphics and various graphical applications due to its simplicity and efficiency.
🎯 Key Points
The Midpoint Circle Algorithm draws circles by plotting a limited set of points based on symmetry.
It reduces computational effort and improves efficiency compared to traditional methods.
The algorithm involves calculating a decision parameter to decide the next point to plot.
The C code for the Midpoint Circle Algorithm is updated and optimized for modern standards.
The code is organized into functions for better readability and maintainability.
Computer Graphics Programs
2D Computer Graphics Programs
3D Computer Graphics Programs