For our exploration of drawing faux-digits, this function draws every possible combination that can be displayed on a standard digital clock. It saves each as a PNG.
import matplotlib.pyplot as plt | |
# Vertices of the digit, starting at the origin, moving clockwise | |
POINTS = { | |
"A": [0, 0], | |
"B": [0, 1], | |
"C": [0, 2], | |
"D": [1, 2], | |
"E": [1, 1], | |
"F": [1, 0], | |
} | |
# Lines of the digit, starting at the top | |
SEGMENTS = { | |
0: { | |
"p1": POINTS["C"], | |
"p2": POINTS["D"] | |
}, | |
1: { | |
"p1": POINTS["C"], | |
"p2": POINTS["B"] | |
}, | |
2: { | |
"p1": POINTS["D"], | |
"p2": POINTS["E"] | |
}, | |
3: { | |
"p1": POINTS["B"], | |
"p2": POINTS["E"] | |
}, | |
4: { | |
"p1": POINTS["B"], | |
"p2": POINTS["A"] | |
}, | |
5: { | |
"p1": POINTS["E"], | |
"p2": POINTS["F"] | |
}, | |
6: { | |
"p1": POINTS["A"], | |
"p2": POINTS["F"] | |
}, | |
} | |
def draw_numeral(decimal_number): | |
fig, ax = plt.subplots() | |
binary_string = format(decimal_number, '07b') | |
for bit, ind in zip(binary_string, range(7)): | |
if int(bit): | |
draw_segment(ax, ind) | |
ax.set_xlim([-1, 2]) | |
ax.set_ylim([-0.25, 2.25]) | |
plt.axis('off') | |
plt.savefig(f"numerals/{binary_string}.png", transparent=True) | |
plt.clf() | |
def draw_segment(ax, line_number): | |
p1 = SEGMENTS[line_number]["p1"] | |
p2 = SEGMENTS[line_number]["p2"] | |
draw_line(ax, p1, p2) | |
def draw_line(ax, p1, p2): | |
x = [p1[0], p2[0]] | |
y = [p1[1], p2[1]] | |
ax.plot(x, y, color="red", linewidth=3.0) | |
if __name__ == "__main__": | |
for decimal_number in range(127): | |
draw_numeral(decimal_number + 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment