JavaScript
W=800
H=600
canvas.width = W;
canvas.height = H;
var ctx = canvas.getContext("2d");
R=function(m) { return m * Math.random()};
RAD=function(deg) { return deg / 180 * Math.PI};
LIMIT=function(x, min, max) {return x < min ? min : x > max ? max : x};
var SPEED = 0.01, HEIGHT = 11.0;
// Ground
var grd = ctx.createLinearGradient(0,0,0,H);
grd.addColorStop(0,"#88ccff");
grd.addColorStop(0.45,"#ffffee");
grd.addColorStop(0.5,"#80cc80");
grd.addColorStop(1,"#001100");
ctx.fillStyle = grd;
ctx.fillRect(0,0, W,H);
Plot = function(dist, side, h, s, alpha, diam)
{
var x, y, a1,a2,scale = H/4 * 3.5 / dist,
x0 = side * scale + s * scale + W/2,
y0 = H/2 + 2.5*scale - h*scale;
k = dist
if (diam > 0.05) {
red = k*3|0;
green = k|0;
a1=alpha+1
a2=alpha-1
}
else
{
green= 80+(1-diam)*k*2|0;
red = k|0;
a1=0;
a2=2*Math.PI;
}
diam *= scale;
h *= scale;
s *= scale;
ctx.beginPath();
ctx.arc(x0,y0,diam/2, a1,a2);//lpha-1, alpha+1);//0,2*Math.PI);
ctx.fillStyle = 'rgb('+red+','+green+',0)';
ctx.fill();
}
Grow = function(dist, side, h, s, alpha, grown)
{
var diam, branchLength = 0.0;
diam = (1.0 - grown / HEIGHT) * 0.5;
do
{
alpha += R(RAD(3)) - RAD(1.5);
h += Math.sin(alpha) * SPEED;
s += Math.cos(alpha) * SPEED;
branchLength += SPEED;
grown += SPEED;
diam = (1.0 - grown / HEIGHT) * 0.5;
Plot(dist, side, h, s, alpha, diam);
} while(branchLength < 5 * diam + R(6 * diam) && diam > 0.02);
if (diam > 0.02)
{
var br = 0;
if(R(10) > 2) br++,Grow(dist, side, h, s, alpha + RAD(15) + R(RAD(20)), grown);
if(R(10) > 2) br++,Grow(dist, side, h, s, alpha - RAD(15) - R(RAD(20)), grown);
if(R(10) < 2 || br == 0) Grow(dist, side, h, s, alpha - RAD(2.5) + R(RAD(5)), grown);
}
}
trees=[]
for(i = 0; i < 300; ++i) trees.push({ z: 1+R(70),...