voronoi.py

Created by schraf

Created on July 01, 2023

593 Bytes


from kandinsky import *
from random import randint

w, h = 320, 222

def hypot(x,y): return x * x + y * y

def rd(n): return randint(0,n)

def points(n):
 return [[rd(w), rd(h), \
    (rd(255),rd(255),rd(255))] for _ in range(n)]

def voronoi(pts):
  num_cells = len(pts)
  for y in range(h):
    for x in range(w):
      dmin, j = hypot(w-1, h-1), -1
      for i in range(num_cells):
        d = hypot(pts[i][0]-x, pts[i][1]-y)
        if d < dmin: dmin, j = d, i
      set_pixel(x, y, pts[j][2])
  for p in pts:
    set_pixel(p[0], p[1], (255,) * 3)    

voronoi(points(100))