ArticleslgStudy

computer science

Midpoint circle algorithm

Midpoint circle algorithm is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Midpoint circle algorithm rather than just read about it. In short: 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.

Midpoint circle algorithm — main illustration
Midpoint circle algorithm — illustration

Key takeaways

  • Midpoint circle algorithm belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Midpoint circle algorithm to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Midpoint circle algorithm from memory before moving on to harder problems.

Reference excerpt

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 y = x, it has reached 45°. The reason for using these angles is shown in the above picture: As x increases, it neither skips nor repeats any x value until reaching 45°. So during the while loop, x increments by 1 with each iteration, and y decrements by 1 on occasion, never exceeding 1 in one iteration. This changes at 45° because that is the point where the tangent is rise=run. Whereas rise>run before and rise<run after. The second part of the problem, the determinant, is far trickier. This determines when to decrement y. 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 z (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.

Algorithm The objective of the algorithm is to approximate a circle or, more formally, the curve x2+y2=r2 using pixels. For simplicity, we aim to approximate a circle of center (0,0) and integer radius r∈ℕ. 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 x2+y2=r2. 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 (xn,yn), we thus impose yn+1=yn+1 and have to decide whether to set xn+1 to xn or xn−1. The choice is made by computing the distance between the decision midpoint (xn−0.5,yn+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=(xn−0.5)2+(yn+1)2. If d>r the midpoint mathematically lies outside the circle, and the innermost candidate point at (xn−1,yn+1) should be drawn. Inversely if d<r, the midpoint lies inside the circle and the candidate point (xn,yn+1) best satisfies the circle equation. The full iteration for the first octant is as follows:

x0=ry0=0dn=(xn−12)2+(yn+1)2xn+1={xnif dn<rxn−1if dn>ryn+1=yn+1 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 Pn as the difference between dn2 and the squared radius r2 to avoid having to compute an expensive and non-integer square-root. This also slightly changes the condition for determining xn+1:

Pn=dn2−r2=(xn−12)2+(yn+1)2−r2xn+1={xnif Pn<0xn−1if Pn>0 To eliminate the 1/2 term, the radius error can be computed recursively:

Pn+1=(xn+1−12)2+(yn+1+1)2−r2=(xn+1−12)2+(yn+1)2+2(yn+1)+1−r2=(xn+1−12)2+Pn−(xn−12)2+2(yn+1)+1=Pn+(xn+12−xn2)−(xn+1−xn)+2(yn+1)+1 Together with the condition on xn+1, computing Pn+1 becomes

Pn+1=Pn+{2(yn+1)+1=2yn+1+1if Pn<02(yn+1)−2(xn−1)+1=2yn+1−2xn+1+1if Pn>0 An initial value P0 for (x0,y0)=(r,0) is found by approximation:

P0=(r−12)2+(0+1)2−r2=r2−r+14+1−r2=1.25−r≈1−r This optimized algorithm can be implemented in Python as follows:

Jesko's method An improved midpoint circle algorithm only requires 5 arithmetic operations per step (for 8 pixels) and is thus best suitable for low-performance systems. The counted operations in the main loop are:

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 < y Pixel (x, y) and all symmetric pixels are colored (8 times) y = y + 1 t1 = t1 + y t2 = t1 - x If t2 >= 0 t1 = t2 x = x - 1

Drawing incomplete octants The implementations above always draw only complete octants or circles. To draw only a certain arc from an angle α to an angle β, the algorithm needs first to calculate the x and y coordinates of these end points, where it is necessary to resort to trigonometric or square root computations (see methods of computing square roots). Then the Bresenham algorithm is run over the complete octant or circle and sets the pixels only if they fall into the wanted interval. After finishing this arc, the algorithm can be ended prematurely. If the angles are given as slopes, then no trigonometry or square roots are necessary: simply check that y/x is between the desired slopes.

Generalizations It is also possible to use the same concept to rasterize a parabola, ellipse, or any other two-dimensional curve.

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

Illustrations

Midpoint circle algorithm: Rasterizing a circle of radius 23 with the Bresenham midpoint circle algorithm. Only the green   octant is actually calculated, it is simply mirrored seven times to form the other seven octants.
Rasterizing a circle of radius 23 with the Bresenham midpoint circle algorithm. Only the green   octant is actually calculated, it is simply mirrored seven times to form the other seven octants.
Midpoint circle algorithm: A circle of radius 23 drawn by the Bresenham algorithm
A circle of radius 23 drawn by the Bresenham algorithm
Midpoint circle algorithm illustration
Midpoint circle algorithm illustration

Worked examples

Example 1 — a first encounter with Midpoint circle algorithm

Start with the simplest possible case. Write down what Midpoint circle algorithm claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Midpoint circle algorithm before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Midpoint circle algorithm ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Midpoint circle algorithm

In research
Midpoint circle algorithm appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Midpoint circle algorithm in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Midpoint circle algorithm is common in secondary-school and first-year university syllabi. It links to neighbouring topics Digital geometry, Geometric algorithms, so understanding it makes those chapters shorter.
In everyday life
Look for Midpoint circle algorithm outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Midpoint circle algorithm” →

How to study Midpoint circle algorithm in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Midpoint circle algorithm means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Midpoint circle algorithm out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Midpoint circle algorithm in simple terms?

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.

Why does Midpoint circle algorithm matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Midpoint circle algorithm?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Midpoint circle algorithm.

Tags

  • Digital geometry
  • Geometric algorithms

Keep exploring