Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_2_Swirly_Tears.py
- from Tkinter import *
- from PIL import Image, ImageTk
- from random import random
- import math
- import sys
- wt = 512
- ht = 512
- root = Tk()
- root.title("Tk 2 Swirly Tears")
- root.geometry("%dx%d+0+0"%(wt,ht))
- canvas = Canvas(root, width=wt, height=ht)
- canvas.grid()
- bgcolor = [0.2, 0.2, 0.2]
- def make_circle(radius, cx, cy, color):
- def f(x,y):
- if (x-cx)*(x-cx) + (y-cy)*(y-cy) < radius*radius:
- return color, True
- return bgcolor, False
- return f
- def distort(x,y):
- xp,yp = x-0.5,y-0.5
- r2 = xp*xp + yp*yp
- theta = 10*r2*math.pi
- sintheta = math.sin(theta)
- costheta = math.cos(theta)
- xr = 0.5 + xp*costheta - yp*sintheta
- yr = 0.5 + xp*sintheta + yp*costheta
- return (xr,yr)
- numsamples = 1
- img = Image.new("RGB", (wt, ht))
- pix = []
- objects = [make_circle(0.2, 0.33, 0.33, [1.0, 0.0, 0.0]),
- make_circle(0.2, 0.66, 0.66, [0.0, 1.0, 0.0])
- ]
- n = numsamples*numsamples
- deltaX = 1.0 / wt
- deltaY = 1.0 / ht
- dx = deltaX / numsamples
- dy = deltaY / numsamples
- y = 1.0 - deltaY
- for row in range(0, ht):
- x = 0.0
- for col in range(0, wt):
- value = [0.0, 0.0, 0.0]
- for i in range(0, numsamples):
- for j in range(0, numsamples):
- sampleX = x + dx*(j+random())
- sampleY = y + dy*(i+random())
- sampleX,sampleY = distort(sampleX, sampleY)
- hitany = False
- for f in objects:
- sampleValue,hitany = f(sampleX, sampleY)
- for k in range(0,3):
- value[k] += sampleValue[k]
- if hitany:
- break;
- pixval = (int(255.0*value[0]/n), int(255.0*value[1]/n), int(255.0*value[2]/n))
- pix.append(pixval)
- x = x + deltaX
- y = y - deltaY
- #print pix
- img.putdata(pix)
- imgTk = ImageTk.PhotoImage(img)
- canvas.create_image(-2, 0, anchor=NW, image=imgTk)
- root.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement