More pythonic circle?

Pythor pythor at gmail.com
Sat Apr 8 21:04:56 EDT 2006


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




More information about the Python-list mailing list