圆的基本公式是(x - a)**2 + (y - b)**2 = r**2
其中(x,y)是一个点,(a,b)是圆的中心,r是半径。width, height = 11, 11
a, b = 5, 5
r = 5
EPSILON = 2.2
map_ = [['.' for x in range(width)] for y in range(height)]
# draw the circle
for y in range(height):
for x in range(width):
# see if we're close to (x-a)**2 + (y-b)**2 == r**2
if abs((x-a)**2 + (y-b)**2 - r**2) < EPSILON**2:
map_[y][x] = '#'
# print the map
for line in map_:
print ' '.join(line)
这将导致. . . # # # # # . . .
. . # . . . . . # . .
. # . . . . . . . # .
# . . . . . . . . . #
# . . . . . . . . . #
# . . . . . . . . . #
# . . . . . . . . . #
# . . . . . . . . . #
. # . . . . . . . # .
. . # . . . . . # . .