The Midpoint Circle Algorithm is a computer graphics algorithm used to draw a circle on a raster display by determining the pixels that best approximate a circle with a given center (h, k)
and radius r
. It is an efficient algorithm that uses the midpoint between pixels to decide which pixels to illuminate, minimizing computational complexity. The algorithm leverages the symmetry of a circle to reduce calculations, plotting points in one octant and mirroring them to the other seven.
(h, k)
and radius r
.x2 + y2 = r2
.x
increases and y
decreases) allows mirroring to the other seven octants.(h, k)
and radius r
.(x, y) = (0, r)
, relative to the center.p = 1 - r
(derived from the circle equation).x < y
):(x + h, y + k)
and use 8-way symmetry to plot points in other octants:
(x + h, y + k)
, (-x + h, y + k)
, (x + h, -y + k)
, (-x + h, -y + k)
(y + h, x + k)
, (-y + h, x + k)
, (y + h, -x + k)
, (-y + h, -x + k)
p
:
p < 0
: Choose the east pixel (x + 1, y)
, update p = p + 2x + 3
.p ≥ 0
: Choose the southeast pixel (x + 1, y - 1)
, update p = p + 2x - 2y + 5
.x
. If p ≥ 0
, decrement y
.x ≥ y
, as the algorithm has covered the first octant.Draw a circle with center (0, 0)
and radius r = 5
:
(x, y) = (0, 5)
.p = 1 - r = 1 - 5 = -4
.(x, y) = (0, 5)
, p = -4
.
p < 0
, choose east pixel: (x + 1, y) = (1, 5)
.p = -4 + 2 ⋅ 0 + 3 = -1
.(1, 5)
, (-1, 5)
, (1, -5)
, (-1, -5)
, (5, 1)
, (-5, 1)
, (5, -1)
, (-5, -1)
.(x, y) = (1, 5)
, p = -1
.
p < 0
, choose east pixel: (2, 5)
.p = -1 + 2 ⋅ 1 + 3 = 4
.(2, 5)
, (-2, 5)
, (2, -5)
, (-2, -5)
, (5, 2)
, (-5, 2)
, (5, -2)
, (-5, -2)
.(x, y) = (2, 5)
, p = 4
.
p ≥ 0
, choose southeast pixel: (3, 4)
.p = 4 + 2 ⋅ 2 - 2 ⋅ 5 + 5 = 4 + 4 - 10 + 5 = 3
.(3, 4)
, (-3, 4)
, (3, -4)
, (-3, -4)
, (4, 3)
, (-4, 3)
, (4, -3)
, (-4, -3)
.(x, y) = (3, 4)
, p = 3
.
p ≥ 0
, choose southeast pixel: (4, 3)
.p = 3 + 2 ⋅ 3 - 2 ⋅ 4 + 5 = 3 + 6 - 8 + 5 = 6
.(4, 3)
, (-4, 3)
, (4, -3)
, (-4, -3)
, (3, 4)
, (-3, 4)
, (3, -4)
, (-3, -4)
.(x, y) = (4, 3)
, x ≥ y
, stop.x2 + y2 = r2
) is computationally expensive due to square root calculations, while the Midpoint algorithm is more efficient.void MidpointCircle(int h, int k, int r) {
int x = 0, y = r;
int p = 1 - r;
while (x <= y) {
plot(h + x, k + y); plot(h - x, k + y); plot(h + x, k - y); plot(h - x, k - y);
plot(h + y, k + x); plot(h - y, k + x); plot(h + y, k - x); plot(h - y, k - x);
x++;
if (p < 0) {
p = p + 2 * x + 3;
} else {
y--;
p = p + 2 * x - 2 * y + 5;
}
}
}
The Midpoint Circle Algorithm is an efficient and widely used method for drawing circles on raster displays. Its use of integer arithmetic and symmetry makes it computationally efficient and accurate, making it ideal for computer graphics applications. Despite its limitation to circles, it remains a cornerstone algorithm in raster graphics.