Skip to content

Instantly share code, notes, and snippets.

@carlesso
Created April 2, 2022 08:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlesso/8341f795a4ad3e9e010438c1ce9b9b0e to your computer and use it in GitHub Desktop.
Save carlesso/8341f795a4ad3e9e010438c1ce9b9b0e to your computer and use it in GitHub Desktop.
from PIL import Image
from random import choice
ITERATIONS = 1_000_000
SIZE = 1000
OFFSET = 50
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
START_POINT = (SIZE // 2, SIZE // 2)
POINTS = (
(OFFSET, SIZE - OFFSET),
(SIZE // 2, OFFSET),
(SIZE - OFFSET, SIZE - OFFSET)
)
def get_point(point):
# Randomly pick a vertex of the triangle
p = choice(POINTS)
# Calculate the point halfway with the provided one
return (int((point[0] + p[0]) / 2), int((point[1] + p[1]) / 2))
def main():
# Create an image filled with white
image = Image.new("RGB", (SIZE, SIZE), (255, 255, 255))
# Draw the vertexes and the starting point in black
for p in POINTS:
image.putpixel(p, BLACK)
current_point = START_POINT
image.putpixel(current_point, BLACK)
# Iterate calcualting the mid point and drawing it in black
for i in range(ITERATIONS):
current_point = get_point(current_point)
image.putpixel(current_point, BLACK)
image.show()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment