I need to implement the Midpoint Circle Algorithm in matplotlib
, so that a circle is rasterized on a square grid of 200x200
cells. Please also refer to my other question on the subject.
Thanks to this answer, I was able to find some code which I assume works flawlessly. My only problem is that I wouldn't know how to integrate it and amend it to make sure matplotlib
draws a filled circle with 1
inside and 0
outside.
This is how I implement the script with matplotlib
:
import numpy
import matplotlib.pyplot as plt
n=200 #Grid size, 4 times my visualized output in order to be able to truncate some circles
empty_lattice=numpy.zeros((n,n)) #The empty 2D grid
radius=int(numpy.random.uniform(30,90)) #Radius
xc=int(numpy.random.uniform(0,n-radius)) #X center
yc=int(numpy.random.uniform(0,n-radius)) #Y center
x=0
y=radius
d=3-2*radius
while (x<=y):
for hor in range(0,x): #This loop is my unfortunate attempt to fill the circle with 1s
for ver in range(0,y):
empty_lattice[xc+x][yc+y]=1 #1st octant
empty_lattice[xc-x][yc+y]=1 #2nd octant
empty_lattice[xc+x][yc-y]=1 #3rd octant
empty_lattice[xc-x][yc-y]=1 #4th octant
empty_lattice[xc+y][yc+x]=1 #5th octant
empty_lattice[xc-y][yc+x]=1 #6th octant
empty_lattice[xc+y][yc-x]=1 #7th octant
empty_lattice[xc-y][yc-x]=1 #8th octant
if (d<0):
d=d+4*x+6
else:
d=d+4*(x-y)+10
y=y-1
x=x+1
Now, this is what I obtain, but you see that my circle is empty and that there is a "gap" in the horizontal line at the bottom of it. This gap occurs at each octant intersection. How can I amend my code to fill the circle and the gap?
EDIT
After having adopted the answer provided below, I have realized that two mirrored circles are drawn in the image below. I think this bug came with my original script and not with the answer. I only want one circle in each image. How can I get rid of this feature?