Advertisement
here2share

# Tk_2_Swirly_Tears.py

Jan 6th, 2020
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.76 KB | None | 0 0
  1. # Tk_2_Swirly_Tears.py
  2.  
  3. from Tkinter import *
  4. from PIL import Image, ImageTk
  5. from random import random
  6.  
  7. import math
  8. import sys
  9.  
  10. wt = 512
  11. ht = 512
  12. root = Tk()
  13. root.title("Tk 2 Swirly Tears")
  14. root.geometry("%dx%d+0+0"%(wt,ht))
  15. canvas = Canvas(root, width=wt, height=ht)
  16. canvas.grid()
  17.  
  18. bgcolor = [0.2, 0.2, 0.2]
  19.  
  20. def make_circle(radius, cx, cy, color):
  21.     def f(x,y):
  22.         if (x-cx)*(x-cx) + (y-cy)*(y-cy) < radius*radius:
  23.             return color, True
  24.         return bgcolor, False
  25.     return f
  26.  
  27.  
  28. def distort(x,y):
  29.     xp,yp = x-0.5,y-0.5
  30.     r2 = xp*xp + yp*yp
  31.     theta = 10*r2*math.pi
  32.     sintheta = math.sin(theta)
  33.     costheta = math.cos(theta)
  34.     xr = 0.5 + xp*costheta - yp*sintheta
  35.     yr = 0.5 + xp*sintheta + yp*costheta
  36.     return (xr,yr)
  37.  
  38. numsamples = 1
  39.  
  40. img = Image.new("RGB", (wt, ht))
  41. pix = []
  42.  
  43. objects = [make_circle(0.2, 0.33, 0.33, [1.0, 0.0, 0.0]),
  44.            make_circle(0.2, 0.66, 0.66, [0.0, 1.0, 0.0])
  45.            ]
  46.  
  47. n = numsamples*numsamples
  48. deltaX = 1.0 / wt
  49. deltaY = 1.0 / ht
  50. dx = deltaX / numsamples
  51. dy = deltaY / numsamples
  52. y = 1.0 - deltaY
  53. for row in range(0, ht):
  54.     x = 0.0
  55.     for col in range(0, wt):
  56.         value = [0.0, 0.0, 0.0]
  57.         for i in range(0, numsamples):
  58.             for j in range(0, numsamples):
  59.                 sampleX = x + dx*(j+random())
  60.                 sampleY = y + dy*(i+random())
  61.                 sampleX,sampleY = distort(sampleX, sampleY)
  62.                 hitany = False
  63.                 for f in objects:
  64.                         sampleValue,hitany = f(sampleX, sampleY)
  65.                         for k in range(0,3):
  66.                             value[k] += sampleValue[k]
  67.                         if hitany:
  68.                            break;
  69.         pixval = (int(255.0*value[0]/n), int(255.0*value[1]/n), int(255.0*value[2]/n))
  70.         pix.append(pixval)
  71.         x = x + deltaX
  72.     y = y - deltaY
  73.     #print pix
  74.  
  75. img.putdata(pix)
  76. imgTk = ImageTk.PhotoImage(img)
  77. canvas.create_image(-2, 0, anchor=NW, image=imgTk)
  78. root.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement