Last active
March 27, 2018 17:50
-
-
Save ElectrWeakHyprCharge/3f476cdacbf7e9873c6c4269423e8fd4 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
from PIL import Image | |
import operator as op | |
from functools import reduce | |
def nCr(n, r): #Just n choose k | |
r = min(r, n-r) | |
numer = reduce(op.mul, range(n, n-r, -1), 1) | |
denom = reduce(op.mul, range(1, r+1), 1) | |
return numer//denom | |
class PascalTriangle: | |
def __init__(self, start_row, end_row, only_odds_and_two, only_left_half, crop_ones): | |
self.only_left_half = only_left_half | |
self.only_odds_and_two = only_odds_and_two | |
count_step = 2 if only_odds_and_two else 1 | |
starting_element = 1 if crop_ones else 0 | |
self.rows = [] | |
for row_index in range(start_row, end_row, count_step): | |
ending_element = row_index//2 if only_left_half else row_index | |
self.rows.append([nCr(row_index, element) for element in range(starting_element, ending_element + 1)]) | |
if row_index % 50 == 0: print('Completed row #', row_index, sep = '') | |
self.width = len(self.rows[-1]) | |
self.height = len(self.rows) | |
def generate_primality_visual(triangle, fill_mod): | |
#COLORS: | |
DOMAIN = (0x00, 0x00, 0x00) | |
OFF_WHITE = (0xAA, 0xAA, 0xAA) | |
scale_width = 2 if triangle.only_left_half else 1 | |
img = Image.new("RGB", (triangle.width * scale_width, triangle.height), OFF_WHITE) | |
pixels = img.load() | |
print("Image size:", img.width, "x", img.height) | |
for y in range(triangle.height): | |
current_row = triangle.rows[y] | |
offset = int((img.width - len(current_row) * scale_width)/2) | |
for x in range(len(current_row)): | |
mod_value = current_row[x] % y | |
try: | |
if mod_value == 0: | |
pixels[x + offset, y] = DOMAIN | |
elif fill_mod: | |
percent = mod_value/y | |
target_shade = (int(percent*255), int(percent*255), int(percent*255) | |
pixels[x + offset, y] = target_shade | |
except Exception as e: | |
print("Failed coordinate writing: (%s, %s)." % (x + offset, y)) | |
raise | |
if triangle.only_left_half: | |
offset += 2; | |
for x in range(len(current_row)): | |
mod_value = current_row[x] % y | |
try: | |
if mod_value == 0: | |
pixels[img.width - x - offset, y] = DOMAIN | |
elif fill_mod: | |
percent = mod_value/y | |
target_shade = (int(percent*255), int(percent*255), int(percent*255) | |
pixels[img.width - x - offset, y] = target_shade; | |
except Exception as e: | |
print("Failed coordinate writing (right half): (%s, %s)." % (x + offset, y)) | |
raise | |
return img | |
if __name__ == '__main__': | |
start, end = 0, 2048 #Feel free to change this | |
print("Making triangle rows %s - %s." % (start, end)) | |
triangle = PascalTriangle(start, end, False, True, True) | |
print("Done making triangle.") | |
#Recommendation: Pickle triangle so it can be loaded faster later | |
#with open('./triangle.pickle', 'wb') as f: pickle.dump(triangle, f) | |
#Then load it with: | |
#with open('./triangle.pickle', 'rb') as f: triangle = pickle.load(f) | |
image = generate_primality_visual(triangle, True) | |
image.save("./triangle.png") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What is generated: