Get the latest tech news

Bresenham's Circle Drawing Algorithm (2021)


Once upon a time I was given the following problem for a technical programming interview: Write a function draw_circle(r)that draws a circle with radius r. Use the given method draw_pixel(x, y)which takes a 2-dimensional point (x, y)and colors it in on the computer screen.

To fix this, I decided to port the Python code to Rust, and then target WASM so that I can use it to draw on the HTML5<canvas> elements (and to eliminate the “aspect ratio” problem). The “purist” in me felt that the decrementing of \(y\) stood out like a sore thumb, and so I created a tweaked version that moves E and NE, starting out from \((0, -r)\) instead. The technique of avoiding the full polynomial calculation behind \(F(M)\) (referred by the book as finding the first and second-order differences) took some time to get used to, but is intuitive enough in the end.

Get the Android app

Or read this on Hacker News

Read more on:

Photo of Bresenham

Bresenham