From 7fa736fbeb6ad48945ee54f3aee878c2dd4ab836 Mon Sep 17 00:00:00 2001 From: Frederic Date: Sun, 14 Jul 2019 15:05:55 +0200 Subject: [PATCH] indicate stack size with color, some refactoring --- lindenmayer-system/lindenmeyer-system.html | 1 - lindenmayer-system/lindenmeyer-system.js | 91 +++++++++++++++------- 2 files changed, 62 insertions(+), 30 deletions(-) diff --git a/lindenmayer-system/lindenmeyer-system.html b/lindenmayer-system/lindenmeyer-system.html index 8ab6f9b..c24bf8c 100644 --- a/lindenmayer-system/lindenmeyer-system.html +++ b/lindenmayer-system/lindenmeyer-system.html @@ -272,7 +272,6 @@ function sierpinski(noise) { p.scale(80); p.translate(new Point(300, 300)); p.strokeWidth = 1; - p.strokeColor = '#ff00ff'; } diff --git a/lindenmayer-system/lindenmeyer-system.js b/lindenmayer-system/lindenmeyer-system.js index 5c3d708..0391bd4 100644 --- a/lindenmayer-system/lindenmeyer-system.js +++ b/lindenmayer-system/lindenmeyer-system.js @@ -27,7 +27,6 @@ function generate(start, rules, iterations) { ruleres = rule[1](); } else if(typeof rule[1] == 'object') { var r = Math.floor(Math.random() * rule[1].length); - console.log(r); ruleres = rule[1][r]; }else { ruleres = rule[1]; @@ -46,24 +45,42 @@ function generate(start, rules, iterations) { word = newword; consumed = newconsumed; } - + return generate(newword, rules, iterations - 1); } -function draw_initstate(p, state) { +function draw_initstate(grp, state) { if(!('dir' in state)) { state.dir = 0; state.turnnoise = 0; state.lennoise = 0; - } - if(!('stepsize' in state)) { + state.pos = new paper.Point(0, 0); + state.pathstack = []; state.stepsize = 1; + state.stack = []; + state.curpath = null; } - if(!('curpath' in state)) { - p.addChild(new paper.Path()); - state.curpath = p.lastChild; - } + + /* + for(let i=0; i<10; i++) { + var color = new paper.Color(255,255,255); + color.saturation = 1; + color.brightness = 1; + color.hue = 36 * (10-i); // TODO remove constant + state.palette.push(color); + } + */ + state.palette = [ + new paper.Color('#ff0000'), + new paper.Color('#ff0000'), + new paper.Color('#ff00ff'), + new paper.Color('#ff00ff'), + new paper.Color('#0000ff'), + new paper.Color('#0000ff'), + new paper.Color('#00ff00'), + new paper.Color('#00ff00'), + ]; } @@ -89,18 +106,30 @@ function draw_set_length_noise(level) { function draw_forward(factor=1) { - return function(p, state) { - draw_initstate(p, state); - if(state.curpath.segments.length == 0) { - state.curpath.add(new paper.Point(0, 0)); - console.log('---------------------------'); + return function(grp, state) { + if(state.curpath == null) { + var p = new paper.Path(); + p.add(state.pos); + state.curpath = p; + // index 0 corresponds to empty state.stack + if(state.pathstack[state.stack.length] == null) { + var cp = new paper.CompoundPath(); + cp.addChild(p); + cp.strokeColor = state.palette[state.stack.length]; + state.pathstack[state.stack.length] = cp; + grp.addChild(cp); + } + state.pathstack[state.stack.length].addChild(p); } + var lastp = state.curpath.segments[state.curpath.segments.length - 1].point; var noise = 1 + (Math.random() - 0.5) * state.lennoise; state.curpath.add(lastp.add(new paper.Point( Math.sin(state.dir) * state.stepsize * factor * noise, Math.cos(state.dir) * state.stepsize * factor * noise ))); + var lastseg = state.curpath.segments[state.curpath.segments.length - 1]; + state.pos = lastseg.point; }; } @@ -112,7 +141,6 @@ function draw_stepsize_mul(factor) { } else { var f = factor; } - draw_initstate(p, state); state.stepsize *= f; }; } @@ -120,7 +148,6 @@ function draw_stepsize_mul(factor) { function draw_turn(angle) { return function(p, state) { - draw_initstate(p, state); state.dir += angle; }; } @@ -128,7 +155,6 @@ function draw_turn(angle) { function draw_angle_turn(fac) { return function(p, state) { - draw_initstate(p, state); var a = state.angle * (1 + (Math.random() - 0.5) * state.turnnoise); state.dir += a * fac; }; @@ -150,38 +176,45 @@ function draw_angle_add(delta) { function draw_state_push() { - return function(p, state) { - if(typeof state.stack == 'undefined') { - state.stack = new Array(); - } + return function(grp, state) { state.stack.push({ - pos: state.curpath.segments[state.curpath.segments.length - 1].point, + pos: state.pos, dir: state.dir, stepsize: state.stepsize, }); + state.curpath = null; + /* + if(state.palette[state.stack.length] != + state.palette[state.stack.length - 1]) { + p.addChild(new paper.Path()); + state.curpath = p.lastChild; + state.curpath.add(pos); + state.curpath.strokeColor = state.palette[state.stack.length]; + } + */ }; } function draw_state_pop() { - return function(p, state) { + return function(grp, state) { var s = state.stack.pop(); state.dir = s.dir; state.stepsize = s.stepsize; - p.addChild(new paper.Path()); - state.curpath = p.lastChild; - state.curpath.add(s.pos); + state.pos = s.pos; + state.curpath = null; }; } function draw(word, actions) { - var p = new paper.CompoundPath(); + var grp = new paper.Group(); var state = {}; + draw_initstate(grp, state); for(let w of word) { if(w in actions) { - actions[w](p, state); + actions[w](grp, state); } } - return p; + return grp; }