Last active
September 19, 2017 04:01
-
-
Save pbfy0/73d930caa16b165fe0667702d260db16 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/python3 | |
from tkinter import * | |
import ctypes | |
import functools | |
import platform | |
is_win = platform.system() == 'Windows' | |
if is_win: | |
u32 = ctypes.windll.user32 | |
u32.SetProcessDPIAware() | |
rows = 9**2#2**8 | |
base = 9 | |
size = 40 / (3**.5) | |
width = 2000 | |
height = 1400 | |
#sieve = None | |
#def product(x): | |
# return functools.reduce(operator.mul, x) | |
#if base == 'prime': | |
# import operator | |
# max_prime = rows#product(range(rows // 2, rows)) // product(range(1, rows//2+1)) * 2 + 1 | |
# sieve = [True] * max_prime | |
# for i in range(2, int(max_prime ** 0.5)): | |
# if sieve[i] == False: continue | |
# for j in range(2*i, max_prime, i): | |
# sieve[j] = False | |
# sieve[1] = False | |
#n_pascals = 0 | |
m_sz = 2*rows+1 | |
@functools.lru_cache(maxsize=1<<(m_sz - 1).bit_length()) | |
def pascal(r, o): | |
#global n_pascals | |
#n_pascals += 1 | |
if o < 0 or o > r: return 0 | |
if r == 0: return 1 | |
return pascal(r - 1, o - 1) + pascal(r - 1, o) | |
master = Tk() | |
sq3_2 = (3**.5)/2 | |
coords = ( | |
0, 1, | |
sq3_2, 1/2, | |
sq3_2, -1/2, | |
0, -1, | |
-sq3_2, -1/2, | |
-sq3_2, 1/2, | |
) | |
def create_hex(w, x, y, sz, *args, **kwargs): | |
return w.create_polygon( | |
*(e * sz + (y if i & 1 else x) for i, e in enumerate(coords)), | |
*args, **kwargs | |
) | |
w = Canvas(master, width=width, height=height) | |
if is_win: | |
dpi = u32.GetDpiForWindow(w.winfo_id()) | |
master.tk.call('tk', 'scaling', dpi / 96) | |
w.pack() | |
w2 = width // 2 | |
for i in range(rows): | |
for idx in range(i + 1): | |
o = idx - i / 2 | |
pc = pascal(i, idx) | |
create_hex(w, | |
w2 + o * 2 * size, | |
(i + 1) * sq3_2 * 2 * size, | |
size / sq3_2, | |
fill='blue' if (pc % base == 0) else '', outline='black' | |
#fill='blue' if (pc % base == 0 if base != 'prime' else (sieve[pc] if pc < len(sieve) else False)) else '', outline='black' | |
) | |
print('done generating') | |
#print(n_pascals) | |
mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment