trees-0
2012. 05. 28.

A binary tree. I know many people have been drawing these trees for decades so I guess you've probably seen a similar one from somewhere already.

Anyway, a binary tree.

There are a number of parameters whose combination determines the final shape of the tree. Click on the screen to randomize them.

» launch trees-0

p5       = processing
branches = []
crooked  = 0
bent     = 0.25
bentr    = 1 / 7
benta    = 1
life     = $(window).height() * 0.05 * 0.75
lifer    = 0.9
thick    = 20
thicka   = 0.7

randomize = ->
  crooked = random(- HALF_PI / 8, HALF_PI / 8)
  bent    = HALF_PI * random(0.01, 0.5)
  bentr   = HALF_PI * random(0, 0.5)
  benta   = random(0.7, 1.2)
  life    = p5.height * 0.05 * random(0.5, 1)
  lifer   = random(0.8, 0.99)

mousePressed = ->
  branches = []
  randomize()
  branches.push new Branch(
                    p5.width / 2,
                    p5.height,
                    TWO_PI * 3 / 4,
                    bent,
                    thick,
                    life,
                    random(8, 13))
  background 255

class Branch
  constructor: (@x, @y, @r, @b, @w, l, lev) ->
    @v   = 4
    @t   = 0
    @l   = parseInt(l)
    @lev = parseInt(lev)

  live: ->
    if @alive()
      @px = @x
      @py = @y
      @x += @v * cos(@r) + random(-1, 1)
      @y += @v * sin(@r) + random(-1, 1)
      @t += 1

      if @t == @l and @l > 1 and @lev > 0
        @split()

  draw: ->
    strokeWeight max(@w, 1)
    line @px, @py, @x, @y

  split: ->
    lr = crooked - @b + random(- bentr, bentr)
    rr = crooked + @b + random(- bentr, bentr)
    branches.push new Branch(@x, @y, @r + lr,
                             @b * benta, @w * thicka,
                             @l * lifer, @lev - 1)
    branches.push new Branch(@x, @y, @r + rr,
                             @b * benta, @w * thicka,
                             @l * lifer, @lev - 1)

  alive: -> @t < @l

setup = ->
  size $(window).width(), $(window).height()
  branches.push new Branch(
                  p5.width / 2, p5.height,
                  TWO_PI * 3 / 4, bent, thick, life, 12)
  stroke 0
  background 255

draw = ->
  for b in branches
    b.live()
    b.draw()

  branches = for b in branches when b.alive()
    b
» capture | close