如何"translate"将中点圆算法导入matplotlib?
How to "translate" the Midpoint Circle Algorithm into matplotlib?
我需要实施关于该主题的 Midpoint Circle Algorithm in matplotlib
, so that a circle is rasterized on a square grid of 200x200
cells. Please also refer to my 。
感谢 ,我能够找到一些我认为可以完美运行的代码。我唯一的问题是我不知道如何整合它并修改它以确保 matplotlib
绘制一个 实心圆 内部 1
和 0
外面.
这就是我用 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
现在,这就是我得到的,但是你看到我的圆圈是空的,并且在它底部的横线上有一个"gap"。该间隙出现在每个八分圆交点处。 如何修改我的代码以填补圆圈和空白?
编辑
采用后,我发现下图中画了两个镜像圆。我认为这个错误来自我的原始脚本而不是答案。我只想要每张图片中的一个圆圈。 我怎样才能摆脱这个功能?
要消除差距,您需要将水平范围扩大一格。在您的代码中,那将是行 for hor in range(0,x + 1):
。我的代码采用了不同的策略。
据我了解,中点圆算法只是绕着圆的周长走。所以我认为它不能轻易修改以填充内部空间。下面的代码检查第一个八分圆中的每个点,看看该点到中心的距离是否小于或等于半径。如果在半径内,则每个八分圆内的8个对应点全部填满。
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
r2 = radius ** 2
for dx in range(0, radius):
dx2 = dx ** 2
for dy in range(0, dx + 1):
dy2 = dy ** 2
if (dx2 + dy2 <= r2):
empty_lattice[xc+dx][yc+dy]=1 #1st octant
empty_lattice[xc-dx][yc+dy]=1 #2nd octant
empty_lattice[xc+dx][yc-dy]=1 #3rd octant
empty_lattice[xc-dx][yc-dy]=1 #4th octant
empty_lattice[xc+dy][yc+dx]=1 #5th octant
empty_lattice[xc-dy][yc+dx]=1 #6th octant
empty_lattice[xc+dy][yc-dx]=1 #7th octant
empty_lattice[xc-dy][yc-dx]=1 #8th octant
plt.imshow(empty_lattice)
plt.show()
如果你真的想坚持使用中点圆算法,你可以画出周长,然后从中心点开始flood fill。
编辑 1:
去掉了两行不必要的内容。
编辑 2:
以上代码以模块化方式将圆环绕图像边缘。如果你不想这样,需要一些额外的条件:
empty_lattice[xc+dx][yc+dy]=1 #1st octant
if (xc - dx >= 0):
empty_lattice[xc-dx][yc+dy]=1 #2nd octant
if (yc - dy >= 0):
empty_lattice[xc+dx][yc-dy]=1 #3rd octant
if (xc - dx >= 0 and yc - dy >= 0):
empty_lattice[xc-dx][yc-dy]=1 #4th octant
if (yc + dx < n):
empty_lattice[xc+dy][yc+dx]=1 #5th octant
if (xc - dy >= 0):
empty_lattice[xc-dy][yc+dx]=1 #6th octant
if (yc - dx >= 0):
empty_lattice[xc+dy][yc-dx]=1 #7th octant
if (xc - dy >= 0 and yc - dx >= 0):
empty_lattice[xc-dy][yc-dx]=1 #8th octant
编辑 3:
我意识到你的原始代码有什么问题。您的 for
循环涉及变量 hor
,但您忘记使用 hor
。此代码有效。我会留给你检查索引是否为正数。
x=0
y=radius
d=3-2*radius
while (x<=y):
for hor in range(0,x + 1): #This loop is my unfortunate attempt to fill the circle with 1s
## for ver in range(0,y):
empty_lattice[xc+hor][yc+y]=1 #1st octant
empty_lattice[xc-hor][yc+y]=1 #2nd octant
empty_lattice[xc+hor][yc-y]=1 #3rd octant
empty_lattice[xc-hor][yc-y]=1 #4th octant
empty_lattice[xc+x][yc+hor]=1 #1st octant
empty_lattice[xc-x][yc+hor]=1 #2nd octant
empty_lattice[xc+x][yc-hor]=1 #3rd octant
empty_lattice[xc-x][yc-hor]=1 #4th octant
empty_lattice[xc+hor][yc+x]=1 #5th octant
empty_lattice[xc-hor][yc+x]=1 #6th octant
empty_lattice[xc+hor][yc-x]=1 #7th octant
empty_lattice[xc-hor][yc-x]=1 #8th octant
empty_lattice[xc+y][yc+hor]=1 #5th octant
empty_lattice[xc-y][yc+hor]=1 #6th octant
empty_lattice[xc+y][yc-hor]=1 #7th octant
empty_lattice[xc-y][yc-hor]=1 #8th octant
if (d<0):
d=d+4*x+6
else:
d=d+4*(x-y)+10
y=y-1
x=x+1
我需要实施关于该主题的 Midpoint Circle Algorithm in matplotlib
, so that a circle is rasterized on a square grid of 200x200
cells. Please also refer to my
感谢 matplotlib
绘制一个 实心圆 内部 1
和 0
外面.
这就是我用 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
现在,这就是我得到的,但是你看到我的圆圈是空的,并且在它底部的横线上有一个"gap"。该间隙出现在每个八分圆交点处。 如何修改我的代码以填补圆圈和空白?
编辑
采用
要消除差距,您需要将水平范围扩大一格。在您的代码中,那将是行 for hor in range(0,x + 1):
。我的代码采用了不同的策略。
据我了解,中点圆算法只是绕着圆的周长走。所以我认为它不能轻易修改以填充内部空间。下面的代码检查第一个八分圆中的每个点,看看该点到中心的距离是否小于或等于半径。如果在半径内,则每个八分圆内的8个对应点全部填满。
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
r2 = radius ** 2
for dx in range(0, radius):
dx2 = dx ** 2
for dy in range(0, dx + 1):
dy2 = dy ** 2
if (dx2 + dy2 <= r2):
empty_lattice[xc+dx][yc+dy]=1 #1st octant
empty_lattice[xc-dx][yc+dy]=1 #2nd octant
empty_lattice[xc+dx][yc-dy]=1 #3rd octant
empty_lattice[xc-dx][yc-dy]=1 #4th octant
empty_lattice[xc+dy][yc+dx]=1 #5th octant
empty_lattice[xc-dy][yc+dx]=1 #6th octant
empty_lattice[xc+dy][yc-dx]=1 #7th octant
empty_lattice[xc-dy][yc-dx]=1 #8th octant
plt.imshow(empty_lattice)
plt.show()
如果你真的想坚持使用中点圆算法,你可以画出周长,然后从中心点开始flood fill。
编辑 1:
去掉了两行不必要的内容。
编辑 2:
以上代码以模块化方式将圆环绕图像边缘。如果你不想这样,需要一些额外的条件:
empty_lattice[xc+dx][yc+dy]=1 #1st octant
if (xc - dx >= 0):
empty_lattice[xc-dx][yc+dy]=1 #2nd octant
if (yc - dy >= 0):
empty_lattice[xc+dx][yc-dy]=1 #3rd octant
if (xc - dx >= 0 and yc - dy >= 0):
empty_lattice[xc-dx][yc-dy]=1 #4th octant
if (yc + dx < n):
empty_lattice[xc+dy][yc+dx]=1 #5th octant
if (xc - dy >= 0):
empty_lattice[xc-dy][yc+dx]=1 #6th octant
if (yc - dx >= 0):
empty_lattice[xc+dy][yc-dx]=1 #7th octant
if (xc - dy >= 0 and yc - dx >= 0):
empty_lattice[xc-dy][yc-dx]=1 #8th octant
编辑 3:
我意识到你的原始代码有什么问题。您的 for
循环涉及变量 hor
,但您忘记使用 hor
。此代码有效。我会留给你检查索引是否为正数。
x=0
y=radius
d=3-2*radius
while (x<=y):
for hor in range(0,x + 1): #This loop is my unfortunate attempt to fill the circle with 1s
## for ver in range(0,y):
empty_lattice[xc+hor][yc+y]=1 #1st octant
empty_lattice[xc-hor][yc+y]=1 #2nd octant
empty_lattice[xc+hor][yc-y]=1 #3rd octant
empty_lattice[xc-hor][yc-y]=1 #4th octant
empty_lattice[xc+x][yc+hor]=1 #1st octant
empty_lattice[xc-x][yc+hor]=1 #2nd octant
empty_lattice[xc+x][yc-hor]=1 #3rd octant
empty_lattice[xc-x][yc-hor]=1 #4th octant
empty_lattice[xc+hor][yc+x]=1 #5th octant
empty_lattice[xc-hor][yc+x]=1 #6th octant
empty_lattice[xc+hor][yc-x]=1 #7th octant
empty_lattice[xc-hor][yc-x]=1 #8th octant
empty_lattice[xc+y][yc+hor]=1 #5th octant
empty_lattice[xc-y][yc+hor]=1 #6th octant
empty_lattice[xc+y][yc-hor]=1 #7th octant
empty_lattice[xc-y][yc-hor]=1 #8th octant
if (d<0):
d=d+4*x+6
else:
d=d+4*(x-y)+10
y=y-1
x=x+1