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))

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.