Fractal tree: Difference between revisions

9,098 bytes added ,  13 days ago
m
m (→‎{{header|Quackery}}: fast graphics)
 
(19 intermediate revisions by 10 users not shown)
Line 22:
DeltaAngle = 0.2 * math:pi
 
F drawTree(outfile, Float x, Float y; len, theta) -> NVoid
I len >= 1
V x2 = x + len * cos(theta)
Line 30:
drawTree(outfile, x2, y2, len * ScaleFactor, theta - DeltaAngle)
 
V outsvg = File(‘tree.svg’, ‘w’WRITE)
outsvg.write(|‘<?xml version='1.0' encoding='utf-8' standalone='no'?>
<!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.1//EN' 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'>
Line 551:
 
{{libheader|SGE}} or {{libheader|cairo}}
<syntaxhighlight lang="c" line="1">#include <SDL/SDL.h>
#ifdef WITH_CAIRO
#include <cairo.h>
Line 562:
#include <math.h>
 
#ifndef M_PI
#ifdef WITH_CAIRO
#define PIM_PI 3.141592653514159265358979323846
#endif
 
#define SIZE 800 // determines size of window
#define SCALE 5 // determines how quickly branches shrink (higher value means faster shrinking)
Line 627:
0.0, -1.0,
INITIAL_LENGTH,
PIM_PI / 8,
BRANCHES);
SDL_UpdateRect(surface, 0, 0, 0, 0);
Line 664:
 
//--------------------------------------------------------------------------------------------------
#ifndef M_PI
const float PI = 3.1415926536f;
#define M_PI 3.14159265358979323846
#endif
 
//--------------------------------------------------------------------------------------------------
Line 786 ⟶ 788:
public:
fractalTree() { _ang = DegToRadian( 24.0f ); }
float DegToRadian( float degree ) { return degree * ( PIM_PI / 180.0f ); }
 
void create( myBitmap* bmp )
Line 1,071 ⟶ 1,073:
=={{header|EasyLang}}==
 
[https://easylang.dev/show/#cod=jY/BDsIgEETvfMUkXrRNCa1y8KBH/4NQqiQUDCXa/r2w6cEmHuSwGWbnLcsOt6h0Ug4pGsN2YDq4ECGlZM8YNNmYsaA3d3hwcAbADllfIYrOx1lv3rZPj+xWEPy0+mN4EbxeZ9QX6DDRrIqyLT/muo/K92EcUGdYHtb4UuKT9X/GyxJfj20Wb9CJPKBB+6tbb7qccZbsaGL+XvAgWXztjCJBrBRoBc6lkkdAl9EP Run it]
[https://easylang.online/apps/_fractal-tree.html Run it]
 
<syntaxhighlight lang="text">
# Fractal tree
func tree x y deg n . .
#
if n > 0
color 555
linewidth n * 0.4
proc tree x y movedeg xn y. .
if n > 0
x += cos deg * n * 1.3 * (randomf + 0.5)
y += sin deg *linewidth n * 1.3 * (randomf + 0.5)4
line move x y
call tree x y+= cos deg - 20* n -* 1.3 * (randomf + 0.5)
call tree x y deg += 20sin 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
call tree 50 9010 -90 10
timer 12
.
</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#}}==
Line 1,315 ⟶ 1,442:
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}}==
Line 2,033 ⟶ 2,208:
</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 2,429 ⟶ 2,627:
{{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}}==
Line 3,644 ⟶ 3,933:
{{trans|Kotlin}}
{{libheader|DOME}}
<syntaxhighlight lang="ecmascriptwren">import "graphics" for Canvas, Color
import "dome" for Window
import "math" for Math
Line 3,702 ⟶ 3,991:
SetVid(3); \restore normal text mode
]</syntaxhighlight>
 
=={{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}}==
1,480

edits