encyclopegia.org

Midpoint circle algorithm

In computer graphics, the midpoint circle algorithm is an algorithm used to determine the points needed for rasterizing a circle. It is a generalization of Bresenham's line algorithm. The algorithm can be further generalized to conic sections.

In computer graphics, the midpoint circle algorithm is an algorithm used to determine the points needed for rasterizing a circle. It is a generalization of Bresenham's line algorithm. The algorithm can be further generalized to conic sections. ==Summary== This algorithm draws all eight octants simultaneously, starting from each cardinal direction (0°, 90°, 180°, 270°) and extends both ways to reach the nearest multiple of 45° (45°, 135°, 225°, 315°). It can determine where to stop because, when = , it has reached 45°. The reason for using these angles is shown in the above picture: As increases, it neither skips nor repeats any value until reaching 45°. So during the while loop, increments by 1 with each iteration, and decrements by 1 on occasion, never exceeding 1 in one iteration. This changes at 45° because that is the point where the tangent is = . Whereas before and after. The second part of the problem, the determinant, is far trickier. This determines when to decrement . It usually comes after drawing the pixels in each iteration, because it never goes below the radius on the first pixel. Because in a continuous function, the function for a sphere is the function for a circle with the radius dependent on (or whatever the third variable is), it stands to reason that the algorithm for a discrete (voxel) sphere would also rely on the midpoint circle algorithm. But when looking at a sphere, the integer radius of some adjacent circles is the same, but it is not expected to have the same exact circle adjacent to itself in the same hemisphere. Instead, a circle of the same radius needs a different determinant, to allow the curve to come in slightly closer to the center or extend out farther. File:Concentric circles drawn with Bresenham's circle algorithm (all black) 01.png|All circles drawn black. File:Concentric circles drawn with Bresenham's circle algorithm.png|Circles variously drawn red, black and blue, to demonstrate their concentricity. ==Algorithm== The objective of the algorithm is to approximate a circle or, more formally, the curve x^2+y^2=r^2 using pixels. For simplicity, we aim to approximate a circle of center (0,0) and integer radius r\in\mathbb N . Furthermore, we only draw to the first octant of the plane, for which we draw a curve from the point (r,0) and proceed counterclockwise until an angle of 45°; the remaining octants can be trivially filled by rotating and/or mirroring that first segment of the curve around the center. At each step, the path is extended by choosing the adjacent pixel which best satisfies x^2+y^2=r^2 . The "fast" direction in the first octant (the basis vector with the greater increase in value) is the y -direction (see differentiation of trigonometric functions). In practice, this means that the algorithm always takes a step in the positive y direction (upwards), and occasionally takes a step in the "slow" direction (the negative x direction, leftwards). If the point drawn at step n is (x_n,y_n) , we thus impose y_{n+1}=y_n+1 and have to decide whether to set x_{n+1} to x_n or x_n-1 . The choice is made by computing the distance between the decision midpoint (x_n-0.5,y_n+1) to the center of the circle, hence the name of the algorithm. With the above assumption of a circle of center (0,0) , this distance is equal to d=\sqrt{(x_n-0.5)^2+(y_n+1)^2} . If d>r the midpoint mathematically lies outside the circle, and the innermost candidate point at (x_n-1,y_n+1) should be drawn. Inversely if d , the midpoint lies inside the circle and the candidate point (x_n,y_n+1) best satisfies the circle equation. The full iteration for the first octant is as follows: \begin{align} x_0&=r\\ y_0&=0\\ d_n&=\sqrt{\left(x_n-\frac12\right)^2+(y_n+1)^2}\\ x_{n+1}&=\begin{cases}x_n&\text{if }d_n r\end{cases}\\ y_{n+1}&=y_n+1 \end{align} The iteration ends once the 45° slope line y=x is crossed, therefore as soon as the condition y>x is met. ===Variant with integer-based arithmetic=== Just as with the parent Bresenham's line algorithm, this algorithm can be optimized to use only integer-based math. We start by defining the radius error P_n as the difference between d_{n}^2 and the squared radius r^2 to avoid having to compute an expensive and non-integer square-root. This also slightly changes the condition for determining x_{n+1} : \begin{align} P_n&=d_n^2-r^2=\left(x_n-\frac12\right)^2+(y_n+1)^2-r^2\\ x_{n+1}&=\begin{cases}x_n&\text{if }P_n 0\end{cases}\end{align} To eliminate the 1/2 term, the radius error can be computed recursively: \begin{align} P_{n+1}&=\left(x_{n+1}-\frac12\right)^2+(y_{n+1}+1)^2-r^2\\ &=\left(x_{n+1}-\frac12\right)^2+(y_n+1)^2+2(y_n+1)+1-r^2\\ &=\left(x_{n+1}-\frac12\right)^2+P_n-\left(x_n-\frac12\right)^2+2(y_n+1)+1\\ &=P_n+\left(x_{n+1}^2-x_n^2\right)-(x_{n+1}-x_n)+2(y_n+1)+1 \end{align} Together with the condition on x_{n+1} , computing P_{n+1} becomes P_{n+1}=P_n+\begin{cases}2(y_n+1)+1&=2y_{n+1}+1&\text{if }P_n 0\end{cases} An initial value P_0 for (x_0,y_0)=(r,0) is found by approximation: \begin{align} P_0&=\left(r-\frac12\right)^2+(0+1)^2-r^2\\ &=r^2-r+\frac14+1-r^2\\ &=1.25-r\\ &\approx1-r \end{align} This optimized algorithm can be implemented in Python as follows: import numpy as np # only for the image array r = 67 # radius of the circle img = np.zeros([2 * r + 1]*2, dtype=int) # image of size (2r + 1) x (2r + 1) to fit the circle x, y, p = r, 0, 1 - r # initial values x0 = r, y0 = 0, p0 = 1 - r while x >= y: # while the point (x, y) is in the first octant for j, k in [(1, 1), (1, -1), (-1, 1), (-1, -1)]: # draw to all quadrants img[j * x + r, k * y + r] = 1 img[k * y + r, j * x + r] = 1 x, y = x - (1 if p > 0 else 0), y + 1 # update x and y according to the radius error p p += 1 - 2 * x + 2 * y if p > 0 else 1 + 2 * y # update the radius error p with updated x and y The comparison x >= y (is counted as a subtraction: x - y >= 0) y=y+1 [y++] t1 + y t1 - x The comparison t2 >= 0 is not counted as no real arithmetic takes place. In two's complement representation of the variables, only the sign bit has to be compared. x=x-1 [x--] Operations: 5 t1 = r / 16 x = r y = 0 Repeat Until x = 0 t1 = t2 x = x - 1 ==References== ==External links== Drawing circles - An article on drawing circles, that derives from a simple scheme to an efficient one Midpoint Circle Algorithm in several programming languages de:Rasterung von Kreisen#Midpoint-Algorithmus

References

No reference rows published for this article.

Cite this article

MLA

"Midpoint circle algorithm." encyclopegia.org, 2026, https://encyclopegia.org/wiki/midpoint-circle-algorithm. Accessed 24 November 2026.

Chicago

"Midpoint circle algorithm." encyclopegia.org. Accessed November 24, 2026. https://encyclopegia.org/wiki/midpoint-circle-algorithm.

APA

encyclopegia.org. (2026). Midpoint circle algorithm. Retrieved November 24, 2026, from https://encyclopegia.org/wiki/midpoint-circle-algorithm

Accessed November 24, 2026 · CC BY-SA 4.0 · citation page · JSON

Related articles

La vuelta de Rocha

enwiki · references available

Viejo barrio

enwiki · references available

Inchgower distillery

enwiki · references available

Lady Knight

enwiki · references available

North Wind (film)

enwiki · references available

Harry Hibbard

enwiki · references available
CC BY-SA 4.0 · share-alike · https://en.wikipedia.org/w/index.php?oldid=1360821163 · 0.5 · snapshot enwiki-001360821163

Cite · JSON · Search

Related services and records

Services

  • Estate — measured countries, lanes and facts
  • Intel — live corroborated events
  • Legal — statutes, cases and citations
  • Encyclopedia — articles with inline sources
  • Space — launches, satellites and orbits
  • Search — federated search and reports
  • Pass — passport account and SSO
  • Corpora — read-only corpus origins
  • e01 — self-hosted corpora and tools

Text and metadata only; each target carries its own sources. Missing data is published as no-data, never estimated.