Mid point algorithm ~ How to draw a circle!
Have you ever tried to draw a circle using code? If yes, you would have tumbled across the mid point algorithm, which defines how to determine the coordinates required to draw a circle.
I have the video explaining it here:
Or you can read through for the explanation.
All you need is an octant
To draw a circle, all you need is to draw one octant (one eighth) of the circle, because a circle is symmetrical across all axis and therefore, when you have one octant in place, you just need to mirror it in 8 ways to get the full circle.
Let’s draw the octant
Let’s consider the octant starting from the positive X axis and going upwards. We know the starting point coordinates (12, 0). For this particular octant:
The “y” value is always increasing by 1
The “x” value is either the same or decreasing by 1
So it would mean that, if we know one point, there will be 2 candidates for the next point.
Mid point algorithm determines which of them is actually closer to the circle (which of them is more accurate) by using their mid point.
So now we have all the information we need to keep calculating the next points until the octant is full.
Once the octant is full, it’s a matter of mirroring the data in 8 ways to get the full circle.