Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_Random_Island.py
- from Tkinter import *
- from PIL import Image, ImageTk
- import random
- import math
- import time
- ww = 600
- hh = 600
- ww2 = ww/2
- hh2 = hh/2
- root = Tk()
- root.title("Tk Random Island")
- root.geometry("%dx%d+-10+0"%(ww,hh))
- canvas = Canvas(root, width=ww, height=hh, bg='royal blue')
- canvas.pack()
- def draw_line(x1,y1,x2,y2): # function to draw line
- xy.append((ww2+x2,hh2+y2))
- def dist(p1,p2): # Euclidean distance betwen p1 and p2
- return ((p1[0]-p2[0])**2 + (p1[1]-p2[1])**2)**0.5
- def shoreline(x1,y1,x2,y2,ratio): # recurisve function to draw the shoreline
- L = dist((x1,y1),(x2,y2))
- if L <= 1: # distance is short enough, directly draw the line
- draw_line(x1,y1,x2,y2)
- return
- rs = ratio + random.uniform(-0.1,0.1) # let ratio flucuate slightly around the chosen value
- rs = max(0.5,rs) # make sure ratio stays at least half of the length
- midx = (x1+x2)/2 # center of ellipse
- midy = (y1+y2)/2
- rx = L/2 + (2*rs-1)/2*L # width of ellipse
- ry = ((L*rs)**2 - (L/2)**2)**0.5 # height of ellipse
- theta = math.atan2(y2-y1,x2-x1) # the tilt angle of ellipse
- alpha = random.uniform(math.pi*0.3,math.pi*0.7) # flucuate around math.pi/2
- x3 = rx*math.cos(alpha)*math.cos(theta) - ry*math.sin(alpha)*math.sin(theta) + midx # parametric equation for ellipse
- y3 = rx*math.cos(alpha)*math.sin(theta) + ry*math.sin(alpha)*math.cos(theta) + midy
- shoreline(x1,y1,x3,y3,ratio) # do this recursively on each segment
- shoreline(x3,y3,x2,y2,ratio)
- while 1:
- xy = []
- canvas.delete('all')
- shoreline(-200,0,200,0,0.55) # call recursion
- shoreline(200,0,-200,0,0.55) # call recursion
- canvas.create_polygon(xy, fill="forest green", outline="tan", width=5)
- canvas.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement