Drawing a circle, point-by-point, without floating point support

by threeme3on 3/24/2022, 7:02 AMwith 39 comments

by userbinatoron 3/28/2022, 4:07 AM

There's an even simpler algorithm if you're fine with "close enough" circles: https://news.ycombinator.com/item?id=15266331

by watersbon 3/28/2022, 4:11 AM

I love Yurichev's books on assembly language, and he gives them away (CC-BY-4.0).

https://beginners.re/

by christkvon 3/28/2022, 3:16 AM

You can always try Bresenham’s circle drawing algorithm.

by rep_lodsbon 3/28/2022, 3:08 PM

With the error initialized to zero, this algorithm will always step immediately after drawing the first pixel, which will cause that pixel to "stick out".

  y += 1;          // y == 1
  err += 2*y + 1;  // err == 3
  x -= 1;          // x == radius-1
  err -= 2*x + 1;  // err == 2-(2*(radius-1))
You could compare the absolute value of the new and old error, or start with err = -(radius-1) instead.

And you don't need calculus to come up with the algorithm, just simple high school algebra: (x+1)² - x² = 2x + 1.

by dahfizzon 3/28/2022, 2:53 AM

> It requires only additions, subtractions and bit shifts: 2x is the same as x<<1, of course. It also requires only integer arithmetic.

Does any of this mean anything in JS, where AFAIK there are no real ints? 2x is an fpu operation under the hood, and not a bit shift.

by dark-staron 3/28/2022, 11:46 AM

This is how we did it on the C64, back in the 80's :-D