What is the Midpoint Circle Algorithm?
The Midpoint Circle Algorithm is a raster graphics algorithm used to draw a circle on a pixel screen. Instead of checking every point on the screen, it calculates the best next pixel around the circle boundary.
The algorithm starts at the top of the circle, moves through one octant, and uses symmetry to plot the remaining seven matching points. This makes circle drawing much faster than testing all possible coordinates.
Eight-way symmetry of a circle
A circle is symmetric in eight directions. If one point (x, y) lies on the circle relative to the center, then seven more points can be plotted without recalculating them.
(xc + x, yc - y) (xc - x, yc - y)
(xc + y, yc + x) (xc - y, yc + x)
(xc + y, yc - x) (xc - y, yc - x)
Decision parameter formula
For a circle with radius r, the algorithm starts with:
y = r
p = 1 - r
If p < 0, the midpoint is inside the circle, so x increases and y stays the same. If p >= 0, the midpoint is outside or on the circle, so x increases and y decreases.
Steps of Midpoint Circle Algorithm
- Read the circle center
(xc, yc)and radiusr. - Initialize
x = 0,y = r, andp = 1 - r. - Plot the eight symmetric points.
- If
p < 0, updatep = p + 2x + 3. - If
p >= 0, decrease y and updatep = p + 2(x - y) + 5. - Increase x and repeat until
x > y.
Example calculation
Suppose the radius is 5. The initial values are:
y = 5
p = 1 - 5 = -4
| Step | x | y | p | Decision |
|---|---|---|---|---|
| 0 | 0 | 5 | -4 | Inside, y unchanged |
| 1 | 1 | 5 | -1 | Inside, y unchanged |
| 2 | 2 | 5 | 4 | Outside, decrease y |
| 3 | 3 | 4 | 3 | Outside, decrease y |
Midpoint Circle Algorithm program in C using graphics.h
This program draws a circle using the midpoint method. It does not call the built-in circle() function; instead, it plots pixels manually with putpixel().
#include <graphics.h>
#include <conio.h>
#include <dos.h>
#include <stdio.h>
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 drawMidpointCircle(int xc, int yc, int radius)
{
int x = 0;
int y = radius;
int p = 1 - radius;
plotCirclePoints(xc, yc, x, y);
while (x < y)
{
x++;
if (p < 0)
{
p = p + (2 * x) + 3;
}
else
{
y--;
p = p + (2 * (x - y)) + 5;
}
plotCirclePoints(xc, yc, x, y);
delay(40);
}
}
int main()
{
int gd = DETECT, gm;
int xc, yc, radius;
printf("Enter center x y: ");
scanf("%d %d", &xc, &yc);
printf("Enter radius: ");
scanf("%d", &radius);
initgraph(&gd, &gm, "");
drawMidpointCircle(xc, yc, radius);
getch();
closegraph();
return 0;
}
Try it online: open the graphics.h online compiler, paste the program, then use center 320 240 and radius 100.
Advantages of Midpoint Circle Algorithm
- It uses integer arithmetic, so it is efficient.
- It plots only one octant and mirrors the remaining points.
- It avoids expensive trigonometric functions.
- It is easy to adapt for classic graphics.h programs.
Limitations
- The circle boundary can look jagged because pixels are square.
- The algorithm draws only the outline, not a filled circle.
- Anti-aliasing is not included in the classic midpoint approach.
Common mistakes
Plotting only one point
If you plot only (x, y), you will draw just one octant. Use eight-way symmetry to complete the circle.
Calling circle() instead of putpixel()
The goal is to implement the algorithm manually. Calling circle(xc, yc, radius) uses the built-in graphics.h function and skips the midpoint logic.
Using the wrong loop condition
The loop should continue while x < y or x <= y. Stopping too early leaves gaps near the diagonal of the circle.
Summary
The Midpoint Circle Algorithm draws a circle efficiently by using a decision parameter and eight-way symmetry. It is one of the most important beginner algorithms for understanding how curved shapes are rasterized on a pixel screen.