Fractal tree: Difference between revisions

4,564 bytes added ,  24 days ago
m
No edit summary
 
(10 intermediate revisions by 8 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
#
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
call tree x y deg - 20 n - 1
call 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>
 
Line 1,096 ⟶ 1,184:
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
[[File:DelphiFractalTree.png|frame|none]]
 
 
<syntaxhighlight lang="Delphi">
Line 1,131 ⟶ 1,219:
 
</pre>
 
 
=={{header|F_Sharp|F#}}==
Line 1,355 ⟶ 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,073 ⟶ 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 3,775 ⟶ 3,933:
{{trans|Kotlin}}
{{libheader|DOME}}
<syntaxhighlight lang="ecmascriptwren">import "graphics" for Canvas, Color
import "dome" for Window
import "math" for Math
1,480

edits