Created
February 25, 2023 06:21
-
-
Save scerruti/0be0ccae4e3e994db3dccc130d18e61d to your computer and use it in GitHub Desktop.
This script creates a ring of octothorpe (#) characters with optional command line arguments for radius and the width of the interior as a portion of the radius.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from math import sqrt | |
def print_circle(circle): | |
""" | |
outputs a circle that is stored as a 2D array of characters | |
:param circle: 2D array of characters | |
:return: | |
""" | |
for row in circle: | |
for col in row: | |
print(col, end='') | |
print() | |
def create_circle(radius, width=0.6): | |
""" | |
Creates a ring of octothorpes of with a radius of radius and | |
an interior clear space width percent of the radius. | |
:param radius: radius of the circle to be drawn | |
:param width: percent of radius that is interior of ring | |
:return: | |
""" | |
circle = [] | |
for _ in range(2 * (radius + 1) + 1): | |
row = [] | |
circle.append(row) | |
for col in range(4 * (radius + 1) + 2): | |
row.append('.') | |
center = (radius + 1, radius + 1) | |
for row in range(len(circle)): | |
for col in range(0, len(circle[row]), 2): | |
distance = sqrt((center[0] - row) ** 2 + (center[1] - col / 2) ** 2) | |
if radius * width < distance < radius: | |
circle[row][col] = circle[row][col + 1] = '#' | |
print_circle(circle) | |
if __name__ == '__main__': | |
r = 8 | |
w = .7 | |
if len(sys.argv) > 1: | |
r = int(sys.argv[1]) | |
if len(sys.argv) > 2: | |
w = float(sys.argv[2]) | |
create_circle(r, w) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment