本文介绍了更多的pythonic圈?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为个人项目编写了以下代码。我需要一个函数

,它将在二维数组中绘制一个实心圆。我找到了

Bresenham的算法,并生成了这段代码。请告诉我有更好的方法来实现这个目标。


导入numpy


def circle( field = None,radius,center =(0,0),value = 255,):

''''''返回距离点''半径''内的点列表

''center''。''''''

if field == None:

field = numpy.zeros((radius,radius) ),''你')

cx,cy = center

filllist = []

dy,dx = 0,radius

r2 =半径** 2

而dy< =(半径* .71):#sin为45度

如果dx ** 2 + dy ** 2< = r2:

for x in range(dx):

filllist.append((x,dy))

dy + = 1

否则:

dx- = 1

如果dx< dy:break

resultlist = [ ]

for(i,j)填写表:

field [cx + i] [cy + j] = value

field [cx + j] [cy + i] =值

字段[cx-j] [cy + i] =值

字段[cx-i] [cy + j] =值

字段[cx-i] [cy-j] =值

字段[cx-j] [c yi] =价值

字段[cx + j] [cy-i] =值

字段[cx + i] [cy-j] = value

返回字段

I wrote the following code for a personal project. I need a function
that will plot a filled circle in a two dimensional array. I found
Bresenham''s algorithm, and produced this code. Please tell me there''s
a better way to do this.

import numpy

def circle(field=None,radius,center=(0,0),value=255,):
''''''Returns a list of points within ''radius'' distance from point
''center''.''''''
if field==None:
field=numpy.zeros((radius,radius),''u'')
cx,cy=center
filllist=[]
dy,dx=0,radius
r2=radius**2
while dy<=(radius*.71): # sin of 45 degrees
if dx**2+dy**2<=r2:
for x in range(dx):
filllist.append((x,dy))
dy+=1
else:
dx-=1
if dx<dy : break
resultlist=[]
for (i,j) in filllist:
field[cx+i][cy+j]=value
field[cx+j][cy+i]=value
field[cx-j][cy+i]=value
field[cx-i][cy+j]=value
field[cx-i][cy-j]=value
field[cx-j][cy-i]=value
field[cx+j][cy-i]=value
field[cx+i][cy-j]=value
return field

推荐答案




我一定很感激。事实上,我正在努力寻找一个体面的b $ b球体版本,并且谷歌无处可去。我试图用自己的方式来计算

,最后每个有效测试都有48个坐标。

我不确定这是不对的。


Lee


I''d definitely appreciate it. In fact, I''m trying to find a decent
sphere version, and getting nowhere with google. I tried to figure it
out on my own, and ended up with 48 coordinates for each valid test.
I''m not sure if that''s right.

Lee



是的。我假设你说我应该制作cx,cy,dx和dy更好的名字。我可能会。到目前为止,我只是在玩这个,并没有真正期待其他人阅读它。


True. I assume your saying I should make cx,cy,dx, and dy better
names. I probably will. Up to now I was just playing around with
this, and not really expecting anyone else to read it.




这有点好笑,因为你刚发布了代码到一个列表与

数百名开发人员。 =)


-

Felipe。



This is kinda funny because you just posted the code to a list with
hundreds of developers. =)

--
Felipe.


这篇关于更多的pythonic圈?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 23:58