Created
June 25, 2020 23:35
-
-
Save christian-oudard/488aaa0841a0a88491ec2df1269a69f0 to your computer and use it in GitHub Desktop.
Use unicode braille characters to draw a picture in the terminal
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 math | |
import shutil | |
# Braille bit order: | |
# 0 3 | |
# 1 4 | |
# 2 5 | |
# 6 7 | |
# bit: (x, y) | |
dot_positions = { | |
0: (0, 0), | |
1: (0, 1), | |
2: (0, 2), | |
3: (1, 0), | |
4: (1, 1), | |
5: (1, 2), | |
6: (0, 3), | |
7: (1, 3), | |
} | |
bit_positions = { pos: i for (i, pos) in dot_positions.items() } | |
n_columns, n_lines = shutil.get_terminal_size() | |
n_columns, n_lines = (40, 20) | |
cell_width, cell_height = (2, 4) | |
width = n_columns * cell_width | |
height = n_lines * cell_height | |
screen_lines = [ bytearray(n_columns) for _ in range(n_lines) ] | |
def set_dot(x, y): | |
cell_x, rem_x = divmod(x, cell_width) | |
cell_y, rem_y = divmod(y, cell_height) | |
bit_index = bit_positions[(rem_x, rem_y)] | |
screen_lines[cell_y][cell_x] |= (1 << bit_index) | |
braille_offset = 0x2800 | |
def show_screen(): | |
for line in screen_lines: | |
line_chars = [ chr(braille_offset + b) for b in line ] | |
print(''.join(line_chars)) | |
if __name__ == '__main__': | |
for i in range(80): | |
set_dot(i, i) | |
set_dot(i, 80 - i - 1) | |
for a in range(256): | |
theta = 2*math.pi * a / 256 | |
x = round(40 + 38*math.cos(theta)) | |
y = round(40 + 38*math.sin(theta)) | |
set_dot(x, y) | |
show_screen() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment