I want to draw circles that will not leave any coordinates not included in any circle. The problem is that currently, when you plot all circles for e.g. radii 1 to 4 that some coordinates are not included in any circle, for example (2,2). current circle with skipped coordinates
Missing coordinates can be included in either the larger or smaller circle.
This is my current code
import matplotlib.pyplot as plt
def bresenham_circle(x0, y0, radius):
x = radius
y = 0
err = 0
points = []
while x >= y:
points.append((x0 + x, y0 + y))
points.append((x0 + y, y0 + x))
points.append((x0 - y, y0 + x))
points.append((x0 - x, y0 + y))
points.append((x0 - x, y0 - y))
points.append((x0 - y, y0 - x))
points.append((x0 + y, y0 - x))
points.append((x0 + x, y0 - y))
y += 1
err += 1 + 2*y
if 2*(err-x) + 1 > 0:
x -= 1
err += 1 - 2*x
return list(set(points))
coords_included_in_circles = []
for i in (1,2,3,4):
coords_included_in_circles += bresenham_circle(0, 0, i)
# Scatter plot to visualize gaps
plt.scatter([x for x, y in coords_included_in_circles], [y for x, y in coords_included_in_circles])
plt.show()
The missing coordinates seem to follow a pattern but I am unable to deduce what pattern this is. I think it should also be possible to compare the circle of the current radius to the circle of radius - 1 and deduce what coordinates would be skipped and add these to the current circle, but wasn't successful with my limited skills.
You can achieve that if you are willing to only update either the
xcoordinate or theycoordinate, but not both in the same iteration. This is easy to accomplish, by just putting theyupdate in anelseblock.Secondly, I think you have applied the update to
errat the wrong moment. It should use thexvalue (yvalue) before it is updated. This will produce a bit more "accurate" circles.Finally, with this altered method, the
ifcondition should compare the difference between options that are now different, so the formula for comparison changes a bit.So replace this piece of code:
with this:
Now you will have "full coverage" if you increase the radius by 1 in each step.
Demo
Just to demo it with a runnable snippet, here is the same function in JavaScript. It draws the cells in an HTML table and pauses briefly between each (larger) circle: