From 3084112265f77c539a75f6e1c56cac7ac2703c81 Mon Sep 17 00:00:00 2001 From: Fr3deric Date: Sat, 9 Mar 2019 22:19:42 +0100 Subject: [PATCH] randomize positioning, draw outline --- baumgen.js | 69 ++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 59 insertions(+), 10 deletions(-) diff --git a/baumgen.js b/baumgen.js index c9a16cf..0475e78 100644 --- a/baumgen.js +++ b/baumgen.js @@ -2,26 +2,26 @@ paper.install(window); -function gen_tree(pos) { +function gen_tree(pos, maxheight) { var baum = new Branch(); - baum.length = (20 + Math.random() * 20); + baum.length = 0.15 * maxheight + 0.15 * maxheight * Math.random(); var shapernd = Math.random() * 3; if(shapernd < 1) { baum.crown = new KiteCrown( - 70 + Math.random() * 30, + 0.49 * maxheight + 0.21 * maxheight * Math.random(), 0.4 + Math.random() * 0.2, {bisect: 0.2 + Math.random() * 0.2}, ); } else if(shapernd < 2) { baum.crown = new TriangleCrown( - 70 + Math.random() * 30, + 0.49 * maxheight + 0.21 * maxheight * Math.random(), 0.4 + Math.random() * 0.2, ); } else { baum.crown = new EllipseCrown( - 70 + Math.random() * 30, + 0.49 * maxheight + 0.21 * maxheight * Math.random(), 0.4 + Math.random() * 0.2, {bisect: 0.1 + Math.random() * 0.4}, ); @@ -80,6 +80,10 @@ class Crown { collides(other) { return this._path.intersects(other._path); } + + get bounds() { + return this._path.bounds; + } } @@ -220,6 +224,24 @@ class Branch { collides_crown(other) { return this.crown.collides(other.crown); } + + get bounds() { + var b = this._branch.bounds; + b = b.unite(this.crown.bounds); + if(typeof this.forks !== "undefined") { + for (let fork of this.forks) { + b = b.unite(fork.branch.bounds); + /* + var r = new Path.Rectangle({ + point: fork.branch.bounds.point, + size: fork.branch.bounds.size, + strokeWidth: 1, + strokeColor: 'green'}); + */ + } + } + return b; + } } @@ -227,11 +249,38 @@ function draw() { var canvas = document.getElementById('canvas'); paper.setup(canvas); - //var br = new Branch(baum); - - for(var x=0; x<10; x++) { - for(var y=0; y<5; y++) { - var br = new Branch(gen_tree(new Point(50 + x*80, 150 + y*160))); + var width = 148 * 5; + var height = 105 * 5; + var rows = 3; + var rowspace = 20; + var inspace = 30; + var outspace = 10; + + var maxh = (height - (rows-1)*rowspace - 2*outspace - 2*inspace) / rows; + + var outline = new Path.Rectangle({ + point: new Point(outspace, outspace), + size: new Size(width - 2*outspace, height - 2*outspace), + strokeWidth: 2, + strokeColor: 'black' + }); + + for(var row=0; row