Drawing   “”   in Python

Vita Smid

Pyvo #103

  1. Pick three vertices
  2. Randomly choose a point p from one of the vertices
  3. Randomly pick one of the vertices and move p halfway between p and the random vertex
  4. Colour p in
  5. GOTO 3

import random
import imageio
import numpy as np


max_x = 1000
max_y = int((max_x**2 - (max_x / 2)**2)**0.5)
a = np.zeros((max_y, max_x), dtype=np.uint8)

vertices = ( # [y, x] coordinates
    np.array([0, max_x // 2]),
    np.array([max_y - 1, 0]),
    np.array([max_y - 1, max_x - 1]),
)
					

p = random.choice(vertices)
for _ in range(A_LOT):
    p = (p + random.choice(vertices)) // 2
    a[tuple(p)] = 255
					

imageio.imwrite('sierpinski.png', a)
					

Thank you

quantlane.com