I'm working on modernizing Rosetta Code's infrastructure. Starting with communications. Please accept this time-limited open invite to RC's Slack.. --Michael Mol (talk) 20:59, 30 May 2020 (UTC)

# Fractal tree

Fractal tree
You are encouraged to solve this task according to the task description, using any language you may know.

Generate and draw a fractal tree.

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

## 11l

Translation of: Nim
-V   Width = 1000   Height = 1000   TrunkLength = 400   ScaleFactor = 0.6   StartingAngle = 1.5 * math:pi   DeltaAngle = 0.2 * math:pi F drawTree(outfile, Float x, Float y; len, theta) -> N   I len >= 1      V x2 = x + len * cos(theta)      V y2 = y + len * sin(theta)      outfile.write("<line x1='#.6' y1='#.6' x2='#.6' y2='#.6' style='stroke:white;stroke-width:1'/>\n".format(x, y, x2, y2))      drawTree(outfile, x2, y2, len * ScaleFactor, theta + DeltaAngle)      drawTree(outfile, x2, y2, len * ScaleFactor, theta - DeltaAngle) V outsvg = File(‘tree.svg’, ‘w’)outsvg.write(|‘<?xml version='1.0' encoding='utf-8' standalone='no'?>               <!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.1//EN' 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'>               <svg width='100%%' height='100%%' version='1.1' xmlns='http://www.w3.org/2000/svg'>               <rect width="100%" height="100%" fill="black"/>               ’)drawTree(outsvg, 0.5 * Width, Height, TrunkLength, StartingAngle)outsvg.write("</svg>\n")

## Action!

Action! language does not support recursion. Therefore an iterative approach with a stack has been proposed.

## Julia

Translation of: F#
 const width = height = 1000.0const trunklength = 400.0const scalefactor = 0.6const startingangle = 1.5 * piconst deltaangle = 0.2 * pi function tree(fh, x, y, len, theta)   if len >= 1.0       x2 = x + len * cos(theta)       y2 = y + len * sin(theta)       write(fh, "<line x1='$x' y1='$y' x2='$x2' y2='$y2' style='stroke:rgb(0,0,0);stroke-width:1'/>\n")       tree(fh, x2, y2, len * scalefactor, theta + deltaangle)       tree(fh, x2, y2, len * scalefactor, theta - deltaangle)    endend outsvg = open("tree.svg", "w")write(outsvg,     """<?xml version='1.0' encoding='utf-8' standalone='no'?>    <!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.1//EN'    'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'>    <svg width='100%%' height='100%%' version='1.1'    xmlns='http://www.w3.org/2000/svg'>\n""") tree(outsvg, 0.5 * width, height, trunklength, startingangle) write(outsvg, "</svg>\n") # view file tree.svg in browser

## Kotlin

Translation of: Java
// version 1.1.2 import java.awt.Colorimport java.awt.Graphicsimport javax.swing.JFrame class FractalTree : JFrame("Fractal Tree") {    init {        background = Color.black        setBounds(100, 100, 800, 600)        isResizable = false        defaultCloseOperation = EXIT_ON_CLOSE    }     private fun drawTree(g: Graphics, x1: Int, y1: Int, angle: Double, depth: Int) {        if (depth == 0) return        val x2 = x1 + (Math.cos(Math.toRadians(angle)) * depth * 10.0).toInt()        val y2 = y1 + (Math.sin(Math.toRadians(angle)) * depth * 10.0).toInt()        g.drawLine(x1, y1, x2, y2)        drawTree(g, x2, y2, angle - 20, depth - 1)        drawTree(g, x2, y2, angle + 20, depth - 1)    }     override fun paint(g: Graphics) {        g.color = Color.white        drawTree(g, 400, 500, -90.0, 9)    }} fun main(args: Array<String>) {    FractalTree().isVisible = true}

## Lambdatalk

 1) defining the function tree: {def tree  {lambda {:e     // last branch length          :s     // trunks length          :k     // ratio between two following branches          :a     // rotate left          :b}    // rotate right  {if {< :s :e}   then    else M:s T:a        {tree :e {* :k :s} :k :a :b}        T-{+ :a :b}        {tree :e {* :k :s} :k :a :b}        T:b M-:s }}} 2) Calling this function generates a sequence of commands mooving a pen:• Tθ rotates the drawing direction "θ" degrees from the previous one• and Md draws a segment "d" pixels in this direction. {def T {tree 1 190 {/ 2 3} 15 45}} and produces 40995 words beginning with: M190 T15 M126.66666666666666 T15 M84.44444444444443 T15 M56.29629629629628 T15 M37.53086419753085 T15 M25.020576131687235 T15 M16.680384087791488 T15 M11.120256058527659 T15 M7.413504039018439 T15 M4.942336026012292 T15 M3.2948906840081946 ... 3) These words are sent to a the turtle lambdatalk primitive which is a graphic device translating the sequence of Md and Tθ into a sequence of SVG points x0 y0 x1 y1 ... xn yn which will feed the points attribute of a polyline SVG element: {svg {@ width="580px" height="580px" style="box-shadow:0 0 8px #000;"}  {polyline   {@ points="{turtle 230 570 180 {T}}"      fill="transparent" stroke="#fff" stroke-width="1"}}} This is an abstract of the output: <svg width="580px" height="580px" style="box-shadow:0 0 8px #000;">  <polyline points="230 580 230 380 195 251 151 174 109 132 75 113 49 106 32 106 21 109 ...                      ... 413 286 324 286 230 380 230 580 "            fill="transparent" stroke="#888" stroke-width="1">  </polyline></svg> The complete ouput can be seen displayed in http://lambdaway.free.fr/lambdawalks/?view=fractal_tree

## Liberty BASIC

LB includes Logo-type turtle commands, so can be drawn that way as well as that shown here.

  NoMainWinsw = 640 :   sh = 480WindowWidth  = sw+8 : WindowHeight = sh+31UpperLeftX = (DisplayWidth -sw)/2UpperLeftY = (DisplayHeight-sh)/2Open"Fractal Tree" For Graphics_nf_nsb As #g#g "Down; Color darkgreen; TrapClose halt"h$= "#g" 'initial assignmentsinitAngle = Acs(-1)*1.5 'radian equivalent of 270 degrees theta = 29 * (Acs(-1)/180) 'convert 29 degrees to radians length = 110 'length in pixels depth = 25 'max recursion depth 'draw the tree Call tree h$, 320, 470, initAngle, theta, length, depth    #g "Flush; when leftButtonDown halt" 'L-click to exit    Wait Sub halt handle$Close #handle$    EndEnd Sub Sub tree h$, x, y, initAngle, theta, length, depth Scan newX = Cos(initAngle) * length + x newY = Sin(initAngle) * length + y #h$ "Line ";x;" ";y;" ";newX;" ";newY    length = length * .78    depth = depth - 1    If depth > 0 Then        Call tree h$, newX, newY, initAngle-theta, theta, length, depth Call tree h$, newX, newY, initAngle+theta, theta, length, depth    End IfEnd Sub

## Lingo

------------------------------------------ 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 imgend 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 ifend

Usage:

fractalDepth = 10initSize = 7.0spreadAngle = 35*PI/180scaleFactor = 0.95img = fractalTree(480, 380, fractalDepth, initSize, spreadAngle, scaleFactor)

## Logo

to tree :depth :length :scale :angle  if :depth=0 [stop]  setpensize round :depth/2  forward :length  right :angle  tree :depth-1 :length*:scale :scale :angle  left 2*:angle  tree :depth-1 :length*:scale :scale :angle  right :angle  back :lengthend clearscreentree 10 80 0.7 30

## Lua

### Bitmap

Needs LÖVE 2D Engine

 g, angle = love.graphics, 26 * math.pi / 180wid, hei = g.getWidth(), g.getHeight()function rotate( x, y, a )  local s, c = math.sin( a ), math.cos( a )  local a, b = x * c - y * s, x * s + y * c  return a, bendfunction branches( a, b, len, ang, dir )  len = len * .76  if len < 5 then return end  g.setColor( len * 16, 255 - 2 * len , 0 )  if dir > 0 then ang = ang - angle  else ang = ang + angle   end  local vx, vy = rotate( 0, len, ang )  vx = a + vx; vy = b - vy  g.line( a, b, vx, vy )  branches( vx, vy, len, ang, 1 )  branches( vx, vy, len, ang, 0 )endfunction createTree()  local lineLen = 127  local a, b = wid / 2, hei - lineLen  g.setColor( 160, 40 , 0 )  g.line( wid / 2, hei, a, b )  branches( a, b, lineLen, 0, 1 )   branches( a, b, lineLen, 0, 0 )endfunction love.load()  canvas = g.newCanvas( wid, hei )  g.setCanvas( canvas )  createTree()  g.setCanvas()endfunction love.draw()  g.draw( canvas )end

### ASCII

Using the Bitmap class and text renderer from here, then extending...

function Bitmap:tree(x, y, angle, depth, forkfn, lengfn)  if depth <= 0 then return end  local fork, leng = forkfn(), lengfn()  local x2 = x + depth * leng * math.cos(angle)  local y2 = y - depth * leng * math.sin(angle)  self:line(math.floor(x), math.floor(y), math.floor(x2), math.floor(y2))  self:tree(x2, y2, angle+fork, depth-1, forkfn, lengfn)  self:tree(x2, y2, angle-fork, depth-1, forkfn, lengfn)end bitmap = Bitmap(128*3,128)bitmap:tree( 64, 120, math.pi/2, 8, function() return 0.3 end, function() return 3 end)bitmap:tree(192, 120, math.pi/2, 8, function() return 0.6 end, function() return 2.5 end)bitmap:tree(320, 120, math.pi/2, 8, function() return 0.2+math.random()*0.3 end, function() return 2.0+math.random()*2.0 end)bitmap:render({[0x000000]='.', [0xFFFFFFFF]='█'})
Output:

Shown at 25% scale:

................................................................................................................................................................................................................................................................................................................................................................................................
................................................................................................................................................................................................................................................................................................................................................................................................
................................................................................................................................................................................................................................................................................................................................................................................................
................................................................................................................................................................................................................................................................................................................................................................................................
................................................................................................................................................................................................................................................................................................................................................................................................
................................................................................................................................................................................................................................................................................................................................................................................................
................................................................................................................................................................................................................................................................................................................................................................................................
................................................................................................................................................................................................................................................................................................................................................................................................
................................................................................................................................................................................................................................................................................................................................................................................................
................................................................................................................................................................................................................................................................................................................................................................................................
................................................................................................................................................................................................................................................................................................................................................................................................
................................................................................................................................................................................................................................................................................................................................................................................................
................................................................................................................................................................................................................................................................................................................................................................................................
....................................................................................................................................................................................................................................................................................................█.....██.█.█................................................................................
.................................................█.█...██.█.█.████.█.█.██...█.█...................................................................................................................................................................................................................██.█..█.█.██.███..............................................................................
..............................................█...█....██.███..██..███.██....█...█..................................................................................................................................................................................................................██...█...█.█................................................................................
...........................................███.█..█..█████.█.█.██.█.█.█████..█..█.███................................................................................................................................................................................................................█...█...██.................................................................................
.......................................█.█.██.██..█.█..██.███..██..███.██..█.█..██.██.█.█.............................................................................................................................................................................................................█..█...██.................................................................................
....................................█.█.█..█.█.█..█.█...█..█...██...█..█...█.█..█.█.█..█.█.█..........................................................................................................................................................................................................█..█...██.................................................................................
................................█.██.█.██..█.█..█████...█████..██..█████...█████..█.█..██.█.██.█.......................................................................................................................................................................................................█.█...█..................................................................................
.............................█...█.███..█..█.█.████.█...████.█.██.█.████...█.████.█.█..█..███.█...█...................................................................................................................................................................................█..█.█........█..███...█..............██....█......█..█...█...............................................
............................█.█..██.██..██.█.██.███..█..████..████..████..█..███.██.█.██..██.██..█.█.................................................................................................................................................................................███.██.█..█.....██.██...█........█.....█..█.███.....███....█.█....█........................................
.............................██..█.██.█.██.█.█.██.█...█.█..█..████..█..█.█...█.██.█.█.██.█.██.█..██....................................................................................................................................................................................█.████.██..█..█..███..█.........█....█...█..██....███..█.██..█.█.........................................
.........................█....█..█..█.█.█.██.█.██.█....██..█...██...█..██....█.██.█.██.█.█.█..█..█....█.................................................................................................................................................................................██..█..█...███..██...█.......███....█...█..█.█...█.█..████...█...█......................................
.......................█.█.█...███...█.██..████..██....██..█...██...█..██....██..████..██.█...███...█.█.█........................................................................................................................................................................██.....██████.█...█.█..█.█..█.........██...█...█..███...█.██.███....█..███.....................................
......................█.███...█████..█..█..█████.█.█...██...█..██..█...██...█.█.█████..█..█..█████...███.█.......................................................................................................................................................................█......██...███...█.█..█.█..█..........█...█...█..█..█.█.█.███.█...██..█.......................................
....................██.█.██.....██.██.█.█..█.█..████...██...█..██..█...██...████..█.█..█.█.██.██.....██.█.██.....................................................................................................................................................................█.......█....███..█..██..█.█............█.█....█.█....██..████.█...███.█..........█............................
.....................███..█......█...████...██....███.█..█...█.██.█...█..█.███....██...████...█......█..███...............................................................................................................................................................█.█....█..█....█.....██.█...██...██............█.█....██.....██.█████.█...█.██............█.█....█....................
.................█..████..██.....█.....███..█......██.█..█...█.██.█...█..█.██......█..███.....█.....██..████..█............................................................................................................................................................█.█...█.███...█......█.█...██...████...........██....██....███.█.██.██..█..██............██....███...................
................█.█....████.█....█.......██.██......███..█...██..██...█..███......██.██.......█....█.████....█.█...................................................................................................................................................█.....██████..█....██..█.....███...█....██.██...........█....█..██.████..█.███..█..██.............█...█......................
.................██.....█.███.....█.......████.......█.█.█....█..█....█.█.█.......████.......█.....███.█.....██.....................................................................................................................................................█......█..██.███....███......██...█....██..█...........█....█....██.██..█..██.██..█..............█..█....█..................
..................█......██..██...█.......█.██.......█..█.█...█..█...█.█..█.......██.█.......█...██..██......█.......................................................................................................................................................█......█..███..██....██.....█.█..█.....█..█..........██...█......█.██..█.█████.█.██.............█.█....█.███...............
..............█....█......█....██..█......██.██......█...██...█..█...██...█......██.██......█..██....█......█....█................................................................................................................................................███████.███...███████████████..█.█..█.....█...█......█.█.█...█......█.█.██..█.████..██............█.█.█...██..................
.............███....█.....█......█.█.......█..█......█....█...█..█...█....█......█..█.......█.█......█.....█....███......................................................................................................................................................█████...█.........█...█████.█......█...█....██.██..█..█.......██.██.██.██████.█.█..........██.██..█....................
...........██.█.███..█....█.......██.......█...█.....█.....█..█..█..█.....█.....█...█.......██.......█....█..███.█.██....................................................................................................................................................████████.█........█......████......██...█....██.█..█..█.......█..█████.███..█.██...........█..█..█..█...██.............
.............██....███.....█.......██......█...█.....█.....█..█..█..█.....█.....█...█......██.......█.....███....██...........................................................................................................................................................█..█████.....█........██.......██...█....█.█..█.█........█.███.█.██.█..█.█......█....█...█..███...█...............
..............█.......██...█........█......█....█....█......█.█..█.█......█....█....█......█........█...██.......█.......................................................█..█....█..█..█..█..█..█.█..█..█..█..█....█..█...........................................█.█..........█...█..███...█........█......███████.....█.█.█.█........██..█.██.█..████......█.....█...████████████.............
.........██....█........██..█........█.....█.....█...█.......██..██.......█...█.....█.....█........█..██........█....██...................................................██......██....██....██.█....██....██......██.............................................██...........█..█.....████........███.....█....███....██.█.█........█...███.████████...█████...█...██..█....██...............
...........█...█..........█.█........█.....█......█..█........█..█........█..█......█.....█........█.█..........█...█.....................................................█........█....█.....█..█.....█....█........█..............................................█............█.█........█........█..█....█.......██...█..█........██...█████....██████........█.██.█.█.....█................
.........██████.█..........██.........█....█......█..█........█..█........█..█......█....█.........██..........█.██████...................................................█........█....█.....█..█.....█....█........█.......................................█.......█............█.█........█.......█...█...█.........█...█.█........█.████...█....██...........███...██.....█.................
...............███..........██.........█...█.......█.█........████........█.█.......█...█.........██..........███...................................................█.....█.......██....██....█..█....██....██.......█.....█................................███..██...█............██.........█......█....█..█..........█..█.█.......█.██.██...█...██............█.....██.....█....█............
.................██..........█..........█..█........██........████........██........█..█..........█..........██......................................................█....█........█....██....█..█....██....█........█....█....................................██..████.............██.........█.....█.....█..█..........██.██......███.██.█...█..█.█...........█......█.....█....█.............
.......██..........█..........█.........█..█........██........████........██........█..█.........█..........█..........██..........................................█████..█......███████████..█..█..███████████......█..█████...................................███...███............█.........█.....█......█.█............███......█...██.█...█.█...█.........█.......█....█.████..............
.........█..........█.........█..........█.█.........█........█..█........█.........█.█..........█.........█..........█.........................................█.█.....███........██████...███..███...██████........███.....█.█...................................██....██...........█.........█.....█......██.............██....██....█..█...██....█........█.......█....███....███...........
.......██████........█.........█..........██.........█........█..█........█.........██..........█.........█........██████........................................█........█........███..█.....█..█.....█..███........█........█......................................█.....██..........█.........█....█.......█.............██..██......█..█...█.....█.......█........█...█.....................
.............██.......█........█..........██..........█......█....█......█..........██..........█........█.......██..............................................█.........█......█..█...█.....██.....█...█..█......█.........█.......................................██████████.......█..........█...█........█............█.██.......█...█..█......█......█.........█..█......................
...............██████..█........█..........█..........█......█....█......█..........█..........█........█..██████..........................................█..█..█.█..█.....█..█..█..█....█....██....█....█..█..█..█.....█..█.█..█..█...............................██..........███.....█..........█..█........█............█.██.......█...█..█......█.....█..........█.█.......................
......███...███......██████......█.........█..........█......█....█......█..........█.........█......██████......███...███..................................██...█..██.......█..██...█.....█...██...█.....█...██..█.......██..█...██..........................██████...............██....█..........█.█.........█...........██.█.......█...█.█.......█....█..........█.█........................
.........███...............███...█..........█.........█.....█......█.....█.........█..........█...███...............███.....................................███..█...█.......█..███..█.....█..█..█..█.....█..███..█.......█...█..███.............................█...................███..█.........█.█.........█...........█...█......█...█.█........█..█...........██.........................
.......██.....................██..█.........█..........█....█......█....█..........█.........█..██.....................██.............................█...███..███...█.....█..███..███......█.█..█.█......███..███..█.....█...███..███...█......................█.......................███..........██..........█.........█.....█....█....██.........█.█............█.............█............
................................███.........█..........█...█........█...█..........█.........███.......................................................█....█....█...█....█....█.....█.......█....█.......█.....█....█....█...█....█....█......................█...........................██.........█..........█.........█......█...█....█..........██............█.............█.............
...................................██.......█..........█...█........█...█..........█.......██...........................................................██..█.....█..█..██.....█......█......█....█......█......█.....██..█..█.....█..██.....................................................██.......█...........█.......█........█..█....█..........█............█.............██████.........
.....................................█.......█..........█..█........█..█..........█.......█.......................................................█.████..███.....█..███..███..█......█......█....█......█......█..███..███..█.....███..████.█.................................................█.......█..........█.......█........█.█.....█..........█............█............██..█...........
......................................█......█..........█.█..........█.█..........█......█.........................................................█........█......█.█.........█.......█.....█....█.....█.......█.........█.█......█........█...................................................█......█...........█.....█..........██.....█.........█............█............█.███████........
.......................................██....█..........█.█..........█.█..........█....██..........................................................█....█....█......█.......█..█........█....█....█....█........█..█.......█......█....█....█....................................................██.....█..........█.....█...........█....█..........█...........█............███...............
.........................................█...█..........█.█..........█.█..........█...█............................................................█.....█....█.....█........█.█........█....█....█....█........█.█........█.....█....█.....█......................................................█....█...........█...█............█....█..........█..........█...........██..................
..........................................█...█..........█............█..........█...█.......................................................█.....█.....███...█...█.███.....███.........███.█....█.███.........███.....███.█...█...███.....█.....█.................................................█...█...........█...█.............█...█.........█...........█.........██....................
...........................................█..█..........█............█..........█..█.........................................................█....█...██...██.█.████...██.██..███...████...██....██...████...███..██.██...████.█.██...██...█....█...................................................██..█...........█.█..............█...█.........█..........█........██......................
............................................█.█..........█............█..........█.█........................................................██████.█..........███.█.......███..█..███........██████........███..█..███.......█.███..........█.██████...................................................█.█...........█.█..............█...█........█..........█........█................██......
.............................................█.█.........█............█.........█.█...............................................................██.........█...█...........███.█...........██████...........█.███...........█...█.........██..........................................................██............█...............█..█.........█.........█.....█████...............█........
..............................................██.........█............█.........██..................................................................█........█...█.............██............█....█............██.............█...█........█.............................................................██...........█...............█..█.........█.........██████.....████......██████........
...............................................█.........█............█.........█...................................................................█.......█....█.............██...........█......█...........██.............█....█.......█..............................................................█...........█................█.█........█........███..............██████......███.....
................................................█........█............█........█.....................................................................█..████.....█..........███.█...........█......█...........█.███..........█.....████..█................................................................█..........█................█.█........█......██.....................██..............
................................................█........█............█........█.....................................................................█.....█.....█............█..█.........█........█.........█..█............█.....█.....█.................................................................█.........█................█.█........█....██.........................███...........
.................................................█.......█............█.......█...........................................................█...........█...█......█............█...█.......█..........█.......█...█............█......█...█...........█......................................................█.........█................█.█.......█....█..............................███........
..................................................█......█............█......█.............................................................█...........█.........█.................█.....█............█.....█.................█.........█...........█........................................................█........█.................█........█..██................................█.........
...................................................█.....█............█.....█............................................................███████.....█████.......█.................█.....█............█.....█.................█.......█████.....███████.......................................................█.......█.................█........███............................................
...................................................█.....█............█.....█................................................................████████.....███....█..................█...█..............█...█..................█....███.....████████............................................................█......█.................█.......██..............................................
....................................................█....█............█....█..................................................................██.............█████...................█.█................█.█...................█████.............██..............................................................█......█................█.......█...............................................
.....................................................█...█............█...█.............................................................█.....██.................█...................█.█................█.█...................█.................██.....█.........................................................█.....█................█......█................................................
.....................................................█...█............█...█..............................................................█....█...................█...................█..................█...................█...................█....█..........................................................█.....█................█.....█.................................................
......................................................█..█............█..█.............................................................█████.██...................█...................█..................█...................█...................██.█████.........................................................█....█................█.....█.................................................
.......................................................█.█............█.█.................................................................█████....................█..................█..................█..................█....................█████.............................................................█...█................█....█..................................................
........................................................██............██.....................................................................██.....................█.................█..................█.................█.....................██.................................................................█..█................█...█...................................................
........................................................██............██....................................................................█..█....................█.................█..................█.................█....................█..█.................................................................█.█................█...█...................................................
.........................................................█............█........................................................................█.....................█................█..................█................█.....................█....................................................................█.█................█..█....................................................
.........................................................█............█.........................................................................█.....................█...............█..................█...............█.....................█......................................................................██................█.█.....................................................
..........................................................█..........█...............................................................█...........█....................█...............█..................█...............█....................█...........█............................................................█................██......................................................
..........................................................█..........█................................................................█..........█.....................█..............█..................█..............█.....................█..........█.............................................................█................██......................................................
..........................................................█..........█..............................................................█████.........███..................█..............█..................█..............█..................███.........█████............................................................█...............█.......................................................
...........................................................█.........█...................................................................██...████...███..............█████...........█..................█...........█████..............███...████...██.................................................................█...............█.......................................................
...........................................................█........█......................................................................███..........███.....██████.....███........█..................█........███.....██████.....███..........███...................................................................█..............█........................................................
...........................................................█........█.....................................................................█................█████..............██......█..................█......██..............█████................█...................................................................█.............█........................................................
............................................................█.......█....................................................................█................█.....................███...█..................█...███.....................█................█..................................................................█.............█........................................................
............................................................█......█...................................................................██.................█........................████..................████........................█.................██.................................................................█...........█.........................................................
............................................................█......█.................................................................███.................█............................█..................█............................█.................███...............................................................█...........█.........................................................
.............................................................█.....█...................................................................█................█..............................█................█..............................█................█.................................................................█...........█.........................................................
.............................................................█.....█...................................................................█...............█...............................█................█...............................█...............█..................................................................█.........█..........................................................
.............................................................█....█....................................................................█...............█................................█..............█................................█...............█..................................................................█.........█..........................................................
..............................................................█...█.....................................................................█.............█..................................█.............█.................................█.............█...................................................................█........█...........................................................
..............................................................█...█...................................................................█████.........██...................................█............█...................................██.........█████..................................................................█.......█...........................................................
..............................................................█..█.........................................................................██...████.█....................................█..........█....................................█.████...██.......................................................................█.......█...........................................................
...............................................................█.█...........................................................................███.....█.....................................█.........█....................................█.....███.........................................................................█......█............................................................
...............................................................█.█..........................................................................█........█.....................................█........█.....................................█........█.........................................................................█.....█............................................................
...............................................................█.█.........................................................................█.........█......................................█.......█.....................................█.........█........................................................................█.....█............................................................
................................................................█.........................................................................█.........█........................................█.....█.......................................█.........█........................................................................█...█.............................................................
................................................................█.......................................................................███.........█........................................█....█........................................█.........███......................................................................█...█.............................................................
................................................................█.........................................................................█.........█.........................................█...█........................................█.........█........................................................................█...█.............................................................
................................................................█.........................................................................█........██..........................................█.█.........................................██........█.........................................................................█.█..............................................................
................................................................█.................................................................................█..█.........................................█.█........................................█..█.................................................................................█.█..............................................................
................................................................█................................................................................█...█..........................................█.........................................█...█................................................................................█.█..............................................................
................................................................█...............................................................................█....█..........................................█.........................................█....█................................................................................█...............................................................
................................................................█.............................................................................███.....█.........................................█........................................█.....███..............................................................................█...............................................................
................................................................█...............................................................................█.....███.......................................█......................................███.....█................................................................................█...............................................................
................................................................█.....................................................................................█.........................................█........................................█......................................................................................█...............................................................
................................................................█...............................................................................................................................█...............................................................................................................................█...............................................................
................................................................█...............................................................................................................................█...............................................................................................................................█...............................................................
................................................................█...............................................................................................................................█...............................................................................................................................█...............................................................
................................................................█...............................................................................................................................█...............................................................................................................................█...............................................................
................................................................█...............................................................................................................................█...............................................................................................................................█...............................................................
................................................................█...............................................................................................................................█...............................................................................................................................█...............................................................
................................................................█...............................................................................................................................█...............................................................................................................................█...............................................................
................................................................█...............................................................................................................................█...............................................................................................................................█...............................................................
................................................................█...............................................................................................................................█...............................................................................................................................█...............................................................
................................................................█...............................................................................................................................█...............................................................................................................................█...............................................................
................................................................█...............................................................................................................................█...............................................................................................................................█...............................................................
................................................................█...............................................................................................................................█...............................................................................................................................█...............................................................
................................................................█...............................................................................................................................█...............................................................................................................................█...............................................................
................................................................█...............................................................................................................................█...............................................................................................................................█...............................................................
................................................................█...............................................................................................................................█...............................................................................................................................█...............................................................
................................................................█...............................................................................................................................█...............................................................................................................................█...............................................................
................................................................................................................................................................................................................................................................................................................................................................................................
................................................................................................................................................................................................................................................................................................................................................................................................
................................................................................................................................................................................................................................................................................................................................................................................................
................................................................................................................................................................................................................................................................................................................................................................................................
................................................................................................................................................................................................................................................................................................................................................................................................
................................................................................................................................................................................................................................................................................................................................................................................................
................................................................................................................................................................................................................................................................................................................................................................................................

## Mathematica / Wolfram Language

fractalTree[  pt : {_, _}, \[Theta]orient_: \[Pi]/2, \[Theta]sep_: \[Pi]/9,   depth_Integer: 9] := Module[{pt2},  If[depth == 0, Return[]];  pt2 = pt + {Cos[\[Theta]orient], Sin[\[Theta]orient]}*depth;  DeleteCases[   [email protected]{     Line[{pt, pt2}],     fractalTree[pt2, \[Theta]orient - \[Theta]sep, \[Theta]sep,       depth - 1],     fractalTree[pt2, \[Theta]orient + \[Theta]sep, \[Theta]sep,       depth - 1]     },   Null   ]  ]Graphics[fractalTree[{0, 0}, \[Pi]/2, \[Pi]/9]]

## NetRexx

Translation of: Java
Library: Swing
Library: AWT
/* NetRexx */options replace format comments java crossref symbols binary import java.awt.Colorimport java.awt.Graphicsimport javax.swing.JFrame class RFractalTree public extends JFrame  properties constant    isTrue  = (1 == 1)    isFalse = \isTrue  -- ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~  method RFractalTree() public    super('Fractal Tree')    setBounds(100, 100, 800, 600)    setResizable(isFalse)    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)    return  -- ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~  method drawTree(g = Graphics, x1 = int, y1 = int, angle = double, depth = int) private    if depth \= 0 then do      x2 = x1 + (int Math.cos(Math.toRadians(angle)) * depth * 10.0)      y2 = y1 + (int Math.sin(Math.toRadians(angle)) * depth * 10.0)      g.drawLine(x1, y1, x2, y2)      drawTree(g, x2, y2, angle - 20, depth - 1)      drawTree(g, x2, y2, angle + 20, depth - 1)      end    return  -- ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~  method paint(g = Graphics) public    g.setColor(Color.BLACK)    drawTree(g, 400, 500, -90, 9)    return  -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  method main(args = String[])public static    RFractalTree().setVisible(isTrue)    return

## Nim

Translation of: Julia
 import mathimport strformat const  Width = 1000  Height = 1000  TrunkLength = 400  ScaleFactor = 0.6  StartingAngle = 1.5 * PI  DeltaAngle = 0.2 * PI proc drawTree(outfile: File; x, y, len, theta: float) =  if len >= 1:    let x2 = x + len * cos(theta)    let y2 = y + len * sin(theta)    outfile.write(      fmt"<line x1='{x}' y1='{y}' x2='{x2}' y2='{y2}' style='stroke:white;stroke-width:1'/>\n")    outfile.drawTree(x2, y2, len * ScaleFactor, theta + DeltaAngle)    outFile.drawTree(x2, y2, len * ScaleFactor, theta - DeltaAngle) let outsvg = open("tree.svg", fmWrite)outsvg.write("""<?xml version='1.0' encoding='utf-8' standalone='no'?>                <!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.1//EN'                'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'>                <svg width='100%%' height='100%%' version='1.1'                xmlns='http://www.w3.org/2000/svg'>\n                <rect width="100%" height="100%" fill="black"/>\n""") outsvg.drawTree(0.5 * Width, Height, TrunkLength, StartingAngle)outsvg.write("</svg>\n")   # View file tree.svg in browser.

## OCaml

Library: ocaml-cairo
#directory "+cairo"#load "bigarray.cma"#load "cairo.cma" let img_name = "/tmp/fractree.png"let width  = 480let height = 640 let level = 9let line_width = 4.0 let color = (1.0, 0.5, 0.0) let pi = 4.0 *. atan 1.0 let angle_split = pi *. 0.12let angle_rand  = pi *. 0.12 let () =  Random.self_init();  let surf = Cairo.image_surface_create Cairo.FORMAT_RGB24 ~width ~height in  let ctx = Cairo.create surf in  Cairo.set_antialias ctx Cairo.ANTIALIAS_SUBPIXEL;  Cairo.set_line_cap ctx Cairo.LINE_CAP_ROUND;   let draw_line (x,y) (dx,dy) =    Cairo.move_to ctx x  (float height -. y);    Cairo.line_to ctx dx (float height -. dy);    Cairo.stroke ctx;  in  let set_color (r,g,b) v =    Cairo.set_source_rgb ctx ~red:(r *. v) ~green:(g *. v) ~blue:(b *. v);  in  let trans_pos (x,y) len angle =    let _x = cos angle    and _y = sin angle in    (x +. (_x *. len),     y +. (_y *. len))  in   let rec loop ~level ~pos ~line_width ~line_len               ~angle ~angle_split ~angle_rand ~intc =    if level > 0 then begin      (* draw the current segment *)      Cairo.set_line_width ctx line_width;      set_color color intc;      let pos_to = trans_pos pos line_len angle in      draw_line pos pos_to;      (* evolution of the parameters *)      let line_width = line_width *. 0.8      and line_len   = line_len   *. 0.62      and angle_split = angle_split *. 1.02      and angle_rand  = angle_rand  *. 1.02      and intc = intc *. 0.9      in      let next_loop =        loop ~level:(pred level) ~pos:pos_to ~intc             ~line_width ~line_len ~angle_split ~angle_rand      in      (* split *)      let angle_left  = angle +. angle_split +. Random.float angle_rand      and angle_right = angle -. angle_split -. Random.float angle_rand      in      next_loop ~angle:angle_left;      next_loop ~angle:angle_right    end  in   let pos = (float width *. 0.5, float height *. 0.1)  and line_len = float height *. 0.3  in  loop ~level ~pos ~angle:(pi /. 2.0)       ~angle_split ~angle_rand       ~line_width ~line_len ~intc:1.0;   Cairo_png.surface_write_to_file surf img_name  (*Cairo_png.surface_write_to_channel surf stdout*)

## PARI/GP

Output FracTree1.png
Output FracTree2.png
Output FracTree3.png

This version with recursion, in general, is a translation of JavaScript version. Some tweaks and options were added to make it reusable and outputting different size of a tree.

Translation of: JavaScript
Works with: PARI/GP version 2.7.4 and above
 \\ Fractal tree (w/recursion)\\ 4/10/16 aevplotline(x1,y1,x2,y2)={plotmove(0, x1,y1);plotrline(0,x2-x1,y2-y1);} plottree(x,y,a,d)={my(x2,y2,d2r=Pi/180.0,a1=a*d2r,d1);if(d<=0, return(););if(d>0, d1=d*10.0;    x2=x+cos(a1)*d1;    y2=y+sin(a1)*d1;    plotline(x,y,x2,y2);    plottree(x2,y2,a-20,d-1);    plottree(x2,y2,a+20,d-1),    return();  );} FractalTree(depth,size)={my(dx=1,dy=0,ttlb="Fractal Tree, depth ",ttl=Str(ttlb,depth));print1(" *** ",ttl); print(", size ",size);plotinit(0);plotcolor(0,6); \\greenplotscale(0, -size,size, 0,size); plotmove(0, 0,0);plottree(0,0,90,depth);plotdraw([0,size,size]);} {\\ Executing:FractalTree(9,500);     \\FracTree1.pngFractalTree(12,1100);   \\FracTree2.pngFractalTree(15,1500);   \\FracTree3.png}
Output:
 *** Fractal Tree, depth 9, size 500
***   last result computed in 140 ms.

*** Fractal Tree, depth 12, size 1100
***   last result computed in 236 ms.

*** Fractal Tree, depth 15, size 1500
***   last result computed in 1,095 ms


## Perl

using the GD::Simple module.

use GD::Simple; my ($width,$height) = (1000,1000); # image dimensionmy $scale = 6/10; # branch scale relative to trunkmy$length = 400; # trunk size my $img = GD::Simple->new($width,$height);$img->fgcolor('black');$img->penSize(1,1); tree($width/2, $height,$length, 270); print $img->png; sub tree{ my ($x, $y,$len, $angle) = @_; return if$len < 1;         $img->moveTo($x,$y);$img->angle($angle);$img->line($len); ($x, $y) =$img->curPos();         tree($x,$y, $len*$scale, $angle+35); tree($x, $y,$len*$scale,$angle-35);}

## Phix

Translation of: XPL0
Library: Phix/pGUI
Library: Phix/online

You can run this online here.

--
-- demo\rosetta\FractalTree.exw
-- ============================
--
with javascript_semantics
include pGUI.e

Ihandle dlg, canvas
cdCanvas cddbuffer, cdcanvas

procedure drawTree(integer level, atom angle, len, integer x, y)
integer xn = x + floor(len*cos(angle)),
yn = y + floor(len*sin(angle)),
red = 255-level*8,
green = level*12+100
cdCanvasSetForeground(cddbuffer, red*#10000 + green*#100)
cdCanvasSetLineWidth(cddbuffer,floor(5-level/3))
cdCanvasLine(cddbuffer, x, 480-y, xn, 480-yn)
if level<12 then
drawTree(level+1, angle-0.4, len*0.8, xn, yn)   --left
drawTree(level+1, angle+0.1, len*0.8, xn, yn)   --right
end if
end procedure

function redraw_cb(Ihandle /*ih*/, integer /*posx*/, /*posy*/)
cdCanvasActivate(cddbuffer)
cdCanvasClear(cddbuffer)
drawTree(0, -PI/2.0, 80.0, 360, 460)
cdCanvasFlush(cddbuffer)
return IUP_DEFAULT
end function

function map_cb(Ihandle ih)
cdcanvas = cdCreateCanvas(CD_IUP, ih)
cddbuffer = cdCreateCanvas(CD_DBUFFER, cdcanvas)
cdCanvasSetBackground(cddbuffer, CD_PARCHMENT)
return IUP_DEFAULT
end function

procedure main()
IupOpen()

canvas = IupCanvas("RASTERSIZE=640x480")
IupSetCallbacks(canvas, {"MAP_CB", Icallback("map_cb"),
"ACTION", Icallback("redraw_cb")})

dlg = IupDialog(canvas,"RESIZE=NO")
IupSetAttribute(dlg, "TITLE", "Fractal Tree")

IupShow(dlg)
if platform()!=JS then
IupMainLoop()
IupClose()
end if
end procedure

main()


## PHP

Image is created with GD module. Code adapted from the JavaScript version.

Output:

## R

Translation of: PARI/GP
Works with: R version 3.3.3 and above
File:FRTR9.png
Output FRTR9.png
File:FRTR12.png
Output FRTR12.png
File:FRTR15.png
Output FRTR15.png
 ## Recursive FT plottingplotftree <- function(x, y, a, d, c) {x2=y2=0; d2r=pi/180.0; a1 <- a*d2r; d1=0;if(d<=0) {return()}if(d>0)  { d1=d*10.0;    x2=x+cos(a1)*d1;    y2=y+sin(a1)*d1;    segments(x*c, y*c, x2*c, y2*c, col='darkgreen');    plotftree(x2,y2,a-20,d-1,c);    plotftree(x2,y2,a+20,d-1,c);    #return(2);  }}## Plotting Fractal Tree. aev 3/27/17## ord - order/depth, c - scale, xsh - x-shift, fn - file name,##  ttl - plot title.pFractalTree <- function(ord, c=1, xsh=0, fn="", ttl="") {  cat(" *** START FRT:", date(), "\n");  m=640;  if(fn=="") {pf=paste0("FRTR", ord, ".png")} else {pf=paste0(fn, ".png")};  if(ttl=="") {ttl=paste0("Fractal tree, order - ", ord)};  cat(" *** Plot file -", pf, "title:", ttl, "\n");  ##plot(NA, xlim=c(0,m), ylim=c(-m,0), xlab="", ylab="", main=ttl);  plot(NA, xlim=c(0,m), ylim=c(0,m), xlab="", ylab="", main=ttl);  plotftree(m/2+xsh,100,90,ord,c);  dev.copy(png, filename=pf, width=m, height=m);  dev.off(); graphics.off();  cat(" *** END FRT:",date(),"\n");}## Executing:pFractalTree(9);pFractalTree(12,0.6,210);pFractalTree(15,0.35,600);
Output:
> pFractalTree(9);
*** START FRT: Tue Mar 28 16:49:49 2017
*** Plot file - FRTR9.png title: Fractal tree, order - 9
*** END FRT: Tue Mar 28 16:49:50 2017
> pFractalTree(12,0.6,210);
*** START FRT: Tue Mar 28 17:32:15 2017
*** Plot file - FRTR12.png title: Fractal tree, order - 12
*** END FRT: Tue Mar 28 17:32:16 2017
> pFractalTree(15,0.35,600);
*** START FRT: Tue Mar 28 17:38:34 2017
*** Plot file - FRTR15.png title: Fractal tree, order - 15
*** END FRT: Tue Mar 28 17:38:41 2017


## Racket

 #lang racket(require graphics/turtles) (define (tree n)  (when (> n 1)    (draw (/ n 2))    (tprompt (split* (turn 60) (turn -60))             (tree (/ n 2)))    (draw (/ n 2))    (turn 5)    (tree (- n 1)))) (turtles #t) (move 100) (turn 90) (move -200)(tree 35)    (save-turtle-bitmap "tree.png" 'png)

## Raku

(formerly Perl 6) Image is created in SVG format.

my ($width,$height) = (1000,1000); # image dimensionmy $scale = 6/10; # branch scale relative to trunkmy$length = 400; # trunk size say "<?xml version='1.0' encoding='utf-8' standalone='no'?><!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.1//EN' 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'><svg width='100%' height='100%' version='1.1'xmlns='http://www.w3.org/2000/svg'>"; tree($width/2,$height, $length, 3*pi/2); say "</svg>"; multi tree($, $,$length where { $length < 1},$) {}multi tree($x,$y, $length,$angle){	my ($x2,$y2) = ( $x +$length * $angle.cos,$y + $length *$angle.sin);	say "<line x1='$x' y1='$y' x2='$x2' y2='$y2' style='stroke:rgb(0,0,0);stroke-width:1'/>";	tree($x2,$y2, $length*$scale, $angle + pi/5); tree($x2, $y2,$length*$scale,$angle - pi/5);}

## Red

Red [Needs: 'View] color: brownwidth: 9view/tight/options/flags/no-wait [	; click image to grow tree	img: image 1097x617 draw [		pen brown line-width 9 line 500x600 500x500] [grow]] [offset: 0x0] [no-border] ends: reduce [500x500 pi * 3 / 2]	; list of terminal nodesda: pi * 30 / 180	; angle of branches in radiansea: pi * 5 / 180	; offset added to angle to break symmetry l: 200			; branches initial lenghtscale: 0.7		; branches scale factorgrow: does [		; grows branches	l: l * scale	color: 2 * color + leaf / 3	width: width - 1	newends: copy []	foreach [p a] ends [		a1: a + da - ea		p1: p + as-pair l * cos a1 l * sin a1		a2: a - da - ea		p2: p + as-pair l * cos a2 l * sin a2		append img/draw compose/deep [					pen (color)	line-width (width) line (p1) (p) (p2)]		append newends reduce [p1 a1 p2 a2]	]	ends: newends]
Output:

## Ring

 load "guilib.ring" new qapp         {        win1 = new qwidget() {               setwindowtitle("drawing using qpainter")               setgeometry(100,100,500,500)               label1 = new qlabel(win1) {                        setgeometry(10,10,400,400)                        settext("")               }               draw()               show()         }         exec()         } func draw     p1 = new qpicture()             color = new qcolor() {             setrgb(0,0,255,255)        }        pen = new qpen() {              setcolor(color)              setwidth(1)        }        new qpainter() {            begin(p1)            setpen(pen)         sizex = 400        sizey = 200        depth = 10         tree(self, sizex, 0, sizey/2, 90, depth)         endpaint()        }        label1 { setpicture(p1) show() }         func tree myObj, x1, y1, size, angle, depth             myObj{             scale = 0.76             spread = 25             x2 = x1 + size * cos(angle)             y2 = y1 + size * sin(angle)             drawline(x1, y1, x2, y2)             if depth > 0              tree(self, x2, y2, size * scale, angle - spread, depth - 1)             tree(self, x2, y2, size * scale, angle + spread, depth - 1) ok}

Output:

## Ruby

Library: Shoes
Shoes.app(:title => "Fractal Tree", :width => 600, :height => 600) do  background "#fff"  stroke "#000"  @deg_to_rad = Math::PI / 180.0   def drawTree(x1, y1, angle, depth)    if depth != 0      x2 = x1 + (Math.cos(angle * @deg_to_rad) * depth * 10.0).to_i      y2 = y1 + (Math.sin(angle * @deg_to_rad) * depth * 10.0).to_i       line x1, y1, x2, y2       drawTree(x2, y2, angle - 20, depth - 1)      drawTree(x2, y2, angle + 20, depth - 1)          end  end   drawTree(300,550,-90,9)end

## Rust

Library: Piston
//Cargo deps ://  piston = "0.35.0"//  piston2d-graphics = "0.23.0"//  piston2d-opengl_graphics = "0.49.0"//  pistoncore-glutin_window = "0.42.0" extern crate piston;extern crate graphics;extern crate opengl_graphics;extern crate glutin_window; use piston::window::WindowSettings;use piston::event_loop::{Events, EventSettings};use piston::input::RenderEvent;use glutin_window::GlutinWindow as Window;use opengl_graphics::{GlGraphics, OpenGL};use graphics::{clear, line, Context}; const ANG: f64 = 20.0;const COLOR: [f32; 4] = [1.0, 0.0, 0.5, 1.0];const LINE_THICKNESS: f64 = 5.0;const DEPTH: u32 = 11; fn main() {    let mut window: Window = WindowSettings::new("Fractal Tree", [1024, 768])        .opengl(OpenGL::V3_2)        .exit_on_esc(true)        .build()        .unwrap();    let mut gl = GlGraphics::new(OpenGL::V3_2);     let mut events = Events::new(EventSettings::new());    while let Some(e) = events.next(&mut window) {        if let Some(args) = e.render_args() {            gl.draw(args.viewport(), |c, g| {                clear([1.0, 1.0, 1.0, 1.0], g);                draw_fractal_tree(512.0, 700.0, 0.0, DEPTH, c, g);            });        }    }} fn draw_fractal_tree(x1: f64, y1: f64, angle: f64, depth: u32, c: Context, g: &mut GlGraphics) {    let x2 = x1 + angle.to_radians().sin() * depth as f64 * 10.0;    let y2 = y1 - angle.to_radians().cos() * depth as f64 * 10.0;    line(        COLOR,        LINE_THICKNESS * depth as f64 * 0.2,        [x1, y1, x2, y2],        c.transform,        g,    );    if depth > 0 {        draw_fractal_tree(x2, y2, angle - ANG, depth - 1, c, g);        draw_fractal_tree(x2, y2, angle + ANG, depth - 1, c, g);    }}

## Scala

Adapted from the Java version. Screenshot below.

import swing._import java.awt.{RenderingHints, BasicStroke, Color} object FractalTree extends SimpleSwingApplication {  val DEPTH = 9   def top = new MainFrame {    contents = new Panel {      preferredSize = new Dimension(600, 500)       override def paintComponent(g: Graphics2D) {        draw(300, 460, -90, DEPTH)         def draw(x1: Int, y1: Int, angle: Double, depth: Int) {          if (depth > 0) {            val x2 = x1 + (math.cos(angle.toRadians) * depth * 10).toInt            val y2 = y1 + (math.sin(angle.toRadians) * depth * 10).toInt             g.setColor(Color.getHSBColor(0.25f - depth * 0.125f / DEPTH, 0.9f, 0.6f))            g.setStroke(new BasicStroke(depth))            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON)            g.drawLine(x1, y1, x2, y2)             draw(x2, y2, angle - 20, depth - 1)            draw(x2, y2, angle + 20, depth - 1)          }        }      }    }  }}

## Scheme

The tree is created as a list of line segments, which can then be drawn on a required device. For this program, the tree is output to an eps file.

 (import (scheme base)        (scheme file)        (scheme inexact)        (scheme write)) (define *scale* 10) ; controls overall size of tree(define *split* 20) ; controls angle of split (in degrees) ;; construct lines for tree as list of 5-tuples (x1 y1 x2 y2 depth);; - x1 y1 is start point;; - angle of this line, in radians;; - depth, depth within tree (controls length of line)(define (create-tree x1 y1 angle depth)  (define (degrees->radians d)    (let ((pi 3.14159265358979323846264338327950288419716939937510582097))      (* d pi 1/180)))  ;  (if (zero? depth)    '()    (let ((x2 (+ x1 (* (cos (degrees->radians angle)) depth *scale*)))          (y2 (+ y1 (* (sin (degrees->radians angle)) depth *scale*))))      (append (list (map truncate (list x1 y1 x2 y2 depth)))              (create-tree x2 y2 (- angle *split*) (- depth 1))              (create-tree x2 y2 (+ angle *split*) (- depth 1)))))) ;; output the tree to an eps file(define (output-tree-as-eps filename tree)  (when (file-exists? filename) (delete-file filename))  (with-output-to-file    filename    (lambda ()      (display "%!PS-Adobe-3.0 EPSF-3.0\n%%BoundingBox: 0 0 800 800\n")        ;; add each line - sets linewidth based on depth in tree      (for-each (lambda (line)                  (display                    (string-append "newpath\n"                                   (number->string (list-ref line 0)) " "                                   (number->string (list-ref line 1)) " "                                   "moveto\n"                                   (number->string (list-ref line 2)) " "                                   (number->string (list-ref line 3)) " "                                   "lineto\n"                                   (number->string (truncate (/ (list-ref line 4) 2)))                                   " setlinewidth\n"                                   "stroke\n"                                   )))                tree)      (display "\n%%EOF")))) (output-tree-as-eps "fractal.eps" (create-tree 400 200 90 9))

## Scilab

### L-System approach

This script uses complex numbers to represent (x,y) coordinates: real part as x position, and imaginary part as y position. The tree is generated using an L-system approach, and the lines are then drawn by interpreting the resulting sentence. The output is plotted onto graphic window.

trunk = 1;                  //trunk lengthratio = 0.8;                //size ratio between two consecutive branchesdepth = 9;                  //final number of branch levelsorign = 0;                  //origin of the tree (should be complex)angle = 45*%pi/180;         //angle between two branches [rad]trunk_angle = 90*%pi/180;   //angle between trunk and X-axis [rad] right_angle = angle/2;      //angles to the right or to the leftleft_angle = 0.8*angle;     //can be set independently or                             //as function of 'angle' //L-system definition://Alphabet: FBD[]+-    //F: go forward             B: go backwards    //[: start new branch       ]: end current branch    //+: branch to the right    -: branch to the left    //D: double line (forward then backward)//Axiom:    D//Rule:     D -> F[+D-D]B //L-system sentence generationsentence = 'D'rule = 'F[+D-D]B';for i=1:depth    sentence = strsubst(sentence,'D',rule);endsentence = strsplit(sentence)'; //Empty treetree_size = 1.0...            + length(find(sentence=='F'|sentence=='B'))...            + 2 * length(find(sentence=='D'));tree=zeros(tree_size,1); //Drawing the treebranch_level = 0;curr_angle = trunk_angle;curr_pos = 1; for ind = 1:size(sentence,'c')    charac = sentence(ind);     select charac        case 'F' then //Draw line forward            tree(curr_pos+1) = tree(curr_pos)...                               + trunk * ratio^branch_level * exp(curr_angle*%i);            curr_pos = curr_pos + 1;         case 'B' then //Draw line backwards            tree(curr_pos+1) = tree(curr_pos)...                               + trunk * ratio^branch_level * exp((%pi+curr_angle)*%i);            curr_pos = curr_pos + 1;         case '[' then //New branch            branch_level = branch_level + 1;         case '+' then //Turn right            curr_angle = curr_angle - right_angle;         case '-' then //Turn left            curr_angle = curr_angle + right_angle + left_angle;         case ']' then //End of branch            branch_level = branch_level - 1;            curr_angle = curr_angle - left_angle;         case 'D' then //Double line            tree(curr_pos+1) = tree(curr_pos)...                               + trunk * ratio^branch_level * exp(curr_angle*%i);            tree(curr_pos+2) = tree(curr_pos+1)...                               + trunk * ratio^branch_level * exp((%pi+curr_angle)*%i);            curr_pos = curr_pos + 2;    endend scf(); clf();xname('Fractal tree: '+string(depth)+' levels')plot2d(real(tree),imag(tree),14);set(gca(),'isoview','on');set(gca(),'axes_visible',['off','off','off']);

### Recursive approach

Translation of: PHP
width = 512;height = 512;img=scf();set(img,'figure_size',[width,height]); function drawTree(x1, y1, angle, depth)    if depth ~= 0 then        x2 = x1 + cos(angle * %pi/180) * depth * 10;        y2 = y1 + sin(angle * %pi/180) * depth * 10;        plot2d([x1 x2],[y1 y2],14);        drawTree(x2, y2, angle - 20, depth - 1);        drawTree(x2, y2, angle + 20, depth - 1);    endendfunction drawTree(width/2,height,90,10);set(gca(),'isoview','on');

## TUSCRIPT

Image is created in SVG-format

 $$MODE TUSCRIPTdest="fracaltree.svg"ERROR/STOP CREATE (dest,fdf-o,-std-)ACCESS d: WRITE/ERASE/RECORDS/UTF8 dest s,textMODE DATA$$ header=*<?xml version="1.0" standalone="yes"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd"><svg xmlns="http://www.w3.org/2000/svg"  xmlns:xlink="http://www.w3.org/1999/xlink" width="400" height="320">  <style type="text/css"><![CDATA[  line { stroke: brown; stroke-width: .05; }  ]]></style>$$WRITE/NEXT d header$$ defsbeg=* <defs>  <g id="stem"> <line x1="0" y1="0" x2="0" y2="-1"/> </g>  <g id="l"><use xlink:href="#stem"/></g>$$WRITE/NEXT d defsbeg$$ LOOP n=10,21$$id=n+1,lastnr=VALUE(n)$$ g=*  <g id="{id}"> <use xlink:href="#{n}" transform="translate(0, -1) rotate(-35) scale(.7)"/>  <use xlink:href="#{n}" transform="translate(0, -1) rotate(+35) scale(.7)"/> <use xlink:href="#stem"/></g>$$WRITE/NEXT d g$$ ENDLOOP$$defsend = *</defs><g transform="translate(200, 320) scale(100)"> <use xlink:href="#{lastnr}"/></g>$$ MODE TUSCRIPTWRITE/NEXT d defsendWRITE/NEXT d "</svg>"ENDACCESS d

## TypeScript

Translation of: JavaScript
// Set up canvas for drawingvar canvas: HTMLCanvasElement = document.createElement('canvas')canvas.width = 600canvas.height = 500document.body.appendChild(canvas)var ctx: CanvasRenderingContext2D = canvas.getContext('2d')ctx.fillStyle = '#000'ctx.lineWidth = 1 // constantsconst degToRad: number = Math.PI / 180.0const totalDepth: number = 9 /** Helper function that draws a line on the canvas */function drawLine(x1: number, y1: number, x2: number, y2: number): void {    ctx.moveTo(x1, y1)    ctx.lineTo(x2, y2)} /** Draws a branch at the given point and angle and then calls itself twice */function drawTree(x1: number, y1: number, angle: number, depth: number): void {    if (depth !== 0) {        let x2: number = x1 + (Math.cos(angle * degToRad) * depth * 10.0)        let y2: number = y1 + (Math.sin(angle * degToRad) * depth * 10.0)        drawLine(x1, y1, x2, y2)        drawTree(x2, y2, angle - 20, depth - 1)        drawTree(x2, y2, angle + 20, depth - 1)    }} // actual drawing of treectx.beginPath()drawTree(300, 500, -90, totalDepth)ctx.closePath()ctx.stroke()

## Wren

Translation of: Kotlin
Library: DOME
import "graphics" for Canvas, Colorimport "dome" for Windowimport "math" for Math var Radians = Fn.new { |d| d * Num.pi / 180 } class FractalTree {    construct new(width, height) {        Window.title = "Fractal Tree"        Window.resize(width, height)        Canvas.resize(width, height)        _fore = Color.white    }     init() {        drawTree(400, 500, -90, 9)    }     drawTree(x1, y1, angle, depth) {        if (depth == 0) return        var r = Radians.call(angle)        var x2 = x1 + (Math.cos(r) * depth * 10).truncate        var y2 = y1 + (Math.sin(r) * depth * 10).truncate        Canvas.line(x1, y1, x2, y2, _fore)        drawTree(x2, y2, angle - 20, depth - 1)        drawTree(x2, y2, angle + 20, depth - 1)    }     update() {}     draw(alpha) {}} var Game = FractalTree.new(800, 600)

## XPL0

Output
include c:\cxpl\codes; proc DrawBranch(Lev, Dir, Len, X, Y);int  Lev; real Dir, Len; int X, Y;int  Red, Grn;[Move(X, Y);X:= X + fix(Len*Cos(Dir));Y:= Y + fix(Len*Sin(Dir));Red:= 255-Lev*8;  Grn:= Lev*12+100;Line(X, Y, Red<<16+Grn<<8);if Lev < 12 then                \limit level of recursion        [DrawBranch(Lev+1, Dir-0.4, Len*0.8, X, Y);     \left         DrawBranch(Lev+1, Dir+0.1, Len*0.8, X, Y);     \right        ];]; [SetVid(\$112);                  \set 640x480x24 video graphics modeDrawBranch(0, -3.14159/2.0, 80.0, 360, 460);if ChIn(1) then [];             \wait for keystrokeSetVid(3);                      \restore normal text mode]

## zkl

Uses the PPM class from http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#zkl

Translation of: BBC BASIC
Translation of: XPL0
fcn fractalTree(){   scale:=0.76;   sizeX:=400; sizeY:=300;   bitmap:=PPM(sizeX*2,sizeY*2,0xFF|FF|FF);   branch:='wrap(x1,y1,size,angle,depth){      ar:=angle.toRad();      x2:=x1 - size*ar.cos();      y2:=y1 + size*ar.sin();      color:=(0xff-depth*8).shiftLeft(16) + (depth*12+100).shiftLeft(8);      bitmap.line(x1,y1, x2,y2, color);      if(depth){         self.fcn(x2,y2,scale*size,angle - 30,depth - 1,vm.pasteArgs(5));	 self.fcn(x2,y2,scale*size,angle + 8, depth - 1,vm.pasteArgs(5));      }   };   branch(sizeX,0,sizeY/2,90.0,10);   bitmap.write(File("foo.ppm","wb"));}();

The funkyness (pasteArgs) in the recursion (self.fcn) is due to the closure ('wrap): the closed over args are stashed in the arglist, they need to be added to the parameters when recursing.

## ZX Spectrum Basic

Translation of: BASIC256
10 LET level=12: LET LONG=4520 LET x=127: LET y=030 LET rotation=PI/240 LET a1=PI/9: LET a2=PI/950 LET c1=0.75: LET c2=0.7560 DIM x(level): DIM y(level)70 BORDER 0: PAPER 0: INK 4: CLS 80 GO SUB 10090 STOP 100 REM Tree110 LET x(level)=x: LET y(level)=y120 GO SUB 1000130 IF level=1 THEN GO TO 240140 LET level=level-1150 LET LONG=LONG*c1160 LET rotation=rotation-a1170 GO SUB 100180 LET LONG=LONG/c1*c2190 LET rotation=rotation+a1+a2200 GO SUB 100210 LET rotation=rotation-a2220 LET LONG=LONG/c2230 LET level=level+1240 LET x=x(level): LET y=y(level)250 RETURN 1000 REM Draw1010 LET yn=-SIN rotation*LONG+y1020 LET xn=COS rotation*LONG+x1030 PLOT x,y: DRAW xn-x,y-yn1040 LET x=xn: LET y=yn1050 RETURN