UserPreferences

LsystemTreeCode


  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
 34 
 35 
 36 
 37 
 38 
 39 
 40 
 41 
 42 
 43 
 44 
 45 
 46 
 47 
 48 
 49 
 50 
 51 
 52 
 53 
 54 
 55 
 56 
 57 
 58 
 59 
 60 
 61 
 62 
 63 
 64 
 65 
 66 
 67 
 68 
 69 
 70 
 71 
 72 
import Tkinter
import random
import math

class metamer:
    def __init__(self,base_x,base_y,angle,ax=None,ap=None):
        self.base_x = base_x
        self.base_y = base_y
        self.angle = angle
        self.ax = ax
        self.ap = ap
        self.drawn = 0

maxtime = 10
maxwidth = 700
maxheight = 700

window = Tkinter.Tk()
canvas = Tkinter.Canvas(window, width=maxwidth, height=maxheight, background='white')
canvas.pack()

def draw_tree(m):
    origin_x = maxwidth - maxwidth / 2
    origin_y = maxheight - maxheight / 3
    if not (m.drawn):
        canvas.create_line(origin_x - m.base_x, origin_y - m.base_y, origin_x - (m.base_x + (math.cos(m.angle)*internode_length)), origin_y - (m.base_y + (math.sin(m.angle)*internode_length)))
        m.drawn = 1
    print m.base_x, m.base_y, rad_to_deg(m.angle)
    if m.ax != None:
        draw_tree(m.ax)
    if m.ap != None:
        draw_tree(m.ap)

def rad_to_deg(rads):
    return 360.0 * rads / 2 / math.pi

def want_ax(m):
    return (random.random() < .3)

def want_ap(m):
    return (random.random() < .3)

internode_length = 10.0
divergence_angle = math.pi / 4.0

def do_metamer(m):
    if m.ax != None:
        do_metamer(m.ax)
    if m.ap != None:
        do_metamer(m.ap)
    new_base_x = m.base_x + (math.cos(m.angle)*internode_length)
    new_base_y = m.base_y + (math.sin(m.angle)*internode_length)
    if m.angle > math.pi/2.0:
        ang = m.angle - divergence_angle
    elif m.angle == math.pi/2.0:
        if random.random() < 0.5:
            ang = m.angle + divergence_angle
        else:
            ang = m.angle - divergence_angle
    else:
        ang = m.angle + divergence_angle
    if m.ax == None and want_ax(m):
        m.ax = metamer(new_base_x, new_base_y, ang)
    if m.ap == None and want_ap(m):
        m.ap = metamer(new_base_x, new_base_y, m.angle)

print 'Lsys 1'
base = metamer(0,0, (math.pi/2.0))
draw_tree(base)
for time in range(maxtime):
    do_metamer(base)
    draw_tree(base)