Fractal tree: Difference between revisions

12,833 bytes added ,  24 days ago
m
 
(29 intermediate revisions by 14 users not shown)
Line 14:
{{trans|Nim}}
 
<langsyntaxhighlight lang="11l">-V
Width = 1000
Height = 1000
Line 22:
DeltaAngle = 0.2 * math:pi
 
F drawTree(outfile, Float x, Float y; len, theta) -> NVoid
I len >= 1
V x2 = x + len * cos(theta)
Line 30:
drawTree(outfile, x2, y2, len * ScaleFactor, theta - DeltaAngle)
 
V outsvg = File(‘tree.svg’, ‘w’WRITE)
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'>
Line 37:
’)
drawTree(outsvg, 0.5 * Width, Height, TrunkLength, StartingAngle)
outsvg.write("</svg>\n")</langsyntaxhighlight>
 
=={{header|Action!}}==
Action! language does not support recursion. Therefore an iterative approach with a stack has been proposed.
<langsyntaxhighlight Actionlang="action!">DEFINE MAXSIZE="12"
 
INT ARRAY SinTab=[
Line 147:
DO UNTIL CH#$FF OD
CH=$FF
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Fractal_tree.png Screenshot from Atari 8-bit computer]
Line 153:
=={{header|Ada}}==
{{libheader|SDLAda}}
<langsyntaxhighlight Adalang="ada">with Ada.Numerics.Elementary_Functions;
 
with SDL.Video.Windows.Makers;
Line 233:
Window.Finalize;
SDL.Finalise;
end Fractal_Tree;</langsyntaxhighlight>
 
=={{header|Amazing Hopper}}==
{{Trans|BBCBasic}}
"Una suma que no quería salir, pero ya salió" :D
[[File:Captura de pantalla de 2022-10-11 14-24-23.png|200px|thumb|rigth|Caption]]
<syntaxhighlight lang="txt">
/*
Execute with:
 
$ hopper jm/tree.jambo -x -o bin/tree
$ rxvt -g 280x250 -fn "xft:FantasqueSansMono-Regular:pixelsize=1" -e ./bin/tree
 
*/
 
#include <jambo.h>
 
Main
Set '25, 0.76, 160, 100, 10' Init 'Spread, Scale, SizeX, SizeY, Depth'
Color back '22', Cls
Color back '15'
Set '{SizeX} Mul by (2), -30, Div(SizeY,2), 90, Depth' Gosub 'Branch'
Pause
End
 
Subrutines
 
Define 'Branch, x1, y1, size, angle, depth'
x2=0, y2=0
Let ( x2 := #(x1 + size * cos(d2r(angle))) )
Let ( y2 := #(y1 + size * sin(d2r(angle))) )
Draw a line ( #(180-y1), #(180-x1), #(180-y2), #(180-x2))
If ( #( depth > 0) )
Set (x2, y2, {size} Mul by 'Scale', {angle} Minus 'Spread',\
Minus one(depth)) Gosub 'Branch'
Set (x2, y2, {size} Mul by 'Scale', {angle} Plus 'Spread',\
Minus one(depth)) Gosub 'Branch'
End If
Return
</syntaxhighlight>
 
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">width: 1000
height: 1000
 
Line 269 ⟶ 308:
'svg ++ "</svg>"
 
write "fractal.svg" svg</langsyntaxhighlight>
 
{{out}}
Line 278 ⟶ 317:
[http://i.imgur.com/H7iJOde.png Image] - Link, since uploads seem to be disabled currently.
{{libheader|GDIP}}
<langsyntaxhighlight AutoHotkeylang="autohotkey">#SingleInstance, Force
#NoEnv
SetBatchLines, -1
Line 343 ⟶ 382:
Exit:
Gdip_Shutdown(pToken)
ExitApp</langsyntaxhighlight>
 
=={{header|BASIC}}==
Line 349 ⟶ 388:
==={{header|BASIC256}}===
[[File:Fractal tree BASIC-256.png|thumb|right|Asymmetric fractal tree image created by the BASIC-256 script]]
<langsyntaxhighlight lang="basic256">graphsize 300,300
 
level = 12 : len =63 # initial values
Line 393 ⟶ 432:
line x,y,xn,yn
x = xn : y = yn
return</langsyntaxhighlight>
 
==={{header|Run BASIC}}===
<langsyntaxhighlight Runlang="run BASICbasic"> 'Fractal Tree - for Run Basic - 29 Apr 2018
'from BASIC256 - http://rosettacode.org/wiki/Fractal_tree#BASIC256
'copy this text and go to http://www.runbasic.com
Line 455 ⟶ 494:
return
'end of code
End</langsyntaxhighlight>
 
==={{header|BBC BASIC}}===
Line 468 ⟶ 507:
 
{{works with|BBC BASIC for Windows}}
<langsyntaxhighlight lang="bbcbasic">
Spread = 25
Scale = 0.76
Line 474 ⟶ 513:
SizeY% = 300
Depth% = 10
</langsyntaxhighlight><syntaxhighlight lang ="bbcbasic">
VDU 23,22,SizeX%;SizeY%;8,16,16,128
Line 490 ⟶ 529:
PROCbranch(x2, y2, size * Scale, angle + Spread, depth% - 1)
ENDIF
ENDPROC</langsyntaxhighlight>
 
==={{header|IS-BASIC}}===
<langsyntaxhighlight ISlang="is-BASICbasic">100 PROGRAM "Tree.bas"
110 OPTION ANGLE DEGREES
120 GRAPHICS HIRES 2
Line 506 ⟶ 545:
210 CALL TREE(N*.75)
220 PLOT RIGHT 25,BACK N,
230 END DEF</langsyntaxhighlight>
 
=={{header|C}}==
Line 512 ⟶ 551:
 
{{libheader|SGE}} or {{libheader|cairo}}
<langsyntaxhighlight lang="c" line="1">#include <SDL/SDL.h>
#ifdef WITH_CAIRO
#include <cairo.h>
Line 523 ⟶ 562:
#include <math.h>
 
#ifndef M_PI
#ifdef WITH_CAIRO
#define PIM_PI 3.141592653514159265358979323846
#endif
 
#define SIZE 800 // determines size of window
#define SCALE 5 // determines how quickly branches shrink (higher value means faster shrinking)
Line 588 ⟶ 627:
0.0, -1.0,
INITIAL_LENGTH,
PIM_PI / 8,
BRANCHES);
SDL_UpdateRect(surface, 0, 0, 0, 0);
Line 612 ⟶ 651:
SDL_Quit();
return 0;
}</langsyntaxhighlight>
 
=={{header|C++}}==
[[File:fracTree_cpp.png|320px]]
<langsyntaxhighlight lang="cpp">
#include <windows.h>
#include <string>
Line 625 ⟶ 664:
 
//--------------------------------------------------------------------------------------------------
#ifndef M_PI
const float PI = 3.1415926536f;
#define M_PI 3.14159265358979323846
#endif
 
//--------------------------------------------------------------------------------------------------
Line 747 ⟶ 788:
public:
fractalTree() { _ang = DegToRadian( 24.0f ); }
float DegToRadian( float degree ) { return degree * ( PIM_PI / 180.0f ); }
 
void create( myBitmap* bmp )
Line 808 ⟶ 849:
}
//--------------------------------------------------------------------------------------------------
</syntaxhighlight>
</lang>
 
=={{header|Ceylon}}==
Line 814 ⟶ 855:
{{libheader|Swing}} {{libheader|AWT}}
Be sure to import java.desktop and ceylon.numeric in your module.ceylon file.
<langsyntaxhighlight lang="ceylon">import javax.swing {
 
JFrame { exitOnClose }
Line 858 ⟶ 899:
fractalTree.visible = true;
}</langsyntaxhighlight>
 
=={{header|Clojure}}==
{{trans|Java}}
{{libheader|Swing}} {{libheader|AWT}}
<langsyntaxhighlight Clojurelang="clojure">(import '[java.awt Color Graphics]
'javax.swing.JFrame)
 
Line 888 ⟶ 929:
(.show)))
 
(fractal-tree 9)</langsyntaxhighlight>
 
=={{header|Common Lisp}}==
{{libheader|lispbuilder-sdl}}
{{trans|Clojure}}
<langsyntaxhighlight lang="lisp">;; (require :lispbuilder-sdl)
 
(defun deg-to-radian (deg)
Line 930 ⟶ 971:
(fractal-tree 9)
</syntaxhighlight>
</lang>
 
=={{header|D}}==
===SVG Version===
{{trans|Raku}}
<langsyntaxhighlight lang="d">import std.stdio, std.math;
 
enum width = 1000, height = 1000; // Image dimension.
Line 957 ⟶ 998:
tree(width / 2.0, height, length, 3 * PI / 2);
"</svg>".writeln;
}</langsyntaxhighlight>
 
===Turtle Version===
This uses the turtle module from the Dragon Curve task, and the module from the Grayscale Image task.
{{trans|Logo}}
<langsyntaxhighlight lang="d">import grayscale_image, turtle;
 
void tree(Color)(Image!Color img, ref Turtle t, in uint depth,
Line 981 ⟶ 1,022:
img.tree(t, 10, 80, 0.7, 30);
img.savePGM("fractal_tree.pgm");
}</langsyntaxhighlight>
 
===Alternative version===
{{trans|Java}}
Using DFL.
<langsyntaxhighlight lang="d">import dfl.all;
import std.math;
 
Line 1,028 ⟶ 1,069:
}
return result;
}</langsyntaxhighlight>
 
