使用 python 在控制台中画圆
Draw circle in console using python
我想在控制台中使用字符而不是像素画一个圆,为此我需要知道每一行有多少像素。
直径作为输入给出,您需要输出一个列表,其中包含图片每行的宽度(以像素为单位)
例如:
输入:7
输出:[3, 5, 7, 7, 7, 5, 3]
输入:12
输出:[4, 8, 10, 10, 12, 12, 12, 12, 10, 10, 8, 4]
如何实施?
这很好地提醒了我在混合基于零和基于一的计算时要小心。在这种情况下,我不得不考虑 for
循环是从零开始的,但直径除以 2 的商是从一开始的。否则,地块将超过或低于 1。
顺便说一句,虽然我匹配了你对 7
的回答,但我没有为 12
想出完全相同的情节:
NOTE - Tested using Python 3.9.6
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].
基于 (所有功劳归功于 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)
输出:
* * *
* * * *
* * * *
* *
* * * *
* * * *
* * *
* * * *
* * * * * *
* * * *
* *
* * * *
* *
* *
* * * *
* *
* * * *
* * * * * *
* * * *
我想在控制台中使用字符而不是像素画一个圆,为此我需要知道每一行有多少像素。 直径作为输入给出,您需要输出一个列表,其中包含图片每行的宽度(以像素为单位)
例如:
输入:7
输出:[3, 5, 7, 7, 7, 5, 3]
输入:12
输出:[4, 8, 10, 10, 12, 12, 12, 12, 10, 10, 8, 4]
如何实施?
这很好地提醒了我在混合基于零和基于一的计算时要小心。在这种情况下,我不得不考虑 for
循环是从零开始的,但直径除以 2 的商是从一开始的。否则,地块将超过或低于 1。
顺便说一句,虽然我匹配了你对 7
的回答,但我没有为 12
想出完全相同的情节:
NOTE - Tested using Python 3.9.6
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].
基于
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)
输出:
* * *
* * * *
* * * *
* *
* * * *
* * * *
* * *
* * * *
* * * * * *
* * * *
* *
* * * *
* *
* *
* * * *
* *
* * * *
* * * * * *
* * * *