here2share

# tk_fractalic_coast_lines.py

Jun 13th, 2021 (edited)
2,050
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.11 KB | None | 0 0
  1. # tk_fractalic_coast_lines.py
  2.  
  3. from tkinter import *
  4. import random, math
  5. from PIL import Image, ImageTk, ImageDraw
  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. pad = 20
  15. rnd = random.Random()
  16. iterations = 12
  17. x, y = 720, 720
  18. r = min(x,y)/2 * 0.82 # initial shape radius is 82% of image radius
  19.  
  20. while 1:
  21.     for gon in [2,3,4]:
  22.  
  23.         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)]
  24.  
  25.         for i in range(3):
  26.             canvas.delete('all')
  27.             lines = [(points[i],points[(i+1)%gon]) for i in range(gon)]
  28.  
  29.             for i in range(iterations):
  30.                 newLines = []
  31.                 for l in lines:
  32.                     s = l[0]
  33.                     e = l[1]
  34.                     d = 0.32 * ((s[0]-e[0])**2 + (s[1]-e[1])**2)**0.5
  35.                     alpha = rnd.random() * 2 * math.pi
  36.                     mx = (s[0]+e[0])/2 + d * math.cos(alpha)
  37.                     my = (s[1]+e[1])/2 + d * math.sin(alpha)
  38.                     m = (mx, my)
  39.                     newLines += [(s,m),(m,e)]
  40.                 lines = newLines
  41.            
  42.             img = Image.new("L", (x,y), 255)
  43.             canvas.create_polygon([l[0] for l in lines], fill='green')
  44.             root.update()
  45.  
Advertisement
Add Comment
Please, Sign In to add comment