=={{header|EasyLang}}==
 
[https://easylang.dev/show/#cod=jY/BDsIgEETvfMUkXrRNCa1y8KBH/4NQqiQUDCXa/r2w6cEmHuSwGWbnLcsOt6h0Ug4pGsN2YDq4ECGlZM8YNNmYsaA3d3hwcAbADllfIYrOx1lv3rZPj+xWEPy0+mN4EbxeZ9QX6DDRrIqyLT/muo/K92EcUGdYHtb4UuKT9X/GyxJfj20Wb9CJPKBB+6tbb7qccZbsaGL+XvAgWXztjCJBrBRoBc6lkkdAl9EP Run it]
[https://easylang.online/apps/fractal-tree.html Run it]
 
<syntaxhighlight lang="text">
<lang># Fractal tree
# Fractal tree
#
color 555
func tree x y deg n . .
proc tree ifx y deg n >. 0.
set_linewidthif n *> 0.4
linewidth n * 0.4
move_pen x y
move x y
x += cos deg * n * 1.3 * (randomf + 0.5)
y x += sincos deg * n * 1.3 * (randomf + 0.5)
y += sin deg * n * 1.3 * (randomf + 0.5)
draw_line x y
call tree line x y deg - 20 n - 1
call tree x y deg +- 20 n - 1
tree x y deg + 20 n - 1
.
.
.
timer 0
set_timer 0
on timer
clear
clear_screen
call tree 50 9010 -90 10
timer 2
set_timer 1
.
.</lang>
</syntaxhighlight>
 
=={{header|Evaldraw}}==
 
Evaldraw version creates a 3D tree with a camera rotating around the tree.
[[File:Evaldraw recursive canopy3d.gif|thumb|alt=Fractal Tree in 3D|4 branches for each recursive step. A forward Z vector is rotated in x or y axis.]]
<syntaxhighlight lang="c">
static ratio = .75;
static branchlength = 60;
static max_branches = 4;
struct vec3{x,y,z;};
()
{
t=klock();
srand(t * 1);
zero1 = .5+.5*cos(t);
maxbranches = int( 1+1 + zero1*5);
cls(0); clz(1e32);
distcam = -70;
camrot = .5 * t;
ca=distcam * cos(camrot);
sa=distcam * sin(camrot);
setcam(sa,-50,ca,camrot,0);
angle = 2*pi / 8;
branchlen = 10+50 * zero1;
tree(maxbranches, 0, branchlen, 0,0,0, pi / 2, 0, angle);
moveto(0,0);
printf("N=%g, frame=%5.0f, cam:%3.0f", maxbranches, numframes, camrot / pi * 180);
printf("\n%gx%g",xres,yres);
sleep(16);
}
 
 
tree(mb, n, blen, x,y,z, ang_yx, ang_yz, angle) {
n++; if( n> mb ) return;
len = blen / n * ratio;
c = 64 + 128 * n/7; setcol(100,c,38);
dx=0; dy=0; dz=0;
double mat[9];
vec3 axis = {0,0,1};
ang2mat(ang_yz, ang_yx, mat);
transformPoint(axis,mat);
dx=axis.x;
dy=-axis.y;
dz=axis.z;
ox = x; oy = y; oz = z;
x += len * dx;
y += -len * dy;
z += len * dz;
rd = 8 / n;
rd2 = 7 / (n+1);
drawcone(ox,oy,oz,rd,x,y,z,rd2,DRAWCONE_FLAT + DRAWCONE_NOPHONG);
nextangle = /*(-.5+1*rnd*pi) * */angle;
tree(mb, n, blen, x, y, z, ang_yx - angle, ang_yz, nextangle);
tree(mb, n, blen, x, y, z, ang_yx + angle, ang_yz, nextangle);
tree(mb, n, blen, x, y, z, ang_yx, ang_yz - angle, nextangle);
tree(mb, n, blen, x, y, z, ang_yx, ang_yz + angle, nextangle);
}
 
ang2mat(hang,vang,mat[9]) {
mat[6] = cos(vang)*sin(hang); mat[0] = cos(hang);
mat[7] = sin(vang); mat[1] = 0;
mat[8] = cos(vang)*cos(hang); mat[2] =-sin(hang);
mat[3] = mat[7]*mat[2] - mat[8]*mat[1];
mat[4] = mat[8]*mat[0] - mat[6]*mat[2];
mat[5] = mat[6]*mat[1] - mat[7]*mat[0];
}
 
transformPoint(vec3 v, thisRot[9]) {
NewX = v.x * thisRot[0] + v.y * thisRot[1] + v.z * thisRot[2];
NewY = v.x * thisRot[3] + v.y * thisRot[4] + v.z * thisRot[5];
NewZ = v.x * thisRot[6] + v.y * thisRot[7] + v.z * thisRot[8];
v.x=newx; v.y=newy; v.z=newz;
}
</syntaxhighlight>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
[[File:DelphiFractalTree.png|frame|none]]
 
<syntaxhighlight lang="Delphi">
 
 
 
procedure DrawTree(Image: TImage; X1, Y1: integer; Angle: double; Depth: integer);
var X2,Y2: integer;
begin
if Depth = 0 then exit;
X2:=trunc(X1 + cos(DegToRad(Angle)) * Depth * 5);
Y2:=trunc(Y1 + sin(DegToRad(Angle)) * Depth * 5);
Image.Canvas.Pen.Color:=ColorMap47[MulDiv(High(ColorMap47),Depth,11)];
Image.Canvas.Pen.Width:=MulDiv(Depth,5,10);
Image.Canvas.MoveTo(X1,Y1);
Image.Canvas.LineTo(X2,Y2);
DrawTree(Image, X2, Y2, Angle - 10, Depth - 1);
DrawTree(Image, X2, Y2, Angle + 35, Depth - 1);
end;
 
 
procedure ShowFactalTree(Image: TImage);
begin
ClearImage(Image,clBlack);
DrawTree(Image, 250, 350, -90, 11);
Image.Invalidate;
end;
 
</syntaxhighlight>
{{out}}
<pre>
 
Elapsed Time: 31.913 ms.
 
</pre>
 
=={{header|F_Sharp|F#}}==
{{trans|Raku}}
<langsyntaxhighlight lang="fsharp">let (cos, sin, pi) = System.Math.Cos, System.Math.Sin, System.Math.PI
 
let (width, height) = 1000., 1000. // image dimension
Line 1,076 ⟶ 1,242:
xmlns='http://www.w3.org/2000/svg'>"
tree (width/2.) height length (3.*pi/2.)
printfn "</svg>"</langsyntaxhighlight>
 
=={{header|Fantom}}==
<langsyntaxhighlight lang="fantom">
using fwt
using gfx
Line 1,115 ⟶ 1,281:
}
}
</syntaxhighlight>
</lang>
 
=={{header|FreeBASIC}}==
{{trans|BBC BASIC}}
<langsyntaxhighlight lang="freebasic">' version 17-03-2017
' compile with: fbc -s gui
 
Line 1,158 ⟶ 1,324:
windowtitle ("Fractal Tree, hit any key to end program")
Sleep
End</langsyntaxhighlight>
 
=={{header|Frege}}==
Line 1,164 ⟶ 1,330:
{{Works with|Frege|3.23.888-g4e22ab6}}
 
<langsyntaxhighlight lang="frege">module FractalTree where
 
import Java.IO
Line 1,242 ⟶ 1,408:
drawTree g t 16
f <- File.new "FractalTreeFrege.png"
void $ ImageIO.write buffy "png" f</langsyntaxhighlight>
 
Output is [http://funwithsoftware.org/images/2016-FractalTreeFrege.png here] due to [[User talk:Short Circuit#Is file uploading blocked forever?|Is file uploading blocked forever?]]
 
=={{header|Frink}}==
<syntaxhighlight lang="frink">
<lang Frink>
// Draw Fractal Tree in Frink
 
Line 1,275 ⟶ 1,441:
// Show the final tree
g.show[]
</syntaxhighlight>
</lang>
 
 
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
_window = 1
_wndWidth = 680
 
void local fn BuildWindow
window _window, @"Fractal Tree", ( 0, 0, _wndWidth, 600 )
WindowSetBackgroundColor( _window, fn ColorBlack )
WindowSubclassContentView( _window )
end fn
 
local fn PlotFractalTree( x1 as double, y1 as double, size as long, angle as double, spread as long, depth as long, scale as double )
double x2, y2
pen 1.0, fn ColorGreen, NSLineCapStyleSquare
// Convert angle to radians
x2 = x1 + size * cos(angle * pi / 180)
y2 = y1 + size * sin(angle * pi / 180)
line x1, y1, x2, y2
if ( depth > 0 )
fn PlotFractalTree( x2, y2, size * scale, angle - spread, spread, depth - 1, scale )
fn PlotFractalTree( x2, y2, size * scale, angle + spread, spread, depth - 1, scale )
end if
end fn
 
void local fn DoDialog( ev as long, tag as long )
select ( tag )
case _windowContentViewTag
double spread = ( 80.0 / (_wndWidth / 2 ) ) * 90
fn PlotFractalTree( _wndWidth / 2, 550, 140, -90, spread, 10, 0.75 )
end select
select ( ev )
case _windowWillClose : end
end select
end fn
 
on dialog fn DoDialog
 
fn BuildWindow
 
HandleEvents
</syntaxhighlight>
[[file:Fractal_tree_FutureBasic.png]]
 
 
=={{header|Go}}==
[[file:GoFtree.png|right|thumb|png converted from output ppm]]
<langsyntaxhighlight lang="go">package main
 
// Files required to build supporting package raster are found in:
Line 1,315 ⟶ 1,529:
ftree(g, x2, y2, distance*frac, direction+angle, depth-1)
}
}</langsyntaxhighlight>
 
=={{header|Haskell}}==
An elegant yet universal monoidal solution.
{{libheader|Gloss}}
<langsyntaxhighlight lang="haskell">import Graphics.Gloss
 
type Model = [Picture -> Picture]
Line 1,331 ⟶ 1,545:
, Translate 0 100 . Scale 0.5 0.5 . Rotate (-30) ]
 
main = animate (InWindow "Tree" (800, 800) (0, 0)) white $ tree1 . (* 60)</langsyntaxhighlight>
 
The solution gives rise to a variety of fractal geometric structures. Each one can be used by substituting <code>tree1</code> in the <code>main</code> function by the desired one.
<langsyntaxhighlight lang="haskell">--animated tree
tree2 t = fractal 8 branches $ Line [(0,0),(0,100)]
where branches = [ Translate 0 100 . Scale 0.75 0.75 . Rotate t
Line 1,357 ⟶ 1,571:
pentagon = Line [ (sin a, cos a) | a <- [0,2*pi/5..2*pi] ]
x = 2*cos(pi/5)
s = 1/(1+x)</langsyntaxhighlight>
 
'''Alternative solution'''
Line 1,365 ⟶ 1,579:
{{libheader|HGL}}
 
<langsyntaxhighlight lang="haskell">import Graphics.HGL.Window
import Graphics.HGL.Run
import Control.Arrow
Line 1,395 ⟶ 1,609:
(\w -> setGraphic w (overGraphics ( map polyline $ pts (n-1))) >> getKey w)
 
main = fractalTree 10</langsyntaxhighlight>
 
=={{header|Icon}} and {{header|Unicon}}==
<langsyntaxhighlight Iconlang="icon">procedure main()
WOpen("size=800,600", "bg=black", "fg=white") | stop("*** cannot open window")
drawtree(400,500,-90,9)
Line 1,415 ⟶ 1,629:
}
return
end</langsyntaxhighlight>
 
{{libheader|Icon Programming Library}}
Line 1,424 ⟶ 1,638:
=={{header|J}}==
 
<langsyntaxhighlight lang="j">require'gl2'
coinsert'jgl2'
 
L0=: 50 NB. initial length
A0=: 1r8p1 NB. initial angle: pi divided by 8
Line 1,431 ⟶ 1,646:
dA=: 0.75 NB. shrink factor for angle
N=: 14 NB. number of branches
 
L=: L0*dL^1+i.N NB. lengths of line segments
 
NB. relative angles of successive line segments
A=: A0*(dA^i.N) +/\@:*("1) _1 ^ #:i.2 ^ N
 
NB. end points for each line segment
P=: 0 0+/\@,"2 +.*.inv (L0,0),"2 L,"0"1 A
 
P_C_paint=: gllines_jgl2_@:<.@(10 + ,/"2 P-"1<./,/P)
wd {{)n
pc P closeok;
setp wh 480 640;
cc C isigraphisidraw flush;
pshow;
}}</lang>
 
gllines <.(10 + ,/"2 P-"1<./,/P)</syntaxhighlight>
 
See the [[Talk:Fractal tree#J Explanation|talk page]] for some implementation notes.
Line 1,452 ⟶ 1,668:
=={{header|Java}}==
{{libheader|Swing}} {{libheader|AWT}}
<langsyntaxhighlight lang="java">import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
Line 1,483 ⟶ 1,699:
new FractalTree().setVisible(true);
}
}</langsyntaxhighlight>
 
=={{header|JavaScript}}==
Implementation using HTML5 canvas element to draw tree structure.
<langsyntaxhighlight JavaScriptlang="javascript"><html>
<body>
<canvas id="canvas" width="600" height="500"></canvas>
Line 1,523 ⟶ 1,739:
 
</body>
</html></langsyntaxhighlight>
 
=={{header|jq}}==
The following generates SVG, which can be viewed by following the link below.
<langsyntaxhighlight lang="jq"># width and height define the outer dimensions;
# len defines the trunk size;
# scale defines the branch length relative to the trunk;
Line 1,564 ⟶ 1,780:
;
 
main(1000; 1000; 400; 6/10)</langsyntaxhighlight>
{{out}}
$ jq -r -n -r -f Fractal_tree_svg.jq > Fractal_tree.svg
Line 1,572 ⟶ 1,788:
=={{header|Julia}}==
{{trans|F#}}
<langsyntaxhighlight lang="julia">
const width = height = 1000.0
const trunklength = 400.0
Line 1,600 ⟶ 1,816:
 
write(outsvg, "</svg>\n") # view file tree.svg in browser
</syntaxhighlight>
</lang>
 
=={{header|Kotlin}}==
{{trans|Java}}
<langsyntaxhighlight lang="scala">// version 1.1.2
 
import java.awt.Color
Line 1,635 ⟶ 1,851:
fun main(args: Array<String>) {
FractalTree().isVisible = true
}</langsyntaxhighlight>
 
=={{header|Lambdatalk}}==
<syntaxhighlight lang="lisp">
<lang Lisp>
1) defining the function tree:
 
Line 1,687 ⟶ 1,903:
 
The complete ouput can be seen displayed in http://lambdaway.free.fr/lambdawalks/?view=fractal_tree
</syntaxhighlight>
</lang>
 
=={{header|Liberty BASIC}}==
LB includes Logo-type turtle commands, so can be drawn that way as well as that shown here.
<syntaxhighlight lang="lb">
<lang lb>
NoMainWin
sw = 640 : sh = 480
Line 1,728 ⟶ 1,944:
End If
End Sub
</syntaxhighlight>
</lang>
 
=={{header|Lingo}}==
<langsyntaxhighlight lang="lingo">----------------------------------------
-- Creates an image of a fractal tree
-- @param {integer} width
Line 1,757 ⟶ 1,973:
_drawTree(img, x2, y2, angle+spreadAngle, depth-1, size*ScaleFactor, spreadAngle, scaleFactor)
end if
end</langsyntaxhighlight>
Usage:
<langsyntaxhighlight lang="lingo">fractalDepth = 10
initSize = 7.0
spreadAngle = 35*PI/180
scaleFactor = 0.95
img = fractalTree(480, 380, fractalDepth, initSize, spreadAngle, scaleFactor)</langsyntaxhighlight>
 
=={{header|Logo}}==
<langsyntaxhighlight lang="logo">to tree :depth :length :scale :angle
if :depth=0 [stop]
setpensize round :depth/2
Line 1,779 ⟶ 1,995:
 
clearscreen
tree 10 80 0.7 30</langsyntaxhighlight>
 
=={{header|Lua}}==
===Bitmap===
Needs L&Ouml;VE 2D Engine
<langsyntaxhighlight lang="lua">
g, angle = love.graphics, 26 * math.pi / 180
wid, hei = g.getWidth(), g.getHeight()
Line 1,822 ⟶ 2,038:
g.draw( canvas )
end
</syntaxhighlight>
</lang>
 
===ASCII===
Using the Bitmap class and text renderer from [[Bitmap/Bresenham%27s_line_algorithm#Lua|here]], then extending...
<langsyntaxhighlight lang="lua">function Bitmap:tree(x, y, angle, depth, forkfn, lengfn)
if depth <= 0 then return end
local fork, leng = forkfn(), lengfn()
Line 1,840 ⟶ 2,056:
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]='█'})</langsyntaxhighlight>
{{out}}
Shown at 25% scale:
Line 1,973 ⟶ 2,189:
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">fractalTree[
pt : {_, _}, \[Theta]orient_: \[Pi]/2, \[Theta]sep_: \[Pi]/9,
depth_Integer: 9] := Module[{pt2},
Line 1,990 ⟶ 2,206:
]
Graphics[fractalTree[{0, 0}, \[Pi]/2, \[Pi]/9]]
</syntaxhighlight>
</lang>
[[File:MathFractalTree.png]]
 
=={{header|MiniScript}}==
This GUI implementation is for use with [http://miniscript.org/MiniMicro Mini Micro].
<syntaxhighlight lang="miniscript">
drawTree = function(x1, y1, angle, depth)
fork_angle = 20
base_len = 9
if depth > 0 then
radians = angle * pi / 180
x2 = x1 + cos(radians) * depth * base_len
y2 = y1 + sin(radians) * depth * base_len
gfx.line x1, y1, x2, y2, "#008000"
drawTree x2, y2, angle - fork_angle, depth - 1
drawTree x2, y2, angle + fork_angle, depth - 1
end if
end function
clear
gfx.clear "#87CEEB"
drawTree 480, 10, 90, 11
img = gfx.getImage(0, 0, 960, 640)
file.saveImage "/usr/fractalTree.png", img
</syntaxhighlight>
[[File:FractalTree-ms.png]]
 
=={{header|NetRexx}}==
Line 1,997 ⟶ 2,236:
{{libheader|Swing}}
{{libheader|AWT}}
<langsyntaxhighlight NetRexxlang="netrexx">/* NetRexx */
options replace format comments java crossref symbols binary
 
Line 2,034 ⟶ 2,273:
RFractalTree().setVisible(isTrue)
return
</syntaxhighlight>
</lang>
=={{header|Nim}}==
{{trans|Julia}}
<syntaxhighlight lang="nim">
<lang Nim>
import math
import strformat
Line 2,069 ⟶ 2,308:
outsvg.write("</svg>\n") # View file tree.svg in browser.
 
</syntaxhighlight>
</lang>
 
=={{header|OCaml}}==
{{libheader|ocaml-cairo}}
 
<langsyntaxhighlight lang="ocaml">#directory "+cairo"
#load "bigarray.cma"
#load "cairo.cma"
Line 2,150 ⟶ 2,389:
 
Cairo_png.surface_write_to_file surf img_name
(*Cairo_png.surface_write_to_channel surf stdout*)</langsyntaxhighlight>
 
=={{header|PARI/GP}}==
Line 2,164 ⟶ 2,403:
{{Works with|PARI/GP|2.7.4 and above}}
 
<langsyntaxhighlight lang="parigp">
\\ Fractal tree (w/recursion)
\\ 4/10/16 aev
Line 2,198 ⟶ 2,437:
FractalTree(15,1500); \\FracTree3.png
}
</langsyntaxhighlight>
 
{{Output}}
Line 2,216 ⟶ 2,455:
=={{header|Perl}}==
using the [http://search.cpan.org/~lds/GD-2.45/GD/Simple.pm GD::Simple] module.
<langsyntaxhighlight lang="perl">use GD::Simple;
 
my ($width, $height) = (1000,1000); # image dimension
Line 2,245 ⟶ 2,484:
tree($x, $y, $len*$scale, $angle+35);
tree($x, $y, $len*$scale, $angle-35);
}</langsyntaxhighlight>
 
=={{header|Phix}}==
Line 2,252 ⟶ 2,491:
{{libheader|Phix/online}}
You can run this online [http://phix.x10.mx/p2js/fractaltree.htm here].
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\FractalTree.exw
Line 2,310 ⟶ 2,549:
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</langsyntaxhighlight>-->
 
=={{header|PHP}}==
Image is created with GD module. Code adapted from the JavaScript version.
<langsyntaxhighlight lang="php">
<?php
header("Content-type: image/png");
Line 2,345 ⟶ 2,584:
imagedestroy($img);
?>
</syntaxhighlight>
</lang>
 
=={{header|PicoLisp}}==
This uses the 'brez' line drawing function from
[[Bitmap/Bresenham's line algorithm#PicoLisp]].
<langsyntaxhighlight PicoLisplang="picolisp">(load "@lib/math.l")
 
(de fractalTree (Img X Y A D)
Line 2,364 ⟶ 2,603:
(prinl "P1")
(prinl 400 " " 300)
(mapc prinl Img) ) )</langsyntaxhighlight>
 
=={{header|Plain English}}==
<langsyntaxhighlight lang="plainenglish">To run:
Start up.
Clear the screen to the lightest blue color.
Line 2,385 ⟶ 2,624:
Turn left 1/16 of the way. Draw another tree given the size times 2/3. Turn right 1/16 of the way.
Turn right 1/16 of the way. Draw a third tree given the size times 2/3. Turn left 1/16 of the way.
Go back to where we were.</langsyntaxhighlight>
{{out}}
[https://commons.wikimedia.org/wiki/File:Fractal-tree.png]
 
=={{header|PL/pgSQL}}==
{{works with|Postgres}}
 
This piece of code generates the coordinates of each branch, builds a version in the standardized geometry representation format: WKT.
 
A temporary table contains the results: coordinates and WKT representation of each branch.
 
In a query (Postgres + postgis function), we can draw a unique geometry that can be displayed in a tool like QGis or DBeaver database manager for example.
 
The query exploits the notion of CTE and its recursive form.
 
[[File:plpgsql-tree.png||200px|thumb|rigth|Pl/PgSQL fractal tree]]
 
<syntaxhighlight lang="sql">
drop table if exists my_temp_tree_table;
 
do $$
declare
_length numeric := 1;
-- a little random
_random_length_reduction_max numeric := 0.6;
_fork_angle numeric := pi()/12;
-- a little random
_random_angle numeric := pi()/12;
_depth numeric := 9 ;
 
begin
create temporary table my_temp_tree_table as
WITH RECURSIVE branch(azimuth, x1, y1, x2, y2, len, n) AS (
VALUES (pi()/2, 0.0, 0.0, 0.0, _length, _length, _depth)
UNION all
select azimuth+a,
x2, y2,
round((x2+cos(azimuth+a)*len)::numeric, 2), round((y2+sin(azimuth+a)*len)::numeric, 2),
(len*(_random_length_reduction_max+(random()*(1-_random_length_reduction_max))))::numeric,
n-1
FROM branch
cross join (
select ((-_fork_angle)+(_random_angle)*(random()-0.5)) a
union
select ((_fork_angle)+(_random_angle)*(random()-0.5)) a2
) a
WHERE n > 0
)
select x1, y1, x2, y2, 'LINESTRING('||x1||' '||y1||','||x2||' '||y2||')' as wkt from branch
;
end $$
;
 
-- coordinates and WKT
select * from my_temp_tree_table;
 
-- binary version (postgis) of each branch
select ST_GeomFromEWKT('SRID=4326;'||wkt) geom from my_temp_tree_table;
 
-- a unique geometry
select st_union(ST_GeomFromEWKT('SRID=4326;'||wkt)) geom from my_temp_tree_table;
</syntaxhighlight>
 
{{out}}
coordinates and WKT
<pre>
x1 |y1 |x2 |y2 |wkt |
-----+----+-----+----+---------------------------------+
0.0| 0.0| 0.0| 1|LINESTRING(0.0 0.0,0.0 1) |
0.0| 1| 0.15|1.99|LINESTRING(0.0 1,0.15 1.99) |
0.0| 1|-0.29|1.96|LINESTRING(0.0 1,-0.29 1.96) |
0.15|1.99| 0.36|2.68|LINESTRING(0.15 1.99,0.36 2.68) |
0.15|1.99| 0.05|2.70|LINESTRING(0.15 1.99,0.05 2.70) |
...
 
</pre>
 
===a simple unparameterized version, without randomness===
<syntaxhighlight lang="sql">
WITH RECURSIVE noeuds(azimuth, x0, y0, x, y, len, n) AS (
VALUES (pi()/2, 0::real, 0::real, 0::real, 10::real, 10::real, 9::int)
UNION all
select azimuth+a, x, y, (x+cos(azimuth+a)*len)::real, (y+sin(azimuth+a)*len)::real, (len/2)::real, n-1
FROM noeuds
cross join (select (-pi()/7)::real a union select (pi()/7)::real a2) a
WHERE n > 0
)
, branche as (
select '('||x0||' '||y0||','||x||' '||y||')' b
from noeuds
)
select ST_GeomFromEWKT('SRID=4326;MULTILINESTRING('||string_agg(b, ',')||')') tree
from branche
</syntaxhighlight>
 
=={{header|PostScript}}==
<langsyntaxhighlight lang="postscript">%!PS
%%BoundingBox: 0 0 300 300
%%EndComments
Line 2,435 ⟶ 2,765:
%
showpage origstate restore
%%EOF</langsyntaxhighlight>
 
Shorter version:<langsyntaxhighlight lang="postscript">%!PS-Adobe-3.0
%%BoundingBox: 0 0 300 300
/!0 { dup 1 sub dup 0 gt } def
Line 2,449 ⟶ 2,779:
 
/d 10 def 5 setlinewidth 1 setlinecap 170 20 translate d tree pop
%%EOF</langsyntaxhighlight>
 
=={{header|POV-Ray}}==
<langsyntaxhighlight lang="povray">#include "colors.inc"
#include "transforms.inc"
 
Line 2,494 ⟶ 2,824:
union {
FractalTree(<-2,0,0>, <1,0,0>, 1, Spread_Ang, Branches)
}</langsyntaxhighlight>
 
=={{header|Prolog}}==
SWI-Prolog has a graphic interface : XPCE.
<langsyntaxhighlight Prologlang="prolog">fractal :-
new(D, window('Fractal')),
send(D, size, size(800, 600)),
Line 2,518 ⟶ 2,848:
drawTree(D, X2, Y2, A2, De).
 
</syntaxhighlight>
</lang>
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">#Spread_Ang = 35
#Scaling_Factor = 0.75
#Deg_to_Rad = #PI / 180
Line 2,551 ⟶ 2,881:
EndIf
 
Repeat: Until WaitWindowEvent(10) = #PB_Event_CloseWindow</langsyntaxhighlight>
[[Image:PB_FractalTree.png]]
 
Line 2,557 ⟶ 2,887:
 
==== Using rotation ====
<langsyntaxhighlight lang="java">void setup() {
size(600, 600);
background(0);
Line 2,577 ⟶ 2,907:
popMatrix();
}
}</langsyntaxhighlight>
 
==== Calculating coordinates ====
Line 2,583 ⟶ 2,913:
{{trans|Python}}
 
<langsyntaxhighlight lang="java">void setup() {
size(600, 600);
background(0);
Line 2,600 ⟶ 2,930:
drawTree(x2, y2, angle + forkAngle, depth - 1);
}
}</langsyntaxhighlight>
 
==={{header|Processing Python mode}}===
Line 2,608 ⟶ 2,938:
{{trans|Processing}}
 
<langsyntaxhighlight lang="python">def setup():
size(600, 600)
background(0)
Line 2,625 ⟶ 2,955:
rotate(2 * -fork_ang)
drawTree(0, 0, depth - 1)
popMatrix()</langsyntaxhighlight>
 
==== Calculating coordinates ====
Line 2,631 ⟶ 2,961:
{{trans|Python}}
 
<langsyntaxhighlight lang="python">def setup():
size(600, 600)
background(0)
Line 2,645 ⟶ 2,975:
line(x1, y1, x2, y2)
drawTree(x2, y2, angle - fork_angle, depth - 1)
drawTree(x2, y2, angle + fork_angle, depth - 1)</langsyntaxhighlight>
 
=={{header|Python}}==
[[File:fractal-tree-python.png|right|thumb]]
{{libheader|pygame}}
<langsyntaxhighlight lang="python">import pygame, math
 
pygame.init()
Line 2,674 ⟶ 3,004:
pygame.display.flip()
while True:
input(pygame.event.wait())</langsyntaxhighlight>
=={{header|QB64}}==
<langsyntaxhighlight lang="qb64">_Title "Fractal Tree"
Const sw% = 640
Const sh% = 480
Line 2,700 ⟶ 3,030:
Call tree(newX, newY, initAngle + theta, theta, iL, iD)
End If
End Sub</langsyntaxhighlight>
 
=={{header|Quackery}}==
<langsyntaxhighlight Quackerylang="quackery">[ $ "turtleduck.qky" loadfile ] now!
 
[ [ 1 1
Line 2,724 ⟶ 3,054:
-v fly ] is tree ( n/d --> )
turtle
20 frames
-1 4 turn
-450 1 fly
500 1 tree</lang>
1 frames</syntaxhighlight>
 
{{output}}
 
[[File:Quackery fractal tree.png|thumb|center]]
https://imgur.com/x0TdhS5
 
=={{header|R}}==
Line 2,739 ⟶ 3,071:
[[File:FRTR12.png|200px|right|thumb|Output FRTR12.png]]
[[File:FRTR15.png|200px|right|thumb|Output FRTR15.png]]
<syntaxhighlight lang="r">
<lang r>
## Recursive FT plotting
plotftree <- function(x, y, a, d, c) {
Line 2,774 ⟶ 3,106:
pFractalTree(12,0.6,210);
pFractalTree(15,0.35,600);
</syntaxhighlight>
</lang>
{{Output}}
<pre>
Line 2,793 ⟶ 3,125:
=={{header|Racket}}==
[[File:tree-racket.png|right|thumb]]
<langsyntaxhighlight lang="racket">
#lang racket
(require graphics/turtles)
Line 2,809 ⟶ 3,141:
(tree 35)
(save-turtle-bitmap "tree.png" 'png)
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
Image is created in [[wp:SVG|SVG]] format.
<syntaxhighlight lang="raku" perl6line>my ($width, $height) = (1000,1000); # image dimension
my $scale = 6/10; # branch scale relative to trunk
my $length = 400; # trunk size
Line 2,835 ⟶ 3,167:
tree($x2, $y2, $length*$scale, $angle + pi/5);
tree($x2, $y2, $length*$scale, $angle - pi/5);
}</langsyntaxhighlight>
 
=={{header|Red}}==
<langsyntaxhighlight Redlang="red">Red [Needs: 'View]
 
color: brown
Line 2,868 ⟶ 3,200:
]
ends: newends
]</langsyntaxhighlight>
{{out}}
[https://raw.githubusercontent.com/Palaing/redlib/master/games/images/fractaltree.png fractal tree image]
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "guilib.ring"
 
Line 2,924 ⟶ 3,256:
tree(self, x2, y2, size * scale, angle - spread, depth - 1)
tree(self, x2, y2, size * scale, angle + spread, depth - 1) ok}
</syntaxhighlight>
</lang>
Output:
 
Line 2,931 ⟶ 3,263:
=={{header|Ruby}}==
{{libheader|Shoes}}
<langsyntaxhighlight Rubylang="ruby">Shoes.app(:title => "Fractal Tree", :width => 600, :height => 600) do
background "#fff"
stroke "#000"
Line 2,949 ⟶ 3,281:
drawTree(300,550,-90,9)
end</langsyntaxhighlight>
 
=={{header|Rust}}==
{{libheader|Piston}}
<langsyntaxhighlight Rustlang="rust">//Cargo deps :
// piston = "0.35.0"
// piston2d-graphics = "0.23.0"
Line 3,010 ⟶ 3,342:
}
}
</syntaxhighlight>
</lang>
 
=={{header|Scala}}==
Adapted from the Java version. Screenshot below.
<langsyntaxhighlight lang="scala">import swing._
import java.awt.{RenderingHints, BasicStroke, Color}
 
Line 3,044 ⟶ 3,376:
}
}
}</langsyntaxhighlight>
[[File:scalaTree.png]]
 
Line 3,051 ⟶ 3,383:
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.
 
<langsyntaxhighlight lang="scheme">
(import (scheme base)
(scheme file)
Line 3,103 ⟶ 3,435:
 
(output-tree-as-eps "fractal.eps" (create-tree 400 200 90 9))
</syntaxhighlight>
</lang>
 
=={{header|Scilab}}==
Line 3,110 ⟶ 3,442:
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.
 
<syntaxhighlight lang="text">trunk = 1; //trunk length
ratio = 0.8; //size ratio between two consecutive branches
depth = 9; //final number of branch levels
Line 3,189 ⟶ 3,521:
plot2d(real(tree),imag(tree),14);
set(gca(),'isoview','on');
set(gca(),'axes_visible',['off','off','off']);</langsyntaxhighlight>
 
===Recursive approach===
{{trans|PHP}}
<syntaxhighlight lang="text">width = 512;
height = 512;
img=scf();
Line 3,209 ⟶ 3,541:
drawTree(width/2,height,90,10);
set(gca(),'isoview','on');</langsyntaxhighlight>
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "float.s7i";
include "math.s7i";
Line 3,241 ⟶ 3,573:
drawTree(300, 470, -90.0, 9);
ignore(getc(KEYBOARD));
end func;</langsyntaxhighlight>
 
Original source: [http://seed7.sourceforge.net/algorith/graphic.htm#fractree]
Line 3,247 ⟶ 3,579:
=={{header|Sidef}}==
{{trans|Perl}}
<langsyntaxhighlight lang="ruby">func tree(img, x, y, scale=6/10, len=400, angle=270) {
 
len < 1 && return()
Line 3,269 ⟶ 3,601:
tree(img, width/2, height)
 
File('tree.png').write(img.png, :raw)</langsyntaxhighlight>
 
=={{header|Smalltalk}}==
Line 3,275 ⟶ 3,607:
This example is coded for Squeak Smalltalk.
 
<langsyntaxhighlight lang="smalltalk">
Object subclass: #FractalTree
instanceVariableNames: ''
Line 3,281 ⟶ 3,613:
poolDictionaries: ''
category: 'RosettaCode'
</syntaxhighlight>
</lang>
 
Methods for FractalTree class:
 
<langsyntaxhighlight lang="smalltalk">
tree: aPoint length: aLength angle: anAngle
| p a |
Line 3,311 ⟶ 3,643:
self tree: 700@700 length: 200 angle: 0.
]
</syntaxhighlight>
</lang>
 
Now open a new Workspace and enter:
 
<langsyntaxhighlight lang="smalltalk">
FractalTree new draw.
</syntaxhighlight>
</lang>
 
=={{header|SVG}}==
Line 3,325 ⟶ 3,657:
 
<div style="clear:both;"></div>
<langsyntaxhighlight lang="xml"><?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">
Line 3,374 ⟶ 3,706:
</g>
</svg></langsyntaxhighlight>
 
=={{header|Swift}}==
[http://i.imgur.com/F8Fyn1i.png Image] - Link, since uploads seem to be disabled currently.
In a playground:
<langsyntaxhighlight lang="swift">extension CGFloat {
func degrees_to_radians() -> CGFloat {
return CGFloat(M_PI) * self / 180.0
Line 3,431 ⟶ 3,763:
let tree = Tree(frame: CGRectMake(0, 0, 300, 300))
tree
</syntaxhighlight>
</lang>
 
=={{header|Standard ML}}==
Works with PolyML
<langsyntaxhighlight Standardlang="standard MLml">open XWindows;
open Motif;
 
Line 3,479 ⟶ 3,811:
end ;
 
demoWindow ();</langsyntaxhighlight>
 
=={{header|Tcl}}==
{{libheader|Tk}}
<langsyntaxhighlight lang="tcl">package require Tk
 
set SIZE 800
Line 3,514 ⟶ 3,846:
pack [canvas .c -width $SIZE -height $SIZE]
draw_tree .c [expr {$SIZE/2}] [expr {$SIZE-10}] 0.0 -1.0 $INITIAL_LENGTH \
[expr {3.1415927 / 8}] $BRANCHES</langsyntaxhighlight>
 
=={{header|TUSCRIPT}}==
Image is created in SVG-format
<langsyntaxhighlight lang="tuscript">
$$ MODE TUSCRIPT
dest="fracaltree.svg"
Line 3,556 ⟶ 3,888:
WRITE/NEXT d "</svg>"
ENDACCESS d
</syntaxhighlight>
</lang>
 
=={{header|TypeScript}}==
{{trans|JavaScript}}
<langsyntaxhighlight JavaScriptlang="javascript">// Set up canvas for drawing
var canvas: HTMLCanvasElement = document.createElement('canvas')
canvas.width = 600
Line 3,596 ⟶ 3,928:
ctx.stroke()
 
</syntaxhighlight>
</lang>
 
=={{header|Wren}}==
{{trans|Kotlin}}
{{libheader|DOME}}
<langsyntaxhighlight ecmascriptlang="wren">import "graphics" for Canvas, Color
import "dome" for Window
import "math" for Math
Line 3,634 ⟶ 3,966:
}
 
var Game = FractalTree.new(800, 600)</langsyntaxhighlight>
 
=={{header|XPL0}}==
[[File:FtreeXPL0.png|200px|thumb|right|Output]]
<langsyntaxhighlight XPL0lang="xpl0">include c:\cxpl\codes;
 
proc DrawBranch(Lev, Dir, Len, X, Y);
Line 3,658 ⟶ 3,990:
if ChIn(1) then []; \wait for keystroke
SetVid(3); \restore normal text mode
]</langsyntaxhighlight>
 
=={{header|Yabasic}}==
<syntaxhighlight lang Yabasic>clear screen
width = 512 : height = 512 : crad = 0.01745329
open window width, height
window origin "cc"
 
sub drawTree(x, y, deg, n)
local x2, y2
if n then
x2 = x + cos(deg * crad) * n * 15 * ran(.5)
y2 = y + sin(deg * crad) * n * 15 * ran(.5)
line x, y, x2, y2
drawTree(x2, y2, deg - 20, n - 1)
drawTree(x2, y2, deg + 20, n - 1)
endif
end sub
 
repeat
clear window
drawTree(0, height/3, -90, 10)
until upper$(inkey$(1)) = "Q"</syntaxhighlight>
 
=={{header|zkl}}==
Line 3,664 ⟶ 4,019:
{{trans|BBC BASIC}}{{trans|XPL0}}
[[File:FractalTree.zkl.jpg|250px|thumb|right]]
<langsyntaxhighlight lang="zkl">fcn fractalTree(){
scale:=0.76;
sizeX:=400; sizeY:=300;
Line 3,681 ⟶ 4,036:
branch(sizeX,0,sizeY/2,90.0,10);
bitmap.write(File("foo.ppm","wb"));
}();</langsyntaxhighlight>
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.
 
=={{header|ZX Spectrum Basic}}==
{{trans|BASIC256}}
<langsyntaxhighlight lang="zxbasic">10 LET level=12: LET long=45
20 LET x=127: LET y=0
30 LET rotation=PI/2
Line 3,716 ⟶ 4,071:
1030 PLOT x,y: DRAW xn-x,y-yn
1040 LET x=xn: LET y=yn
1050 RETURN </langsyntaxhighlight>
 
[[Category:Geometry]]
1,480

edits