Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save ElectrWeakHyprCharge/3f476cdacbf7e9873c6c4269423e8fd4 to your computer and use it in GitHub Desktop.

Select an option

Save ElectrWeakHyprCharge/3f476cdacbf7e9873c6c4269423e8fd4 to your computer and use it in GitHub Desktop.

Revisions

  1. ElectrWeakHyprCharge revised this gist Mar 27, 2018. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions Pascal's triangle mod everything thing.py
    Original file line number Diff line number Diff line change
    @@ -31,7 +31,6 @@ def generate_primality_visual(triangle, fill_mod):
    #COLORS:
    DOMAIN = (0x00, 0x00, 0x00)
    OFF_WHITE = (0xAA, 0xAA, 0xAA)
    target_shade = (None, None, None) #This is just a placeholder, actually

    scale_width = 2 if triangle.only_left_half else 1

    @@ -53,7 +52,7 @@ def generate_primality_visual(triangle, fill_mod):

    elif fill_mod:
    percent = mod_value/y
    target_shade = (0, int(percent*255), 0)
    target_shade = (int(percent*255), int(percent*255), int(percent*255)
    pixels[x + offset, y] = target_shade

    except Exception as e:
    @@ -70,7 +69,7 @@ def generate_primality_visual(triangle, fill_mod):
    pixels[img.width - x - offset, y] = DOMAIN
    elif fill_mod:
    percent = mod_value/y
    target_shade = (0, int(percent*255), 0)
    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))
  2. ElectrWeakHyprCharge renamed this gist Mar 27, 2018. 1 changed file with 0 additions and 0 deletions.
  3. ElectrWeakHyprCharge renamed this gist Mar 27, 2018. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. ElectrWeakHyprCharge created this gist Mar 27, 2018.
    94 changes: 94 additions & 0 deletions math thing.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,94 @@
    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)
    target_shade = (None, None, None) #This is just a placeholder, actually

    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 = (0, int(percent*255), 0)
    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 = (0, int(percent*255), 0)
    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")