I'm experimenting with drawing circles and have brute-forced a very simple one:
r=7 d=r*2 for y = 0 to d for x = 0 to d if (x-r)^2 + (y-r)^2 <= (r)^2 plot(x+r, y+r, c) endif next next
The principal is simple. Step through every point in a square and plot every point which lies within the area of the circle.
I end up with something like this:
I don't want the single-pixels that appear on on each side so I adjusted this line from:
if (x-r)^2 + (y-r)^2 <= (r)^2
if (x-r)^2 + (y-r)^2 < (r)^2
I end up with a circle like this:
While this IS a circle, it's not quite the circle that I want. The edges are too sharp and don't slope in a convincing way. Instead, I want to draw something little smoother like this:
But I'm not sure how to "relax" the the pixels to achieve this.
The code I posted is meant to illustrate how I'm drawing the circle-- I'm not looking for optimizations. I'm only interested in tweaking it to manipulate the smoothness of the circle being drawn.
This is more than an adjustment for the last pixel along each side. I want something that scales with the size of the circle.
I experimented with the code provided by @user1118321 an found that I could manipulate which pixels were drawn along the edge by considering the distance of the point being plotted from the center of the circle.
This allows me to apply a threshold on the points at the very edge of the circle and to decide which ones I want to skip over.
The results vary depending on a few different factors and would benefit from fine-tuning with a table for an optimal threshold for very small circles to taste.
Here is one solution which provided me with the circle I was looking for:
for y = 0 to r*2 for x = 0 to r*2 deltaX = r - x deltaY = r - y distance = sqr(deltaX^2 + deltaY^2) // Point lies outside of the circle if distance-radius > 1 continue endif // Edge threshold if radius/distance < 0.9 continue endif plot(x, y) next next
Here are some examples of various circles I was able to create: