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

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

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

    https://beginners.re/

  • You can always try Bresenham’s circle drawing algorithm.

  • 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.

  • > 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.

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

  • undefined