pixels_in_line = 0
pixels_per_line = []
diameter = int(input('Enter the diameter of the circle: '))
# You must account for the loops being zero-based, but the quotient of the diameter / 2 being
# one-based. If you use the exact radius, you will be short one column and one row.
offset_radius = (diameter / 2) - 0.5
for i in range(diameter):
for j in range(diameter):
x = i - offset_radius
y = j - offset_radius
if x * x + y * y <= offset_radius * offset_radius + 1:
print('*', end=' ')
pixels_in_line += 1
else:
print(' ', end=' ')
pixels_per_line.append(pixels_in_line)
pixels_in_line = 0
print()
print('The pixels per line are {0}.'.format(pixels_per_line))
7 的输出结果:
Enter the diameter of the circle: 7
* * *
* * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * *
* * *
The pixels per line are [3, 5, 7, 7, 7, 5, 3].
12 的输出:
Enter the diameter of the circle: 12
* *
* * * * * *
* * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * *
* * * * * *
* *
The pixels per line are [2, 6, 8, 10, 10, 12, 12, 10, 10, 8, 6, 2].
if x ** 2 + y ** 2 <= offset_radius ** 2 + 1:
代替if x * x + y * y <= offset_radius * offset_radius + 1:
。另外,您可以将所有的*
放在一个字符串中(用'\n'连接行),并一次性打印该字符串。不需要打印每个像素,并且不需要在循环内计算每个*
的数量(可以使用list.count('*')
方法进行计数)。我认为这将是更清晰的代码,只需三个步骤:(1)生成字符串,(2)打印字符串,(3)统计字符串中每行的“像素”数量并打印报告。 - Yuri Khristich根据Rob的解决方案(所有功劳归Rob!),我已经成功地为12x12像素网格调整了代码:
diameter = 12
radius = diameter / 2 - .5
r = (radius + .25)**2 + 1
result = ''
for i in range(diameter):
y = (i - radius)**2
for j in range(diameter):
x = (j - radius)**2
if x + y <= r:
result = result + '* '
else:
result = result + ' '
result = result + '\n'
print(result)
result = result.split('\n')[:-1]
pixels_per_line = [x.count('*') for x in result]
print(f'The pixels per line are {pixels_per_line}.')
输出:
* * * *
* * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * *
* * * *
The pixels per line are [4, 8, 10, 10, 12, 12, 12, 12, 10, 10, 8, 4].
如果您需要圆形内部的空白区域,只需进行最小更改即可:
diameter = 7
radius = diameter / 2 - .5
r = (radius + .25)**2 + 1
r_min = (radius - 1)**2 + 1 # <-------- here
result = ''
for i in range(diameter):
y = (i - radius)**2
for j in range(diameter):
x = (j - radius)**2
if r_min <= x + y <= r: # <----- here
result = result + '* '
else:
result = result + ' '
result = result + '\n'
print(result)
输出:
* * *
* * * *
* * * *
* *
* * * *
* * * *
* * *
* * * *
* * * * * *
* * * *
* *
* * * *
* *
* *
* * * *
* *
* * * *
* * * * * *
* * * *
import math
radius = 10
for i in range(-radius,radius+1):
for j in range(-radius, radius +1):
if radius-1 <= math.sqrt(i**2 + j**2) <= radius:
print("*",end = " ")
else:
print(" ", end = ' ')
print()
*
* * * * * * * * *
* * * *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* * * *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* * * *
* * * * * * * * *
*
radius = 12
for i in range(-radius,radius+1):
for j in range(-radius, radius +1):
if math.sqrt(i**2 + j**2) <= radius:
print("*",end = " ")
else:
print(" ", end = ' ')
print()
输出
*
* * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * *
*