Fractal Tree 分形树
A recursive function that draws a branch, then calls itself twice at 70% length — 6 levels deep. 一个递归函数:画一根枝,然后以 70% 的长度调用自己两次,递归六层。
Runs live on this page. Move the pointer, click, or drag — whichever the effect listens for. 就在本页实时运行。移动鼠标、点击或拖拽——看它响应哪一种。
How it works原理拆解
br() terminates when len < 4, which is the base case that stops the recursion. Each call draws from the current point to a computed endpoint, then spawns two children at ±0.4 radians. The a variable sweeps slowly and is mixed into the child angles, which is what makes the whole tree sway instead of standing rigid. br() 在 len < 4 时返回,这就是止住递归的基线条件。每次调用从当前点画到算出的端点,再在 ±0.4 弧度处分出两个子枝。变量 a 缓慢推进并被混进子枝角度,整棵树才会摇,而不是僵着。
What to change可以改的地方
- 1Change 0.7 to 0.75 and the tree gets noticeably denser and taller.
- 2Set the two child angles asymmetrically (±0.6 and ±0.2) for a wind-bent tree.
- 10.7 改成 0.75,树会明显更密更高。
- 2两个子枝角度改成不对称(±0.6 和 ±0.2),就是被风吹歪的树。
Source code源码
Self-contained — paste it into any HTML file and it runs. No libraries. 完全独立——粘进任意 HTML 文件就能跑,不需要任何库。