From bc71bf0b5501fbd6bbdc3c1f4d84b48c4fe2bf83 Mon Sep 17 00:00:00 2001 From: Fr3deric Date: Sun, 3 Mar 2019 22:54:12 +0100 Subject: [PATCH] determine branch angle range before randomizing --- baumgen.js | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/baumgen.js b/baumgen.js index b81fee7..438fc50 100644 --- a/baumgen.js +++ b/baumgen.js @@ -30,37 +30,32 @@ function gen_tree(pos) { baum.angle = Math.PI / 2; // + Math.random() * 0.2 - 0.1; if((baum.crown.shape != "triangle") && (Math.random() < 0.5)) { - var crown = baum.crown.clone(); - crown.length *= 0.4; var b = { length: baum.length * 0.4, - angle: Math.PI / 5 + Math.random() * 0.45, - crown: crown + angle: Math.PI / 2, + crown: baum.crown.clone() }; + b.crown.length *= 0.4; baum.forks = [{ pos: 0.2 + Math.random() * 0.4, branch: new Branch(b), }]; baum.draw(pos); - var rectified = false; - var i=0; + var step = (Math.random() < 0.5) ? -0.1 : 0.1; while(baum.collides_crown(baum.forks[0].branch)) { - baum.forks[0].branch.angle -= 0.01; + baum.forks[0].branch.angle += step; baum.draw(pos); - rectified = true; - i += 1; - if(i>50) { - var c = new Path.Circle({center:pos, radius: 2, strokeWidt: 1, strokeColor: '#ff0000'}); - //break; - } } - if(rectified) { - baum.forks[0].branch.angle -= Math.random() * 0.1; - var c = new Path.Circle({center:pos, radius: 2, strokeWidt: 1, strokeColor: '#ff0000'}); - baum.draw(pos); + var range = 4*Math.PI/9 - Math.abs(Math.PI/2 - baum.forks[0].branch.angle); + if(step > 0) { + baum.forks[0].branch.angle += range * (0.1 + 0.9 * Math.random()); + } else { + baum.forks[0].branch.angle -= range * (0.1 + 0.9 * Math.random()); } + baum.draw(pos); + } baum.draw(pos);