Created
April 2, 2022 08:17
-
-
Save carlesso/8341f795a4ad3e9e010438c1ce9b9b0e to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
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