Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_ElectricNeon.py
- from tkinter import *
- from PIL import Image, ImageTk
- import random
- import math
- import time
- # Define constants
- ww = 512
- hh = 512
- o255 = [z for z in range(256)]
- o255 = o255[1:-1] + o255[::-1]
- Lo = len(o255)
- xy2field = {}
- c = 0
- for y in range(hh):
- for x in range(ww):
- xy2field[x,y] = c
- c += 1
- XY = [0] * (ww * hh)
- # Define function for calculating distance between two points
- def distance(x1, y1, x2, y2):
- return int(((x1 - x2)**2 + (y1 - y2)**2)**0.5) # no need to get exact distance
- rndi = random.randint
- def Voronoi(numPoints):
- basePoints = []
- for i in range(numPoints):
- x = rndi(0, ww)
- y = rndi(0, hh)
- basePoints.append((x, y))
- for y in range(hh):
- for x in range(ww):
- distances = []
- for px, py in basePoints:
- distances += [distance(x, y, px, py)]
- distances = sorted(distances)
- r = o255[sum(distances[:3])%Lo]
- g = o255[sum(distances[2:6])%Lo]
- b = o255[sum(distances[4:9])%Lo]
- points[xy2field[x,y]] = (r,g,b)
- return points
- root = Tk()
- root.title("Tk ElectricNeon")
- root.geometry("%dx%d+-10+0"%(ww,hh))
- canvas = Canvas(root, width=ww, height=hh)
- canvas.pack()
- while 1:
- points = XY[:]
- pixels = Voronoi(30)
- img = Image.new("RGB", (ww,hh))
- img.putdata(pixels)
- imgTk = ImageTk.PhotoImage(img)
- canvas.create_image(0, 0, anchor=NW, image=imgTk)
- canvas.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement