更多的pythonic圈? [英] More pythonic circle?

查看:46
本文介绍了更多的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

推荐答案

OTTOMH,急于出门:从不介意Pythonic,以下申请

任何语言:

(1)准确度:(a)如果我错了就起诉我,但我认为你需要范围(dx + 1)

使dx像素填入(b)0.71后可能有用的几个数字
可能有用

(2)效率:似乎是范围(dy,dx) +1)可以节省一些磨损和撕掉电路上的
:-)

(3)易读性:这个剧本没有奖品,绝对是

最小号码太空人物:-)

我想我在

档案中有一篇关于更好的Bresenham的文章;以后会把它挖出来。

干杯,

John

OTTOMH, in a rush to go out: never mind Pythonic, following apply to
any language:
(1) accuracy: (a) sue me if I''m wrong, but I think you need range(dx+1)
so that the dx pixel is filled in (b) a few more digits after 0.71
might be useful
(2) efficiency: seems that range(dy, dx+1) would save some wear & tear
on the circuitry :-)
(3) legibility: there''s no prize for the script with the absolutely
minimum number of space characters :-)
I think I''ve got an article on better Bresenham somewhere in the
archives; will dig it out later.
Cheers,
John




John Machin写道:

John Machin wrote:
OTTOMH,急于出去:从不介意Pythonic,以下申请
任何语言:
(1)准确性:(a)起诉我,如果我错了,但我认为你需要范围(dx + 1)
以便dx像素填充
Hmm。我觉得你是对的。谢谢。 (b)在0.71
之后可能有用几个数字
45度的正弦实际上是.707 ...我收尾了,因为我正在使用

< = 。想象一下就可以说清楚了。 (2)效率:似乎范围(dy,dx + 1)可以节省一些磨损和在电路上撕裂:-)
我花了几分钟才弄明白你的意思。这将

肯定有助于减少重复坐标。再次感谢。

(3)易读性:对于具有绝对最小空格字符数的脚本没有奖品:-)
True。我假设你说我应该更好地使用cx,cy,dx和dy

名字。我可能会。到目前为止,我只是在玩这个,而不是真的希望其他人阅读它。

我想我有一篇关于更好的Bresenham的文章在档案馆中;我会在以后挖掘它。
OTTOMH, in a rush to go out: never mind Pythonic, following apply to
any language:
(1) accuracy: (a) sue me if I''m wrong, but I think you need range(dx+1)
so that the dx pixel is filled in Hmm. I think you''re right. Thanks. (b) a few more digits after 0.71
might be useful Sine of 45 degrees is actually .707... I rounded up, since I was using
<=. Figured that would make it clear. (2) efficiency: seems that range(dy, dx+1) would save some wear & tear
on the circuitry :-) It took me a few minutes to figure out waht you meant here. This will
certainly help reduce the repeated coordinates. Thanks, again.
(3) legibility: there''s no prize for the script with the absolutely
minimum number of space characters :-) 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.
I think I''ve got an article on better Bresenham somewhere in the
archives; will dig it out later.



我一定很感激。事实上,我正在努力寻找一个体面的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


Em S ?? b,2006-04-08?* s 21:17 - 0700,Pythor escreveu:
Em S??b, 2006-04-08 ?*s 21:17 -0700, Pythor escreveu:
John Machin写道:
John Machin wrote:
(3)易读性:绝对
最小空间数的剧本没有奖品字符: - )
(3) legibility: there''s no prize for the script with the absolutely
minimum number of space characters :-)


是的。我假设你说我应该制作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圈?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