I want to draw a circle in the console using characters instead of pixels, for this I need to know how many pixels are in each row. The diameter is given as input, you need to output a list with the width in pixels of each line of the picture
For example:
input: 7
output: [3, 5, 7, 7, 7, 5, 3]
input: 12
output: [4, 8, 10, 10, 12, 12, 12, 12, 10, 10, 8, 4]
How can this be implemented?
This was a good reminder for me to be careful when mixing zero-based and one-based computations. In this case, I had to account for the for
loops being zero-based, but the quotient of the diameter divided by 2 being one-based. Otherwise, the plots would have been over or under by 1.
By the way, while I matched your answer for 7
, I didn't come up with the same exact plot for 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))
Output for 7:
Enter the diameter of the circle: 7
* * *
* * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * *
* * *
The pixels per line are [3, 5, 7, 7, 7, 5, 3].
Output for 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].