Generate and draw a fractal tree.

 Draw the trunk
 At the end of the trunk, split by some angle and draw two branches
 Repeat at the end of each branch until a sufficient level of branching is reached


Related tasks
 Pythagoras Tree


----------------------------------------
-- Creates an image of a fractal tree
-- @param {integer} width
-- @param {integer} height
-- @param {integer} fractalDepth
-- @param {integer|float} initSize
-- @param {float} spreadAngle
-- @param {float} [scaleFactor=1.0]
-- @return {image}
----------------------------------------
on fractalTree (width, height, fractalDepth, initSize, spreadAngle, scaleFactor)
  if voidP(scaleFactor) then scaleFactor = 1.0
  img = image(width, height, 24)
  img.fill(img.rect, rgb(0,0,0))
  _drawTree(img, width/2, height, -PI/2, fractalDepth, initSize, spreadAngle, scaleFactor)
  return img
end
 
on _drawTree (img, x1, y1, angle, depth, size, spreadAngle, scaleFactor)
  if (depth) then
    x2 = x1 + cos(angle)*depth*size
    y2 = y1 + sin(angle)*depth*size
    img.draw(x1, y1, x2, y2, [#color:rgb(255,255,255)])
    _drawTree(img, x2, y2, angle-spreadAngle, depth-1, size*ScaleFactor, spreadAngle, scaleFactor)
    _drawTree(img, x2, y2, angle+spreadAngle, depth-1, size*ScaleFactor, spreadAngle, scaleFactor)
  end if
end