Fill a circle with 0s and 1s
#!/usr/bin/env python3 | |
# | |
# Fill a circle with 0 and 1's | |
# The circle is made of half as many lines as it is made of columns. | |
# This way, it usually appears round (with most terminal fonts at least). | |
# Accept the circle's width-radius as argument. See the default radius r, below. | |
r = 15 | |
from sys import argv | |
if len(argv) > 1: | |
r = int(argv[1]) | |
import random | |
def rand01generator(): | |
while True: | |
r = random.randrange(2 ** 32) | |
for i in range(32): | |
yield 1 & r >> i | |
class Circle: | |
def __init__(self, r): | |
self.r = r | |
def __contains__(self, point): | |
x, y = point | |
return x ** 2 + y ** 2 <= self.r ** 2 | |
c = Circle(r) | |
g = rand01generator() | |
text = [] | |
for y in range(r): | |
line = [] | |
for x in range(2*r): | |
if (x + 0.5 - r, 2 * y + 1 - r) in c: | |
line.append(str(next(g))) | |
else: | |
if x <= r: | |
line.append(" ") | |
text.append("".join(line)) | |
print("\n".join(text)) | |
example_output = """ | |
0101010011 | |
011110000011100101 | |
1110111010111111011011 | |
00000000101011000010100100 | |
1101011100101001001110110100 | |
1101110101110101100101111111 | |
010101001110010111001011111101 | |
101100011100101101110011000111 | |
110111000110000010000010001101 | |
0010100110001000110010010000 | |
0100110101000100011000100111 | |
11100100010011000000001000 | |
1001010101101101010101 | |
011111010101101011 | |
1011110000 | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment