Fractal tree: Difference between revisions
m
→{{header|11l}}: Void
No edit summary |
Alextretyak (talk | contribs) m (→{{header|11l}}: Void) |
||
(102 intermediate revisions by 34 users not shown) | |||
Line 10:
* [[Pythagoras_tree|Pythagoras Tree]]
<br><br>
=={{header|11l}}==
{{trans|Nim}}
<syntaxhighlight lang="11l">-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, y; len, theta) -> Void
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’, 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'>
<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")</syntaxhighlight>
=={{header|Action!}}==
Action! language does not support recursion. Therefore an iterative approach with a stack has been proposed.
<syntaxhighlight lang="action!">DEFINE MAXSIZE="12"
INT ARRAY SinTab=[
0 4 9 13 18 22 27 31 36 40 44 49 53 58 62 66 71 75 79 83
88 92 96 100 104 108 112 116 120 124 128 132 136 139 143
147 150 154 158 161 165 168 171 175 178 181 184 187 190
193 196 199 202 204 207 210 212 215 217 219 222 224 226
228 230 232 234 236 237 239 241 242 243 245 246 247 248
249 250 251 252 253 254 254 255 255 255 256 256 256 256]
INT ARRAY xStack(MAXSIZE),yStack(MAXSIZE),angleStack(MAXSIZE)
BYTE ARRAY lenStack(MAXSIZE),dirStack(MAXSIZE)
BYTE stacksize=[0]
INT FUNC Sin(INT a)
WHILE a<0 DO a==+360 OD
WHILE a>360 DO a==-360 OD
IF a<=90 THEN
RETURN (SinTab(a))
ELSEIF a<=180 THEN
RETURN (SinTab(180-a))
ELSEIF a<=270 THEN
RETURN (-SinTab(a-180))
ELSE
RETURN (-SinTab(360-a))
FI
RETURN (0)
INT FUNC Cos(INT a)
RETURN (Sin(a-90))
BYTE FUNC IsEmpty()
IF stacksize=0 THEN
RETURN (1)
FI
RETURN (0)
BYTE FUNC IsFull()
IF stacksize=MAXSIZE THEN
RETURN (1)
FI
RETURN (0)
PROC Push(INT x,y,angle BYTE len,dir)
IF IsFull() THEN Break() FI
xStack(stacksize)=x yStack(stacksize)=y
angleStack(stacksize)=angle lenStack(stacksize)=len
dirStack(stacksize)=dir
stacksize==+1
RETURN
PROC Pop(INT POINTER x,y,angle BYTE POINTER len,dir)
IF IsEmpty() THEN Break() FI
stacksize==-1
x^=xStack(stacksize) y^=yStack(stacksize)
angle^=angleStack(stacksize) len^=lenStack(stacksize)
dir^=dirStack(stacksize)
RETURN
PROC DrawTree(INT x,y,len,angle,leftAngle,rightAngle)
BYTE depth,dir
Plot(x,y)
x==+Cos(angle)*len/256
y==-Sin(angle)*len/256
DrawTo(x,y)
Push(x,y,angle,len,0)
WHILE IsEmpty()=0
DO
Pop(@x,@y,@angle,@len,@dir)
IF dir<2 THEN
Push(x,y,angle,len,dir+1)
IF dir=0 THEN
angle==-leftAngle
ELSE
angle==+rightAngle
FI
len=13*len/16
Plot(x,y)
x==+Cos(angle)*len/256
y==-Sin(angle)*len/256
DrawTo(x,y)
IF IsFull()=0 THEN
Push(x,y,angle,len,0)
FI
FI
OD
RETURN
PROC Main()
BYTE CH=$02FC,COLOR1=$02C5,COLOR2=$02C6
Graphics(8+16)
Color=1
COLOR1=$BA
COLOR2=$B2
DrawTree(140,191,40,110,35,15)
DO UNTIL CH#$FF OD
CH=$FF
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Fractal_tree.png Screenshot from Atari 8-bit computer]
=={{header|Ada}}==
{{libheader|SDLAda}}
<syntaxhighlight lang="ada">with Ada.Numerics.Elementary_Functions;
with SDL.Video.Windows.Makers;
with SDL.Video.Renderers.Makers;
with SDL.Video.Rectangles;
with SDL.Events.Events;
procedure Fractal_Tree is
Width : constant := 600;
Height : constant := 600;
Level : constant := 13;
Length : constant := 130.0;
X_Start : constant := 475.0;
Y_Start : constant := 580.0;
A_Start : constant := -1.54;
Angle_1 : constant := 0.10;
Angle_2 : constant := 0.35;
C_1 : constant := 0.71;
C_2 : constant := 0.87;
Window : SDL.Video.Windows.Window;
Renderer : SDL.Video.Renderers.Renderer;
Event : SDL.Events.Events.Events;
procedure Draw_Tree (Level : in Natural;
Length : in Float;
Angle : in Float;
X, Y : in Float)
is
use SDL;
use Ada.Numerics.Elementary_Functions;
Pi : constant := Ada.Numerics.Pi;
X_2 : constant Float := X + Length * Cos (Angle, 2.0 * Pi);
Y_2 : constant Float := Y + Length * Sin (Angle, 2.0 * Pi);
Line : constant SDL.Video.Rectangles.Line_Segment
:= ((C.int (X), C.int (Y)), (C.int (X_2), C.int (Y_2)));
begin
if Level > 0 then
Renderer.Set_Draw_Colour (Colour => (0, 220, 0, 255));
Renderer.Draw (Line => Line);
Draw_Tree (Level - 1, C_1 * Length, Angle + Angle_1, X_2, Y_2);
Draw_Tree (Level - 1, C_2 * Length, Angle - Angle_2, X_2, Y_2);
end if;
end Draw_Tree;
procedure Wait is
use type SDL.Events.Event_Types;
begin
loop
while SDL.Events.Events.Poll (Event) loop
if Event.Common.Event_Type = SDL.Events.Quit then
return;
end if;
end loop;
delay 0.100;
end loop;
end Wait;
begin
if not SDL.Initialise (Flags => SDL.Enable_Screen) then
return;
end if;
SDL.Video.Windows.Makers.Create (Win => Window,
Title => "Fractal tree",
Position => SDL.Natural_Coordinates'(X => 10, Y => 10),
Size => SDL.Positive_Sizes'(Width, Height),
Flags => 0);
SDL.Video.Renderers.Makers.Create (Renderer, Window.Get_Surface);
Renderer.Set_Draw_Colour ((0, 0, 0, 255));
Renderer.Fill (Rectangle => (0, 0, Width, Height));
Draw_Tree (Level, Length, A_Start, X_Start, Y_Start);
Window.Update_Surface;
Wait;
Window.Finalize;
SDL.Finalise;
end Fractal_Tree;</syntaxhighlight>
=={{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}}==
<syntaxhighlight lang="rebol">width: 1000
height: 1000
trunkLength: 400
scaleFactor: 0.6
startingAngle: 1.5 * pi
deltaAngle: 0.2 * pi
drawTree: function [out x y len theta][
if len < 1 -> return null
x2: x + len * cos theta
y2: y + len * sin theta
'out ++ ~"<line x1='|x|' y1='|y|' x2='|x2|' y2='|y2|' style='stroke: white; stroke-width:1'/>\n"
drawTree out x2 y2 len*scaleFactor theta+deltaAngle
drawTree out x2 y2 len*scaleFactor theta-deltaAngle
]
svg: {
<?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 svg 0.5*width height trunkLength startingAngle
'svg ++ "</svg>"
write "fractal.svg" svg</syntaxhighlight>
{{out}}
[https://i.ibb.co/XLf31G1/fractal.png Fractal Tree output in Arturo]
=={{header|AutoHotkey}}==
[http://i.imgur.com/H7iJOde.png Image] - Link, since uploads seem to be disabled currently.
{{libheader|GDIP}}
<
#NoEnv
SetBatchLines, -1
Line 79 ⟶ 382:
Exit:
Gdip_Shutdown(pToken)
ExitApp</
=={{header|
==={{header|BASIC256}}===
[[File:Fractal tree BASIC-256.png|thumb|right|Asymmetric fractal tree image created by the BASIC-256 script]]
<
level = 12 : len =63 # initial values
Line 127 ⟶ 432:
line x,y,xn,yn
x = xn : y = yn
return</
==={{header|
<syntaxhighlight lang="run basic"> '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
WindowWidth = 500 'Run Basic max size 800 x 600
WindowHeight = 350
c = 255 '255 for white '0 for black
graphic #w, WindowWidth, WindowHeight
#w cls("black") 'black background color
#w color(c,c,c) 'changes color to white
level = 10 ' initial values
leng = 50
x = 230: y = 325 ' initial values x = 230: y = 285
pi = 3.1415
rotation = 3.1415/2
'A1 = pi/27 : A2 = pi/8 ' constants which determine shape
'C1 = 0.7 : C2 = 0.85 ' tree is drifted left
A1 = pi/9 : A2 = pi/9 ' constants which determine shape
C1 = 0.85 : C2 = 0.85 ' Symmetrical Tree
dim xs(level+1) : dim ys(level+1) ' stacks
print : print "Welcome to the Run BASIC Fractal Tree Program"
#w color("green") 'color green
gosub [tree]
render #w
' imgsave "Fractal_tree_BASIC-256.png", "PNG"
Print "Thank you and goodbye"
end
[tree]
xs(level) = x : ys(level) = y
gosub [putline]
if level>0 then
level = level - 1
leng = leng*C1
rotation = rotation - A1
gosub [tree]
leng = leng/C1*C2
rotation = rotation + A1 + A2
gosub [tree]
rotation = rotation - A2
leng = leng/C2
level = level + 1
end if
x = xs(level) : y = ys(level)
return
[putline]
yn = -1*sin(rotation)*leng + y
xn = cos(rotation)*leng + x
#w line(x,y,xn,yn)
x = xn : y = yn
return
'end of code
End</syntaxhighlight>
==={{header|BBC BASIC}}===
{{Out}}[[Image:fractal_tree_bbc.gif|right]]
<br>
Line 140 ⟶ 507:
{{works with|BBC BASIC for Windows}}
<
Spread = 25
Scale = 0.76
Line 146 ⟶ 513:
SizeY% = 300
Depth% = 10
</
VDU 23,22,SizeX%;SizeY%;8,16,16,128
Line 162 ⟶ 529:
PROCbranch(x2, y2, size * Scale, angle + Spread, depth% - 1)
ENDIF
ENDPROC</
==={{header|IS-BASIC}}===
<syntaxhighlight lang="is-basic">100 PROGRAM "Tree.bas"
110 OPTION ANGLE DEGREES
120 GRAPHICS HIRES 2
130 SET PALETTE 0,170
140 PLOT 640,10;ANGLE 90;
150 CALL TREE(200)
160 DEF TREE(N)
170 IF N<24 THEN EXIT DEF
180 PLOT FORWARD N;RIGHT 25;
190 CALL TREE(N*.75)
200 PLOT LEFT 50;
210 CALL TREE(N*.75)
220 PLOT RIGHT 25,BACK N,
230 END DEF</syntaxhighlight>
=={{header|C}}==
Line 168 ⟶ 551:
{{libheader|SGE}} or {{libheader|cairo}}
<
#ifdef WITH_CAIRO
#include <cairo.h>
Line 179 ⟶ 562:
#include <math.h>
#ifndef M_PI
#define
#endif
#define SIZE 800 // determines size of window
#define SCALE 5 // determines how quickly branches shrink (higher value means faster shrinking)
Line 244 ⟶ 627:
0.0, -1.0,
INITIAL_LENGTH,
BRANCHES);
SDL_UpdateRect(surface, 0, 0, 0, 0);
Line 268 ⟶ 651:
SDL_Quit();
return 0;
}</
=={{header|C++}}==
[[File:fracTree_cpp.png|320px]]
<
#include <windows.h>
#include <string>
Line 282 ⟶ 664:
//--------------------------------------------------------------------------------------------------
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
//--------------------------------------------------------------------------------------------------
Line 404 ⟶ 788:
public:
fractalTree() { _ang = DegToRadian( 24.0f ); }
float DegToRadian( float degree ) { return degree * (
void create( myBitmap* bmp )
Line 465 ⟶ 849:
}
//--------------------------------------------------------------------------------------------------
</syntaxhighlight>
=={{header|Ceylon}}==
{{trans|Java}}
{{libheader|Swing}} {{libheader|AWT}}
Be sure to import java.desktop and ceylon.numeric in your module.ceylon file.
<syntaxhighlight lang="ceylon">import javax.swing {
JFrame { exitOnClose }
}
import java.awt {
Color { white, black },
Graphics
}
import ceylon.numeric.float {
cos,
toRadians,
sin
}
shared void run() {
value fractalTree = object extends JFrame("fractal tree") {
background = black;
setBounds(100, 100, 800, 600);
resizable = false;
defaultCloseOperation = exitOnClose;
shared actual void paint(Graphics g) {
void drawTree(Integer x1, Integer y1, Float angle, Integer depth) {
if (depth <= 0) {
return;
}
value x2 = x1 + (cos(toRadians(angle)) * depth * 10.0).integer;
value y2 = y1 + (sin(toRadians(angle)) * depth * 10.0).integer;
g.drawLine(x1, y1, x2, y2);
drawTree(x2, y2, angle - 20, depth - 1);
drawTree(x2, y2, angle + 20, depth - 1);
}
g.color = white;
drawTree(400, 500, -90.0, 9);
}
};
fractalTree.visible = true;
}</syntaxhighlight>
=={{header|Clojure}}==
{{trans|Java}}
{{libheader|Swing}} {{libheader|AWT}}
<
'javax.swing.JFrame)
Line 495 ⟶ 929:
(.show)))
(fractal-tree 9)</
=={{header|Common Lisp}}==
{{libheader|lispbuilder-sdl}}
{{trans|Clojure}}
<
(defun deg-to-radian (deg)
Line 536 ⟶ 971:
(fractal-tree 9)
</syntaxhighlight>
=={{header|D}}==
===SVG Version===
{{trans|
<
enum width = 1000, height = 1000; // Image dimension.
Line 562 ⟶ 998:
tree(width / 2.0, height, length, 3 * PI / 2);
"</svg>".writeln;
}</
===Turtle Version===
This uses the turtle module from the Dragon Curve task, and the module from the Grayscale Image task.
{{trans|Logo}}
<
void tree(Color)(Image!Color img, ref Turtle t, in uint depth,
Line 586 ⟶ 1,022:
img.tree(t, 10, 80, 0.7, 30);
img.savePGM("fractal_tree.pgm");
}</
===Alternative version===
{{trans|Java}}
Using DFL.
<
import std.math;
Line 633 ⟶ 1,069:
}
return result;
}</
=={{header|EasyLang}}==
[https://easylang.dev/show/#cod=jY/BDsIgEETvfMUkXrRNCa1y8KBH/4NQqiQUDCXa/r2w6cEmHuSwGWbnLcsOt6h0Ug4pGsN2YDq4ECGlZM8YNNmYsaA3d3hwcAbADllfIYrOx1lv3rZPj+xWEPy0+mN4EbxeZ9QX6DDRrIqyLT/muo/K92EcUGdYHtb4UuKT9X/GyxJfj20Wb9CJPKBB+6tbb7qccZbsaGL+XvAgWXztjCJBrBRoBc6lkkdAl9EP Run it]
<syntaxhighlight lang="text">
# Fractal tree
#
color 555
proc tree x y deg n . .
if n > 0
linewidth n * 0.4
move x y
x += cos deg * n * 1.3 * (randomf + 0.5)
y += sin deg * n * 1.3 * (randomf + 0.5)
line x y
tree x y deg - 20 n - 1
tree x y deg + 20 n - 1
.
.
timer 0
on timer
clear
tree 50 10 90 10
timer 2
.
</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|
<
let (width, height) = 1000., 1000. // image dimension
Line 657 ⟶ 1,242:
xmlns='http://www.w3.org/2000/svg'>"
tree (width/2.) height length (3.*pi/2.)
printfn "</svg>"</
=={{header|Fantom}}==
<
using fwt
using gfx
Line 696 ⟶ 1,281:
}
}
</syntaxhighlight>
=={{header|FreeBASIC}}==
{{trans|BBC BASIC}}
<
' compile with: fbc -s gui
Line 738 ⟶ 1,324:
windowtitle ("Fractal Tree, hit any key to end program")
Sleep
End</
=={{header|Frege}}==
Line 744 ⟶ 1,330:
{{Works with|Frege|3.23.888-g4e22ab6}}
<
import Java.IO
Line 822 ⟶ 1,408:
drawTree g t 16
f <- File.new "FractalTreeFrege.png"
void $ ImageIO.write buffy "png" f</
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">
// Draw Fractal Tree in Frink
// Define the tree function
FractalTree[x1, y1, angleval, lengthval, graphicsobject] :=
{
if lengthval > 1
{
// Define current line end points (x2 and y2)
x2 = x1 + ((cos[angleval degrees]) * lengthval)
y2 = y1 + ((sin[angleval degrees]) * lengthval)
// Draw line - notice that graphicsobject is the graphics object passed into the function.
graphicsobject.line[x1,y1,x2,y2]
// Calculate branches. You can change the lengthval multiplier factor and angleval summand to create different trees
FractalTree[x2, y2, angleval - 20, lengthval * 0.7, graphicsobject]
FractalTree[x2, y2, angleval + 20, lengthval * 0.7, graphicsobject]
}
}
// Create graphics object
g = new graphics
// Start the recursive function. In Frink, a -90° angle moves from the bottom of the screen to the top.
FractalTree[0, 0, -90, 30, g]
// Show the final tree
g.show[]
</syntaxhighlight>
=={{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]]
<
// Files required to build supporting package raster are found in:
Line 864 ⟶ 1,529:
ftree(g, x2, y2, distance*frac, direction+angle, depth-1)
}
}</
=={{header|Haskell}}==
An elegant yet universal monoidal solution.
{{libheader|Gloss}}
<
type Model = [Picture -> Picture]
Line 880 ⟶ 1,545:
, Translate 0 100 . Scale 0.5 0.5 . Rotate (-30) ]
main = animate (InWindow "Tree" (800, 800) (0, 0)) white $ tree1 . (* 60)</
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.
<
tree2 t = fractal 8 branches $ Line [(0,0),(0,100)]
where branches = [ Translate 0 100 . Scale 0.75 0.75 . Rotate t
Line 906 ⟶ 1,571:
pentagon = Line [ (sin a, cos a) | a <- [0,2*pi/5..2*pi] ]
x = 2*cos(pi/5)
s = 1/(1+x)</
'''Alternative solution'''
Line 914 ⟶ 1,579:
{{libheader|HGL}}
<
import Graphics.HGL.Run
import Control.Arrow
Line 944 ⟶ 1,609:
(\w -> setGraphic w (overGraphics ( map polyline $ pts (n-1))) >> getKey w)
main = fractalTree 10</
=={{header|Icon}} and {{header|Unicon}}==
<
WOpen("size=800,600", "bg=black", "fg=white") | stop("*** cannot open window")
drawtree(400,500,-90,9)
Line 964 ⟶ 1,629:
}
return
end</
{{libheader|Icon Programming Library}}
Line 973 ⟶ 1,638:
=={{header|J}}==
<
coinsert'jgl2'
L0=: 50 NB. initial length
A0=: 1r8p1 NB. initial angle: pi divided by 8
Line 980 ⟶ 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
wd {{)n
pc P closeok;
cc C
pshow;
}}
gllines <.(10 + ,/"2 P-"1<./,/P)</syntaxhighlight>
See the [[Talk:Fractal tree#J Explanation|talk page]] for some implementation notes.
Line 1,002 ⟶ 1,668:
=={{header|Java}}==
{{libheader|Swing}} {{libheader|AWT}}
<
import java.awt.Graphics;
import javax.swing.JFrame;
Line 1,033 ⟶ 1,699:
new FractalTree().setVisible(true);
}
}</
=={{header|JavaScript}}==
Implementation using HTML5 canvas element to draw tree structure.
<
<body>
<canvas id="canvas" width="600" height="500"></canvas>
Line 1,045 ⟶ 1,711:
var context = elem.getContext('2d');
context.fillStyle = '#
context.lineWidth = 1;
Line 1,073 ⟶ 1,739:
</body>
</html></
=={{header|jq}}==
The following generates SVG, which can be viewed by following the link below.
<
# len defines the trunk size;
# scale defines the branch length relative to the trunk;
Line 1,114 ⟶ 1,780:
;
main(1000; 1000; 400; 6/10)</
{{out}}
$ jq -r -n -r -f Fractal_tree_svg.jq > Fractal_tree.svg
[https://drive.google.com/file/d/0BwMI1gZaY2-MWEI4d1kxNHZ4cGs/view?usp=sharing Fractal_tree.svg]
=={{header|Julia}}==
{{trans|F#}}
<syntaxhighlight lang="julia">
const width = height = 1000.0
const trunklength = 400.0
const scalefactor = 0.6
const startingangle = 1.5 * pi
const 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)
end
end
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
</syntaxhighlight>
=={{header|Kotlin}}==
{{trans|Java}}
<
import java.awt.Color
Line 1,153 ⟶ 1,851:
fun main(args: Array<String>) {
FractalTree().isVisible = true
}</
=={{header|Lambdatalk}}==
<syntaxhighlight lang="lisp">
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
</syntaxhighlight>
=={{header|Liberty BASIC}}==
LB includes Logo-type turtle commands, so can be drawn that way as well as that shown here.
<syntaxhighlight lang="lb">
NoMainWin
sw = 640 : sh = 480
Line 1,194 ⟶ 1,944:
End If
End Sub
</syntaxhighlight>
=={{header|Lingo}}==
<
-- Creates an image of a fractal tree
-- @param {integer} width
Line 1,223 ⟶ 1,973:
_drawTree(img, x2, y2, angle+spreadAngle, depth-1, size*ScaleFactor, spreadAngle, scaleFactor)
end if
end</
Usage:
<
initSize = 7.0
spreadAngle = 35*PI/180
scaleFactor = 0.95
img = fractalTree(480, 380, fractalDepth, initSize, spreadAngle, scaleFactor)</
=={{header|Logo}}==
<
if :depth=0 [stop]
setpensize round :depth/2
Line 1,245 ⟶ 1,995:
clearscreen
tree 10 80 0.7 30</
=={{header|Lua}}==
===Bitmap===
Needs LÖVE 2D Engine
<
g, angle = love.graphics, 26 * math.pi / 180
wid, hei = g.getWidth(), g.getHeight()
Line 1,287 ⟶ 2,038:
g.draw( canvas )
end
</syntaxhighlight>
===ASCII===
Using the Bitmap class and text renderer from [[Bitmap/Bresenham%27s_line_algorithm#Lua|here]], then extending...
<syntaxhighlight lang="lua">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]='█'})</syntaxhighlight>
{{out}}
Shown at 25% scale:
<pre style="font-size:25%">................................................................................................................................................................................................................................................................................................................................................................................................
................................................................................................................................................................................................................................................................................................................................................................................................
................................................................................................................................................................................................................................................................................................................................................................................................
................................................................................................................................................................................................................................................................................................................................................................................................
................................................................................................................................................................................................................................................................................................................................................................................................
................................................................................................................................................................................................................................................................................................................................................................................................
................................................................................................................................................................................................................................................................................................................................................................................................
................................................................................................................................................................................................................................................................................................................................................................................................
................................................................................................................................................................................................................................................................................................................................................................................................
................................................................................................................................................................................................................................................................................................................................................................................................
................................................................................................................................................................................................................................................................................................................................................................................................
................................................................................................................................................................................................................................................................................................................................................................................................
................................................................................................................................................................................................................................................................................................................................................................................................
....................................................................................................................................................................................................................................................................................................█.....██.█.█................................................................................
.................................................█.█...██.█.█.████.█.█.██...█.█...................................................................................................................................................................................................................██.█..█.█.██.███..............................................................................
..............................................█...█....██.███..██..███.██....█...█..................................................................................................................................................................................................................██...█...█.█................................................................................
...........................................███.█..█..█████.█.█.██.█.█.█████..█..█.███................................................................................................................................................................................................................█...█...██.................................................................................
.......................................█.█.██.██..█.█..██.███..██..███.██..█.█..██.██.█.█.............................................................................................................................................................................................................█..█...██.................................................................................
....................................█.█.█..█.█.█..█.█...█..█...██...█..█...█.█..█.█.█..█.█.█..........................................................................................................................................................................................................█..█...██.................................................................................
................................█.██.█.██..█.█..█████...█████..██..█████...█████..█.█..██.█.██.█.......................................................................................................................................................................................................█.█...█..................................................................................
.............................█...█.███..█..█.█.████.█...████.█.██.█.████...█.████.█.█..█..███.█...█...................................................................................................................................................................................█..█.█........█..███...█..............██....█......█..█...█...............................................
............................█.█..██.██..██.█.██.███..█..████..████..████..█..███.██.█.██..██.██..█.█.................................................................................................................................................................................███.██.█..█.....██.██...█........█.....█..█.███.....███....█.█....█........................................
.............................██..█.██.█.██.█.█.██.█...█.█..█..████..█..█.█...█.██.█.█.██.█.██.█..██....................................................................................................................................................................................█.████.██..█..█..███..█.........█....█...█..██....███..█.██..█.█.........................................
.........................█....█..█..█.█.█.██.█.██.█....██..█...██...█..██....█.██.█.██.█.█.█..█..█....█.................................................................................................................................................................................██..█..█...███..██...█.......███....█...█..█.█...█.█..████...█...█......................................
.......................█.█.█...███...█.██..████..██....██..█...██...█..██....██..████..██.█...███...█.█.█........................................................................................................................................................................██.....██████.█...█.█..█.█..█.........██...█...█..███...█.██.███....█..███.....................................
......................█.███...█████..█..█..█████.█.█...██...█..██..█...██...█.█.█████..█..█..█████...███.█.......................................................................................................................................................................█......██...███...█.█..█.█..█..........█...█...█..█..█.█.█.███.█...██..█.......................................
....................██.█.██.....██.██.█.█..█.█..████...██...█..██..█...██...████..█.█..█.█.██.██.....██.█.██.....................................................................................................................................................................█.......█....███..█..██..█.█............█.█....█.█....██..████.█...███.█..........█............................
.....................███..█......█...████...██....███.█..█...█.██.█...█..█.███....██...████...█......█..███...............................................................................................................................................................█.█....█..█....█.....██.█...██...██............█.█....██.....██.█████.█...█.██............█.█....█....................
.................█..████..██.....█.....███..█......██.█..█...█.██.█...█..█.██......█..███.....█.....██..████..█............................................................................................................................................................█.█...█.███...█......█.█...██...████...........██....██....███.█.██.██..█..██............██....███...................
................█.█....████.█....█.......██.██......███..█...██..██...█..███......██.██.......█....█.████....█.█...................................................................................................................................................█.....██████..█....██..█.....███...█....██.██...........█....█..██.████..█.███..█..██.............█...█......................
.................██.....█.███.....█.......████.......█.█.█....█..█....█.█.█.......████.......█.....███.█.....██.....................................................................................................................................................█......█..██.███....███......██...█....██..█...........█....█....██.██..█..██.██..█..............█..█....█..................
..................█......██..██...█.......█.██.......█..█.█...█..█...█.█..█.......██.█.......█...██..██......█.......................................................................................................................................................█......█..███..██....██.....█.█..█.....█..█..........██...█......█.██..█.█████.█.██.............█.█....█.███...............
..............█....█......█....██..█......██.██......█...██...█..█...██...█......██.██......█..██....█......█....█................................................................................................................................................███████.███...███████████████..█.█..█.....█...█......█.█.█...█......█.█.██..█.████..██............█.█.█...██..................
.............███....█.....█......█.█.......█..█......█....█...█..█...█....█......█..█.......█.█......█.....█....███......................................................................................................................................................█████...█.........█...█████.█......█...█....██.██..█..█.......██.██.██.██████.█.█..........██.██..█....................
...........██.█.███..█....█.......██.......█...█.....█.....█..█..█..█.....█.....█...█.......██.......█....█..███.█.██....................................................................................................................................................████████.█........█......████......██...█....██.█..█..█.......█..█████.███..█.██...........█..█..█..█...██.............
.............██....███.....█.......██......█...█.....█.....█..█..█..█.....█.....█...█......██.......█.....███....██...........................................................................................................................................................█..█████.....█........██.......██...█....█.█..█.█........█.███.█.██.█..█.█......█....█...█..███...█...............
..............█.......██...█........█......█....█....█......█.█..█.█......█....█....█......█........█...██.......█.......................................................█..█....█..█..█..█..█..█.█..█..█..█..█....█..█...........................................█.█..........█...█..███...█........█......███████.....█.█.█.█........██..█.██.█..████......█.....█...████████████.............
.........██....█........██..█........█.....█.....█...█.......██..██.......█...█.....█.....█........█..██........█....██...................................................██......██....██....██.█....██....██......██.............................................██...........█..█.....████........███.....█....███....██.█.█........█...███.████████...█████...█...██..█....██...............
...........█...█..........█.█........█.....█......█..█........█..█........█..█......█.....█........█.█..........█...█.....................................................█........█....█.....█..█.....█....█........█..............................................█............█.█........█........█..█....█.......██...█..█........██...█████....██████........█.██.█.█.....█................
.........██████.█..........██.........█....█......█..█........█..█........█..█......█....█.........██..........█.██████...................................................█........█....█.....█..█.....█....█........█.......................................█.......█............█.█........█.......█...█...█.........█...█.█........█.████...█....██...........███...██.....█.................
...............███..........██.........█...█.......█.█........████........█.█.......█...█.........██..........███...................................................█.....█.......██....██....█..█....██....██.......█.....█................................███..██...█............██.........█......█....█..█..........█..█.█.......█.██.██...█...██............█.....██.....█....█............
.................██..........█..........█..█........██........████........██........█..█..........█..........██......................................................█....█........█....██....█..█....██....█........█....█....................................██..████.............██.........█.....█.....█..█..........██.██......███.██.█...█..█.█...........█......█.....█....█.............
.......██..........█..........█.........█..█........██........████........██........█..█.........█..........█..........██..........................................█████..█......███████████..█..█..███████████......█..█████...................................███...███............█.........█.....█......█.█............███......█...██.█...█.█...█.........█.......█....█.████..............
.........█..........█.........█..........█.█.........█........█..█........█.........█.█..........█.........█..........█.........................................█.█.....███........██████...███..███...██████........███.....█.█...................................██....██...........█.........█.....█......██.............██....██....█..█...██....█........█.......█....███....███...........
.......██████........█.........█..........██.........█........█..█........█.........██..........█.........█........██████........................................█........█........███..█.....█..█.....█..███........█........█......................................█.....██..........█.........█....█.......█.............██..██......█..█...█.....█.......█........█...█.....................
.............██.......█........█..........██..........█......█....█......█..........██..........█........█.......██..............................................█.........█......█..█...█.....██.....█...█..█......█.........█.......................................██████████.......█..........█...█........█............█.██.......█...█..█......█......█.........█..█......................
...............██████..█........█..........█..........█......█....█......█..........█..........█........█..██████..........................................█..█..█.█..█.....█..█..█..█....█....██....█....█..█..█..█.....█..█.█..█..█...............................██..........███.....█..........█..█........█............█.██.......█...█..█......█.....█..........█.█.......................
......███...███......██████......█.........█..........█......█....█......█..........█.........█......██████......███...███..................................██...█..██.......█..██...█.....█...██...█.....█...██..█.......██..█...██..........................██████...............██....█..........█.█.........█...........██.█.......█...█.█.......█....█..........█.█........................
.........███...............███...█..........█.........█.....█......█.....█.........█..........█...███...............███.....................................███..█...█.......█..███..█.....█..█..█..█.....█..███..█.......█...█..███.............................█...................███..█.........█.█.........█...........█...█......█...█.█........█..█...........██.........................
.......██.....................██..█.........█..........█....█......█....█..........█.........█..██.....................██.............................█...███..███...█.....█..███..███......█.█..█.█......███..███..█.....█...███..███...█......................█.......................███..........██..........█.........█.....█....█....██.........█.█............█.............█............
................................███.........█..........█...█........█...█..........█.........███.......................................................█....█....█...█....█....█.....█.......█....█.......█.....█....█....█...█....█....█......................█...........................██.........█..........█.........█......█...█....█..........██............█.............█.............
...................................██.......█..........█...█........█...█..........█.......██...........................................................██..█.....█..█..██.....█......█......█....█......█......█.....██..█..█.....█..██.....................................................██.......█...........█.......█........█..█....█..........█............█.............██████.........
.....................................█.......█..........█..█........█..█..........█.......█.......................................................█.████..███.....█..███..███..█......█......█....█......█......█..███..███..█.....███..████.█.................................................█.......█..........█.......█........█.█.....█..........█............█............██..█...........
......................................█......█..........█.█..........█.█..........█......█.........................................................█........█......█.█.........█.......█.....█....█.....█.......█.........█.█......█........█...................................................█......█...........█.....█..........██.....█.........█............█............█.███████........
.......................................██....█..........█.█..........█.█..........█....██..........................................................█....█....█......█.......█..█........█....█....█....█........█..█.......█......█....█....█....................................................██.....█..........█.....█...........█....█..........█...........█............███...............
.........................................█...█..........█.█..........█.█..........█...█............................................................█.....█....█.....█........█.█........█....█....█....█........█.█........█.....█....█.....█......................................................█....█...........█...█............█....█..........█..........█...........██..................
..........................................█...█..........█............█..........█...█.......................................................█.....█.....███...█...█.███.....███.........███.█....█.███.........███.....███.█...█...███.....█.....█.................................................█...█...........█...█.............█...█.........█...........█.........██....................
...........................................█..█..........█............█..........█..█.........................................................█....█...██...██.█.████...██.██..███...████...██....██...████...███..██.██...████.█.██...██...█....█...................................................██..█...........█.█..............█...█.........█..........█........██......................
............................................█.█..........█............█..........█.█........................................................██████.█..........███.█.......███..█..███........██████........███..█..███.......█.███..........█.██████...................................................█.█...........█.█..............█...█........█..........█........█................██......
.............................................█.█.........█............█.........█.█...............................................................██.........█...█...........███.█...........██████...........█.███...........█...█.........██..........................................................██............█...............█..█.........█.........█.....█████...............█........
..............................................██.........█............█.........██..................................................................█........█...█.............██............█....█............██.............█...█........█.............................................................██...........█...............█..█.........█.........██████.....████......██████........
...............................................█.........█............█.........█...................................................................█.......█....█.............██...........█......█...........██.............█....█.......█..............................................................█...........█................█.█........█........███..............██████......███.....
................................................█........█............█........█.....................................................................█..████.....█..........███.█...........█......█...........█.███..........█.....████..█................................................................█..........█................█.█........█......██.....................██..............
................................................█........█............█........█.....................................................................█.....█.....█............█..█.........█........█.........█..█............█.....█.....█.................................................................█.........█................█.█........█....██.........................███...........
.................................................█.......█............█.......█...........................................................█...........█...█......█............█...█.......█..........█.......█...█............█......█...█...........█......................................................█.........█................█.█.......█....█..............................███........
..................................................█......█............█......█.............................................................█...........█.........█.................█.....█............█.....█.................█.........█...........█........................................................█........█.................█........█..██................................█.........
...................................................█.....█............█.....█............................................................███████.....█████.......█.................█.....█............█.....█.................█.......█████.....███████.......................................................█.......█.................█........███............................................
...................................................█.....█............█.....█................................................................████████.....███....█..................█...█..............█...█..................█....███.....████████............................................................█......█.................█.......██..............................................
....................................................█....█............█....█..................................................................██.............█████...................█.█................█.█...................█████.............██..............................................................█......█................█.......█...............................................
.....................................................█...█............█...█.............................................................█.....██.................█...................█.█................█.█...................█.................██.....█.........................................................█.....█................█......█................................................
.....................................................█...█............█...█..............................................................█....█...................█...................█..................█...................█...................█....█..........................................................█.....█................█.....█.................................................
......................................................█..█............█..█.............................................................█████.██...................█...................█..................█...................█...................██.█████.........................................................█....█................█.....█.................................................
.......................................................█.█............█.█.................................................................█████....................█..................█..................█..................█....................█████.............................................................█...█................█....█..................................................
........................................................██............██.....................................................................██.....................█.................█..................█.................█.....................██.................................................................█..█................█...█...................................................
........................................................██............██....................................................................█..█....................█.................█..................█.................█....................█..█.................................................................█.█................█...█...................................................
.........................................................█............█........................................................................█.....................█................█..................█................█.....................█....................................................................█.█................█..█....................................................
.........................................................█............█.........................................................................█.....................█...............█..................█...............█.....................█......................................................................██................█.█.....................................................
..........................................................█..........█...............................................................█...........█....................█...............█..................█...............█....................█...........█............................................................█................██......................................................
..........................................................█..........█................................................................█..........█.....................█..............█..................█..............█.....................█..........█.............................................................█................██......................................................
..........................................................█..........█..............................................................█████.........███..................█..............█..................█..............█..................███.........█████............................................................█...............█.......................................................
...........................................................█.........█...................................................................██...████...███..............█████...........█..................█...........█████..............███...████...██.................................................................█...............█.......................................................
...........................................................█........█......................................................................███..........███.....██████.....███........█..................█........███.....██████.....███..........███...................................................................█..............█........................................................
...........................................................█........█.....................................................................█................█████..............██......█..................█......██..............█████................█...................................................................█.............█........................................................
............................................................█.......█....................................................................█................█.....................███...█..................█...███.....................█................█..................................................................█.............█........................................................
............................................................█......█...................................................................██.................█........................████..................████........................█.................██.................................................................█...........█.........................................................
............................................................█......█.................................................................███.................█............................█..................█............................█.................███...............................................................█...........█.........................................................
.............................................................█.....█...................................................................█................█..............................█................█..............................█................█.................................................................█...........█.........................................................
.............................................................█.....█...................................................................█...............█...............................█................█...............................█...............█..................................................................█.........█..........................................................
.............................................................█....█....................................................................█...............█................................█..............█................................█...............█..................................................................█.........█..........................................................
..............................................................█...█.....................................................................█.............█..................................█.............█.................................█.............█...................................................................█........█...........................................................
..............................................................█...█...................................................................█████.........██...................................█............█...................................██.........█████..................................................................█.......█...........................................................
..............................................................█..█.........................................................................██...████.█....................................█..........█....................................█.████...██.......................................................................█.......█...........................................................
...............................................................█.█...........................................................................███.....█.....................................█.........█....................................█.....███.........................................................................█......█............................................................
...............................................................█.█..........................................................................█........█.....................................█........█.....................................█........█.........................................................................█.....█............................................................
...............................................................█.█.........................................................................█.........█......................................█.......█.....................................█.........█........................................................................█.....█............................................................
................................................................█.........................................................................█.........█........................................█.....█.......................................█.........█........................................................................█...█.............................................................
................................................................█.......................................................................███.........█........................................█....█........................................█.........███......................................................................█...█.............................................................
................................................................█.........................................................................█.........█.........................................█...█........................................█.........█........................................................................█...█.............................................................
................................................................█.........................................................................█........██..........................................█.█.........................................██........█.........................................................................█.█..............................................................
................................................................█.................................................................................█..█.........................................█.█........................................█..█.................................................................................█.█..............................................................
................................................................█................................................................................█...█..........................................█.........................................█...█................................................................................█.█..............................................................
................................................................█...............................................................................█....█..........................................█.........................................█....█................................................................................█...............................................................
................................................................█.............................................................................███.....█.........................................█........................................█.....███..............................................................................█...............................................................
................................................................█...............................................................................█.....███.......................................█......................................███.....█................................................................................█...............................................................
................................................................█.....................................................................................█.........................................█........................................█......................................................................................█...............................................................
................................................................█...............................................................................................................................█...............................................................................................................................█...............................................................
................................................................█...............................................................................................................................█...............................................................................................................................█...............................................................
................................................................█...............................................................................................................................█...............................................................................................................................█...............................................................
................................................................█...............................................................................................................................█...............................................................................................................................█...............................................................
................................................................█...............................................................................................................................█...............................................................................................................................█...............................................................
................................................................█...............................................................................................................................█...............................................................................................................................█...............................................................
................................................................█...............................................................................................................................█...............................................................................................................................█...............................................................
................................................................█...............................................................................................................................█...............................................................................................................................█...............................................................
................................................................█...............................................................................................................................█...............................................................................................................................█...............................................................
................................................................█...............................................................................................................................█...............................................................................................................................█...............................................................
................................................................█...............................................................................................................................█...............................................................................................................................█...............................................................
................................................................█...............................................................................................................................█...............................................................................................................................█...............................................................
................................................................█...............................................................................................................................█...............................................................................................................................█...............................................................
................................................................█...............................................................................................................................█...............................................................................................................................█...............................................................
................................................................█...............................................................................................................................█...............................................................................................................................█...............................................................
................................................................█...............................................................................................................................█...............................................................................................................................█...............................................................
................................................................................................................................................................................................................................................................................................................................................................................................
................................................................................................................................................................................................................................................................................................................................................................................................
................................................................................................................................................................................................................................................................................................................................................................................................
................................................................................................................................................................................................................................................................................................................................................................................................
................................................................................................................................................................................................................................................................................................................................................................................................
................................................................................................................................................................................................................................................................................................................................................................................................
................................................................................................................................................................................................................................................................................................................................................................................................</pre>
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<
pt : {_, _}, \[Theta]orient_: \[Pi]/2, \[Theta]sep_: \[Pi]/9,
depth_Integer: 9] := Module[{pt2},
Line 1,307 ⟶ 2,206:
]
Graphics[fractalTree[{0, 0}, \[Pi]/2, \[Pi]/9]]
</syntaxhighlight>
[[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,314 ⟶ 2,236:
{{libheader|Swing}}
{{libheader|AWT}}
<
options replace format comments java crossref symbols binary
Line 1,351 ⟶ 2,273:
RFractalTree().setVisible(isTrue)
return
</syntaxhighlight>
=={{header|Nim}}==
{{trans|Julia}}
<syntaxhighlight lang="nim">
import math
import 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.
</syntaxhighlight>
=={{header|OCaml}}==
{{libheader|ocaml-cairo}}
<
#load "bigarray.cma"
#load "cairo.cma"
Line 1,432 ⟶ 2,389:
Cairo_png.surface_write_to_file surf img_name
(*Cairo_png.surface_write_to_channel surf stdout*)</
=={{header|PARI/GP}}==
Line 1,446 ⟶ 2,403:
{{Works with|PARI/GP|2.7.4 and above}}
<
\\ Fractal tree (w/recursion)
\\ 4/10/16 aev
Line 1,480 ⟶ 2,437:
FractalTree(15,1500); \\FracTree3.png
}
</
{{Output}}
Line 1,495 ⟶ 2,452:
*** last result computed in 1,095 ms
</pre>
=={{header|Perl}}==
using the [http://search.cpan.org/~lds/GD-2.45/GD/Simple.pm GD::Simple] module.
<
my ($width, $height) = (1000,1000); # image dimension
Line 1,528 ⟶ 2,484:
tree($x, $y, $len*$scale, $angle+35);
tree($x, $y, $len*$scale, $angle-35);
}</
=={{header|Phix}}==
{{Trans|XPL0}}
{{libheader|Phix/pGUI}}
{{libheader|Phix/online}}
You can run this online [http://phix.x10.mx/p2js/fractaltree.htm here].
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\FractalTree.exw
-- ============================
--</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">include</span> <span style="color: #000000;">pGUI</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<span style="color: #004080;">Ihandle</span> <span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">canvas</span>
<span style="color: #004080;">cdCanvas</span> <span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">cdcanvas</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">drawTree</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">level</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">angle</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">len</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">xn</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">x</span> <span style="color: #0000FF;">+</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">len</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">cos</span><span style="color: #0000FF;">(</span><span style="color: #000000;">angle</span><span style="color: #0000FF;">)),</span>
<span style="color: #000000;">yn</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">y</span> <span style="color: #0000FF;">+</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">len</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">sin</span><span style="color: #0000FF;">(</span><span style="color: #000000;">angle</span><span style="color: #0000FF;">)),</span>
<span style="color: #000000;">red</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">255</span><span style="color: #0000FF;">-</span><span style="color: #000000;">level</span><span style="color: #0000FF;">*</span><span style="color: #000000;">8</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">green</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">level</span><span style="color: #0000FF;">*</span><span style="color: #000000;">12</span><span style="color: #0000FF;">+</span><span style="color: #000000;">100</span>
<span style="color: #7060A8;">cdCanvasSetForeground</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">red</span><span style="color: #0000FF;">*</span><span style="color: #000000;">#10000</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">green</span><span style="color: #0000FF;">*</span><span style="color: #000000;">#100</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">cdCanvasSetLineWidth</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">5</span><span style="color: #0000FF;">-</span><span style="color: #000000;">level</span><span style="color: #0000FF;">/</span><span style="color: #000000;">3</span><span style="color: #0000FF;">))</span>
<span style="color: #7060A8;">cdCanvasLine</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">480</span><span style="color: #0000FF;">-</span><span style="color: #000000;">y</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">xn</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">480</span><span style="color: #0000FF;">-</span><span style="color: #000000;">yn</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">level</span><span style="color: #0000FF;"><</span><span style="color: #000000;">12</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">drawTree</span><span style="color: #0000FF;">(</span><span style="color: #000000;">level</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">angle</span><span style="color: #0000FF;">-</span><span style="color: #000000;">0.4</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">len</span><span style="color: #0000FF;">*</span><span style="color: #000000;">0.8</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">xn</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">yn</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">--left</span>
<span style="color: #000000;">drawTree</span><span style="color: #0000FF;">(</span><span style="color: #000000;">level</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">angle</span><span style="color: #0000FF;">+</span><span style="color: #000000;">0.1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">len</span><span style="color: #0000FF;">*</span><span style="color: #000000;">0.8</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">xn</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">yn</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">--right</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">redraw_cb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000080;font-style:italic;">/*ih*/</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000080;font-style:italic;">/*posx*/</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">/*posy*/</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">cdCanvasActivate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">cdCanvasClear</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">drawTree</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #004600;">PI</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2.0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">80.0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">360</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">460</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">cdCanvasFlush</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_DEFAULT</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">map_cb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000000;">ih</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">cdcanvas</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">cdCreateCanvas</span><span style="color: #0000FF;">(</span><span style="color: #004600;">CD_IUP</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">ih</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">cddbuffer</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">cdCreateCanvas</span><span style="color: #0000FF;">(</span><span style="color: #004600;">CD_DBUFFER</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">cdcanvas</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">cdCanvasSetBackground</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #004600;">CD_PARCHMENT</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_DEFAULT</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">IupOpen</span><span style="color: #0000FF;">()</span>
<span style="color: #000000;">canvas</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupCanvas</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"RASTERSIZE=640x480"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupSetCallbacks</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"MAP_CB"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"map_cb"</span><span style="color: #0000FF;">),</span>
<span style="color: #008000;">"ACTION"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"redraw_cb"</span><span style="color: #0000FF;">)})</span>
<span style="color: #000000;">dlg</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupDialog</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"RESIZE=NO"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"TITLE"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Fractal Tree"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupShow</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()!=</span><span style="color: #004600;">JS</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">IupMainLoop</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</syntaxhighlight>-->
=={{header|PHP}}==
Image is created with GD module. Code adapted from the JavaScript version.
<
<?php
header("Content-type: image/png");
Line 1,653 ⟶ 2,584:
imagedestroy($img);
?>
</syntaxhighlight>
=={{header|PicoLisp}}==
This uses the 'brez' line drawing function from
[[Bitmap/Bresenham's line algorithm#PicoLisp]].
<
(de fractalTree (Img X Y A D)
Line 1,672 ⟶ 2,603:
(prinl "P1")
(prinl 400 " " 300)
(mapc prinl Img) ) )</
=={{header|Plain English}}==
<syntaxhighlight lang="plainenglish">To run:
Start up.
Clear the screen to the lightest blue color.
Pick a brownish color.
Put the screen's bottom minus 1/2 inch into the context's spot's y coord.
Draw a tree given 3 inches.
Refresh the screen.
Wait for the escape key.
Shut down.
To draw a tree given a size:
If the size is less than 1/32 inch, exit.
Put the size divided by 1/4 inch into the pen size.
If the size is less than 1/4 inch, pick a greenish color.
Remember where we are.
Stroke the size.
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.</syntaxhighlight>
{{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}}==
<
%%BoundingBox: 0 0 300 300
%%EndComments
Line 1,720 ⟶ 2,765:
%
showpage origstate restore
%%EOF</
Shorter version:<
%%BoundingBox: 0 0 300 300
/!0 { dup 1 sub dup 0 gt } def
Line 1,734 ⟶ 2,779:
/d 10 def 5 setlinewidth 1 setlinecap 170 20 translate d tree pop
%%EOF</
=={{header|POV-Ray}}==
<
#include "transforms.inc"
Line 1,779 ⟶ 2,824:
union {
FractalTree(<-2,0,0>, <1,0,0>, 1, Spread_Ang, Branches)
}</
=={{header|Prolog}}==
SWI-Prolog has a graphic interface : XPCE.
<
new(D, window('Fractal')),
send(D, size, size(800, 600)),
Line 1,802 ⟶ 2,848:
drawTree(D, X2, Y2, A2, De).
</syntaxhighlight>
=={{header|PureBasic}}==
<
#Scaling_Factor = 0.75
#Deg_to_Rad = #PI / 180
Line 1,834 ⟶ 2,881:
EndIf
Repeat: Until WaitWindowEvent(10) = #PB_Event_CloseWindow</
[[Image:PB_FractalTree.png]]
=={{header|Processing}}==
==== Using rotation ====
<syntaxhighlight lang="java">void setup() {
size(600, 600);
background(0);
stroke(255);
drawTree(300, 550, 9);
}
void drawTree(float x, float y, int depth) {
float forkAngle = radians(20);
float baseLen = 10.0;
if (depth > 0) {
pushMatrix();
translate(x, y - baseLen * depth);
line(0, baseLen * depth, 0, 0);
rotate(forkAngle);
drawTree(0, 0, depth - 1);
rotate(2 * -forkAngle);
drawTree(0, 0, depth - 1);
popMatrix();
}
}</syntaxhighlight>
==== Calculating coordinates ====
{{trans|Python}}
<syntaxhighlight lang="java">void setup() {
size(600, 600);
background(0);
stroke(255);
drawTree(300, 550, -90, 9);
}
void drawTree(float x1, float y1, float angle, int depth) {
float forkAngle = 20;
float baseLen = 10.0;
if (depth > 0) {
float x2 = x1 + cos(radians(angle)) * depth * baseLen;
float y2 = y1 + sin(radians(angle)) * depth * baseLen;
line(x1, y1, x2, y2);
drawTree(x2, y2, angle - forkAngle, depth - 1);
drawTree(x2, y2, angle + forkAngle, depth - 1);
}
}</syntaxhighlight>
==={{header|Processing Python mode}}===
==== Using rotation ====
{{trans|Processing}}
<syntaxhighlight lang="python">def setup():
size(600, 600)
background(0)
stroke(255)
drawTree(300, 550, 9)
def drawTree(x, y, depth):
fork_ang = radians(20)
base_len = 10
if depth > 0:
pushMatrix()
translate(x, y - baseLen * depth)
line(0, baseLen * depth, 0, 0)
rotate(fork_ang)
drawTree(0, 0, depth - 1)
rotate(2 * -fork_ang)
drawTree(0, 0, depth - 1)
popMatrix()</syntaxhighlight>
==== Calculating coordinates ====
{{trans|Python}}
<syntaxhighlight lang="python">def setup():
size(600, 600)
background(0)
stroke(255)
drawTree(300, 550, -90, 9)
def drawTree(x1, y1, angle, depth):
fork_angle = 20
base_len = 10.0
if depth > 0:
x2 = x1 + cos(radians(angle)) * depth * base_len
y2 = y1 + sin(radians(angle)) * depth * base_len
line(x1, y1, x2, y2)
drawTree(x2, y2, angle - fork_angle, depth - 1)
drawTree(x2, y2, angle + fork_angle, depth - 1)</syntaxhighlight>
=={{header|Python}}==
[[File:fractal-tree-python.png|right|thumb]]
{{libheader|pygame}}
<
pygame.init()
Line 1,848 ⟶ 2,988:
def drawTree(x1, y1, angle, depth):
base_len = 10.0
if depth > 0:
x2 = x1 + int(math.cos(math.radians(angle)) * depth * base_len)
y2 = y1 + int(math.sin(math.radians(angle)) * depth * base_len)
pygame.draw.line(screen, (255,255,255), (x1, y1), (x2, y2), 2)
drawTree(x2, y2, angle -
drawTree(x2, y2, angle +
def input(event):
Line 1,862 ⟶ 3,004:
pygame.display.flip()
while True:
input(pygame.event.wait())</
=={{header|QB64}}==
<syntaxhighlight lang="qb64">_Title "Fractal Tree"
Const sw% = 640
Const sh% = 480
Screen _NewImage(sw, sh, 8)
Cls , 15: Color 2
Call tree(sw \ 2, sh - 10, _Pi * 1.5, _Pi / 180 * 29, 112, 15)
Sleep
System
Sub tree (x As Integer, y As Integer, initAngle As Double, theta As Double, length As Double, depth As Integer)
Dim As Integer iL, newX, newY, iX, iY, iD
iL = length: iX = x: iY = y: iD = depth
newX = Cos(initAngle) * length + iX
newY = Sin(initAngle) * length + iY
Line (iX, iY)-(newX, newY)
iL = length * .78
iD = iD - 1
If iD > 0 Then
Call tree(newX, newY, initAngle - theta, theta, iL, iD)
Call tree(newX, newY, initAngle + theta, theta, iL, iD)
End If
End Sub</syntaxhighlight>
=={{header|Quackery}}==
<syntaxhighlight lang="quackery">[ $ "turtleduck.qky" loadfile ] now!
[ [ 1 1
30 times
[ tuck + ]
swap join ] constant
do ] is phi ( --> n/d )
[ 2dup 5 1 v< iff
2drop done
2dup 5 1 v/
proper 2drop wide
2dup walk
1 5 turn
2dup phi v/
2dup recurse
-2 5 turn
recurse
1 5 turn
-v fly ] is tree ( n/d --> )
turtle
20 frames
-1 4 turn
-450 1 fly
500 1 tree
1 frames</syntaxhighlight>
{{output}}
[[File:Quackery fractal tree.png|thumb|center]]
=={{header|R}}==
Line 1,870 ⟶ 3,071:
[[File:FRTR12.png|200px|right|thumb|Output FRTR12.png]]
[[File:FRTR15.png|200px|right|thumb|Output FRTR15.png]]
<syntaxhighlight lang="r">
## Recursive FT plotting
plotftree <- function(x, y, a, d, c) {
Line 1,905 ⟶ 3,106:
pFractalTree(12,0.6,210);
pFractalTree(15,0.35,600);
</syntaxhighlight>
{{Output}}
<pre>
Line 1,924 ⟶ 3,125:
=={{header|Racket}}==
[[File:tree-racket.png|right|thumb]]
<
#lang racket
(require graphics/turtles)
Line 1,940 ⟶ 3,141:
(tree 35)
(save-turtle-bitmap "tree.png" 'png)
</syntaxhighlight>
=={{header|Raku}}==
(formerly Perl 6)
Image is created in [[wp:SVG|SVG]] format.
<syntaxhighlight lang="raku" line>my ($width, $height) = (1000,1000); # image dimension
my $scale = 6/10; # branch scale relative to trunk
my $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);
}</syntaxhighlight>
=={{header|Red}}==
<syntaxhighlight lang="red">Red [Needs: 'View]
color: brown
width: 9
view/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 nodes
da: pi * 30 / 180 ; angle of branches in radians
ea: pi * 5 / 180 ; offset added to angle to break symmetry
l: 200 ; branches initial lenght
scale: 0.7 ; branches scale factor
grow: 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
]</syntaxhighlight>
{{out}}
[https://raw.githubusercontent.com/Palaing/redlib/master/games/images/fractaltree.png fractal tree image]
=={{header|Ring}}==
<
load "guilib.ring"
Line 1,994 ⟶ 3,256:
tree(self, x2, y2, size * scale, angle - spread, depth - 1)
tree(self, x2, y2, size * scale, angle + spread, depth - 1) ok}
</syntaxhighlight>
Output:
Line 2,001 ⟶ 3,263:
=={{header|Ruby}}==
{{libheader|Shoes}}
<
background "#fff"
stroke "#000"
Line 2,019 ⟶ 3,281:
drawTree(300,550,-90,9)
end</
=={{header|Rust}}==
{{libheader|Piston}}
<syntaxhighlight lang="rust">//Cargo deps :
// piston = "0.35.0"
// piston2d-graphics = "0.23.0"
Line 2,081 ⟶ 3,342:
}
}
</syntaxhighlight>
=={{header|Scala}}==
Adapted from the Java version. Screenshot below.
<
import java.awt.{RenderingHints, BasicStroke, Color}
Line 2,115 ⟶ 3,376:
}
}
}</
[[File:scalaTree.png]]
Line 2,122 ⟶ 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.
<
(import (scheme base)
(scheme file)
Line 2,174 ⟶ 3,435:
(output-tree-as-eps "fractal.eps" (create-tree 400 200 90 9))
</syntaxhighlight>
=={{header|Scilab}}==
Line 2,181 ⟶ 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 2,260 ⟶ 3,521:
plot2d(real(tree),imag(tree),14);
set(gca(),'isoview','on');
set(gca(),'axes_visible',['off','off','off']);</
===Recursive approach===
{{trans|PHP}}
<syntaxhighlight lang="text">width = 512;
height = 512;
img=scf();
Line 2,280 ⟶ 3,541:
drawTree(width/2,height,90,10);
set(gca(),'isoview','on');</
=={{header|Seed7}}==
<
include "float.s7i";
include "math.s7i";
Line 2,312 ⟶ 3,573:
drawTree(300, 470, -90.0, 9);
ignore(getc(KEYBOARD));
end func;</
Original source: [http://seed7.sourceforge.net/algorith/graphic.htm#fractree]
Line 2,318 ⟶ 3,579:
=={{header|Sidef}}==
{{trans|Perl}}
<
len < 1 && return()
Line 2,340 ⟶ 3,601:
tree(img, width/2, height)
File('tree.png').write(img.png, :raw)</
=={{header|Smalltalk}}==
Line 2,346 ⟶ 3,607:
This example is coded for Squeak Smalltalk.
<
Object subclass: #FractalTree
instanceVariableNames: ''
Line 2,352 ⟶ 3,613:
poolDictionaries: ''
category: 'RosettaCode'
</syntaxhighlight>
Methods for FractalTree class:
<
tree: aPoint length: aLength angle: anAngle
| p a |
Line 2,382 ⟶ 3,643:
self tree: 700@700 length: 200 angle: 0.
]
</syntaxhighlight>
Now open a new Workspace and enter:
<
FractalTree new draw.
</syntaxhighlight>
=={{header|SVG}}==
Line 2,397 ⟶ 3,657:
<div style="clear:both;"></div>
<
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN"
"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
Line 2,446 ⟶ 3,706:
</g>
</svg></
=={{header|Swift}}==
[http://i.imgur.com/F8Fyn1i.png Image] - Link, since uploads seem to be disabled currently.
In a playground:
<syntaxhighlight lang="swift">extension CGFloat {
func degrees_to_radians() -> CGFloat {
return CGFloat(M_PI) * self / 180.0
Line 2,463 ⟶ 3,721:
return Double(M_PI) * self / 180.0
}
}
Line 2,501 ⟶ 3,758:
drawTree(self.frame.width / 2 , y1: self.frame.height * 0.8, angle: -90 , depth: 9 )
}
}
Line 2,508 ⟶ 3,763:
let tree = Tree(frame: CGRectMake(0, 0, 300, 300))
tree
</syntaxhighlight>
=={{header|Standard ML}}==
Works with PolyML
<syntaxhighlight lang="standard ml">open XWindows;
open Motif;
fun toI {x=x,y=y} = {x=Real.toInt IEEEReal.TO_NEAREST x,y=Real.toInt IEEEReal.TO_NEAREST y} ;
fun drawOnTop win usegc ht hs {x=l1,y=l2} {x=r1,y=r2} =
let
val xy = {x=l1 - ht * (l2-r2) , y = l2 - ht * (r1-l1) }
val zt = {x=r1 - ht * (l2-r2) , y= r2 - ht * (r1-l1) }
val ab = {x= ( (#x xy + #x zt) + hs * (#y zt - #y xy ) )/2.0 , y = ( (#y zt + #y xy) - hs * (#x zt - #x xy )) /2.0 }
in
if abs (l1 - #x xy ) < 0.9 andalso abs (l2 - #y xy ) < 0.9
then XFlush (XtDisplay win)
else
(XFillPolygon (XtWindow win) usegc [ (XPoint o toI) {x=l1,y=l2},
(XPoint o toI ) xy ,
(XPoint o toI ) ab ,
(XPoint o toI ) zt ,
(XPoint o toI ) {x=r1,y=r2} ] Convex CoordModeOrigin ;
drawOnTop win usegc (0.87*ht) hs xy ab ;
drawOnTop win usegc (0.93*ht) hs ab zt )
end ;
val demoWindow = fn () =>
let
val shell = XtAppInitialise "" "tree" "top" [] [ XmNwidth 800, XmNheight 650] ;
val main = XmCreateMainWindow shell "main" [ XmNmappedWhenManaged true ] ;
val canvas = XmCreateDrawingArea main "drawarea" [ XmNwidth 800, XmNheight 650] ;
val usegc = DefaultGC (XtDisplay canvas) ;
in
XtSetCallbacks canvas [ (XmNexposeCallback ,
(fn (w,c,t) => ( drawOnTop canvas usegc 8.0 0.85 {x=385.0,y=645.0} {x=415.0,y=645.0} ; t) ) )
] XmNarmCallback ;
XtManageChild canvas ;
XtManageChild main ;
XtRealizeWidget shell
end ;
demoWindow ();</syntaxhighlight>
=={{header|Tcl}}==
{{libheader|Tk}}
<
set SIZE 800
Line 2,543 ⟶ 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</
=={{header|TUSCRIPT}}==
Image is created in SVG-format
<
$$ MODE TUSCRIPT
dest="fracaltree.svg"
Line 2,584 ⟶ 3,888:
WRITE/NEXT d "</svg>"
ENDACCESS d
</syntaxhighlight>
=={{header|TypeScript}}==
{{trans|JavaScript}}
<
var canvas: HTMLCanvasElement = document.createElement('canvas')
canvas.width = 600
Line 2,624 ⟶ 3,928:
ctx.stroke()
</syntaxhighlight>
=={{header|Wren}}==
{{trans|Kotlin}}
{{libheader|DOME}}
<syntaxhighlight lang="wren">import "graphics" for Canvas, Color
import "dome" for Window
import "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)</syntaxhighlight>
=={{header|XPL0}}==
[[File:FtreeXPL0.png|200px|thumb|right|Output]]
<
proc DrawBranch(Lev, Dir, Len, X, Y);
Line 2,648 ⟶ 3,990:
if ChIn(1) then []; \wait for keystroke
SetVid(3); \restore normal text mode
]</
=={{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 2,654 ⟶ 4,019:
{{trans|BBC BASIC}}{{trans|XPL0}}
[[File:FractalTree.zkl.jpg|250px|thumb|right]]
<
scale:=0.76;
sizeX:=400; sizeY:=300;
Line 2,671 ⟶ 4,036:
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.
=={{header|ZX Spectrum Basic}}==
{{trans|BASIC256}}
<
20 LET x=127: LET y=0
30 LET rotation=PI/2
Line 2,706 ⟶ 4,071:
1030 PLOT x,y: DRAW xn-x,y-yn
1040 LET x=xn: LET y=yn
1050 RETURN </
[[Category:Geometry]]
|