Advertisement
here2share

# Tk_fractalic_coast_lines.py

Jun 13th, 2021
1,833
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.23 KB | None | 0 0
  1. # Tk_fractalic_coast_lines.py
  2.  
  3. from Tkinter import *
  4. import random
  5. from PIL import Image, ImageTk
  6.  
  7. root = Tk()
  8. root.title("Tk fractalic coast lines")
  9. xm, ym = 720, 720
  10.  
  11. canvas = Canvas(root, width=xm, height=ym)
  12. canvas.grid()
  13.  
  14. from PIL import Image, ImageDraw
  15. import random, math
  16.  
  17. pad = 20
  18. rnd = random.Random()
  19. iterations = 12
  20. x, y = 720, 720
  21. r = min(x,y)/2 * 0.82 # initial shape radius is 82% of image radius
  22.  
  23. while 1:
  24.     for gon in [2,3,4]:
  25.  
  26.         points = [(x/2+r*math.cos(2.*math.pi*i/gon),y/2+r*math.sin(2.*math.pi*i/gon)) for i in range(gon)]
  27.  
  28.         for i in range(3):
  29.             canvas.delete('all')
  30.             lines = [(points[i],points[(i+1)%gon]) for i in range(gon)]
  31.  
  32.             for i in range(iterations):
  33.                 newLines = []
  34.                 for l in lines:
  35.                     s = l[0]
  36.                     e = l[1]
  37.                     d = 0.32 * ((s[0]-e[0])**2 + (s[1]-e[1])**2)**0.5
  38.                     alpha = rnd.random() * 2 * math.pi
  39.                     mx = (s[0]+e[0])/2 + d * math.cos(alpha)
  40.                     my = (s[1]+e[1])/2 + d * math.sin(alpha)
  41.                     m = (mx, my)
  42.                     newLines += [(s,m),(m,e)]
  43.                 lines = newLines
  44.  
  45.             '''
  46.             for l in lines:
  47.                 canvas.create_line((l[0], l[1]), fill='blue')
  48.             '''
  49.            
  50.             img = Image.new("L", (x,y), 255)
  51.             canvas.create_polygon([l[0] for l in lines], fill='green')
  52.             root.update()
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement