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


DEFINE MAXSIZE="12"
 
INT ARRAY SinTab=[
  0 4 9 13 18 22 27 31 36 40 44 49 53 58 62 66 71 75 79 83
  88 92 96 100 104 108 112 116 120 124 128 132 136 139 143
  147 150 154 158 161 165 168 171 175 178 181 184 187 190
  193 196 199 202 204 207 210 212 215 217 219 222 224 226
  228 230 232 234 236 237 239 241 242 243 245 246 247 248
  249 250 251 252 253 254 254 255 255 255 256 256 256 256]
 
INT ARRAY xStack(MAXSIZE),yStack(MAXSIZE),angleStack(MAXSIZE)
BYTE ARRAY lenStack(MAXSIZE),dirStack(MAXSIZE)
BYTE stacksize=[0]
 
INT FUNC Sin(INT a)
  WHILE a<0 DO a==+360 OD
  WHILE a>360 DO a==-360 OD
  IF a<=90 THEN
    RETURN (SinTab(a))
  ELSEIF a<=180 THEN
    RETURN (SinTab(180-a))
  ELSEIF a<=270 THEN
    RETURN (-SinTab(a-180))
  ELSE
    RETURN (-SinTab(360-a))
  FI
RETURN (0)
 
INT FUNC Cos(INT a)
RETURN (Sin(a-90))
 
BYTE FUNC IsEmpty()
  IF stacksize=0 THEN
    RETURN (1)
  FI
RETURN (0)
 
BYTE FUNC IsFull()
  IF stacksize=MAXSIZE THEN
    RETURN (1)
  FI
RETURN (0)
 
PROC Push(INT x,y,angle BYTE len,dir)
  IF IsFull() THEN Break() FI
  xStack(stacksize)=x yStack(stacksize)=y
  angleStack(stacksize)=angle lenStack(stacksize)=len
  dirStack(stacksize)=dir
  stacksize==+1
RETURN
 
PROC Pop(INT POINTER x,y,angle BYTE POINTER len,dir)
  IF IsEmpty() THEN Break() FI
  stacksize==-1
  x^=xStack(stacksize) y^=yStack(stacksize)
  angle^=angleStack(stacksize) len^=lenStack(stacksize)
  dir^=dirStack(stacksize)
RETURN
 
PROC DrawTree(INT x,y,len,angle,leftAngle,rightAngle)
  BYTE depth,dir
 
  Plot(x,y)
  x==+Cos(angle)*len/256
  y==-Sin(angle)*len/256
  DrawTo(x,y)
 
  Push(x,y,angle,len,0)
 
  WHILE IsEmpty()=0
  DO
    Pop(@x,@y,@angle,@len,@dir)
    IF dir<2 THEN
      Push(x,y,angle,len,dir+1)
      IF dir=0 THEN
        angle==-leftAngle
      ELSE
        angle==+rightAngle
      FI
 
      len=13*len/16
      Plot(x,y)
      x==+Cos(angle)*len/256
      y==-Sin(angle)*len/256
      DrawTo(x,y)
 
      IF IsFull()=0 THEN
        Push(x,y,angle,len,0)
      FI
    FI
  OD
 
RETURN
 
PROC Main()
  BYTE CH=$02FC,COLOR1=$02C5,COLOR2=$02C6
 
  Graphics(8+16)
  Color=1
  COLOR1=$BA
  COLOR2=$B2
 
  DrawTree(140,191,40,110,35,15)
 
  DO UNTIL CH#$FF OD
  CH=$FF
RETURN