One of the most bind-blowing algorithms I learned is Bresenham's...

@tagir_valeev
Tagir Valeev@tagir_valeev
21 views May 08, 2024 ~3 min read
Advertisement
1
One of the most bind-blowing algorithms I learned is Bresenham's circle algorithm. It's useful to draw a circle raster image. The classic algorithm produces an 8-connected 1-pixel wide circle. The mind-blowing part is that you need only addition and bit shift operations.
Media image
2
When speaking about circles, the equation x² + y² = r² comes into mind. So, for a given x, we have to set pixels at y = ±√(r² - x²). It looks like, we need quite a complex math, including multiplication and square root calculation.
3
Also, for some x coordinates, we need to set more than two pixels. For example, here, four pixels are set for the same x coordinate. If we don't do this, our circle will have holes. All of this looks rather non-trivial at the first glance.
Media image
4
The first observation that simplifies things is that the drawing has 8-symmetry. We only need to compute pixel positions inside the yellow sector. Then we can copy every point changing the sign and swapping x and y coordinates.
Media image
5
Another cool thing is that inside this yellow sector the y value always changes slower than the x. So we can set exactly one pixel for every x coordinate, and we won't have any holes. We should start with x = 0, y = r, and finish when x = y. So what's between?
Media image
6
We must set pixels that are the closest to the actual circle of radius r, no more than half-a-pixel away from it. To simplify the calculations, let's increase the radius by half-a-pixel. In this case, we must set the closest pixel which is inside the circle: x² + y² < (r+0.5)².
7
Let's eliminate parentheses: x² + y² < r² + r + 0.25. Assuming that x, y, and r are integers, we may skip tracking 0.25, as it's the only fractional value here. So we have x² + y² ≤ r² + r, or x² + y² + e = r² + r, where e ≥ 0 is an error amount; e = r² + r - x² - y².
8
As we start with x = 0, y = r, the initial value of e is r² + r - 0² - r² = r. Now, at every step we increment x. This replaces x² with (x+1)² = x²+2x+1, so the e value is decreased by 2x+1. Eventually, e will become negative. This means that we should decrement y.
9
Once we decrement y, y² will be replaced with (y-1)² = y²-2y+1, and the e value will be increased by 2y-1 becoming positive again. That's basically all. We continue this procedure until the end of the sector is reached (x = y).
10
Here's a very simple Java implementation, without any graphics engine. We just print ** to console for every pixel. gist.github.com/amaembo/aabca8…
Media image
11
As you can see, the algorithm uses only addition and multiplication by 2 (which is a bit shift), so it was easy to implement it even on simple microcontrollers. It looks like magic that a combination of addition, subtraction, and doubling produces a real circle✨
Media image
Actions
What You Can Do
  • Export as PDF or Markdown
  • Batch Export to Notion
  • Bookmark & Highlight
  • Screenshot Tweet
Create Free Account

Includes 7-day Premium trial

Advertisement