From 24f263a2ca56336b7c18d0a5d9842b8afdef087b Mon Sep 17 00:00:00 2001 From: Frederic Date: Sat, 1 Jun 2019 17:26:52 +0200 Subject: [PATCH] add barnsley fern --- lindenmayer-system/lindenmeyer-system.html | 40 ++++++++++++++++++ lindenmayer-system/lindenmeyer-system.js | 49 ++++++++++++++++------ 2 files changed, 77 insertions(+), 12 deletions(-) diff --git a/lindenmayer-system/lindenmeyer-system.html b/lindenmayer-system/lindenmeyer-system.html index a7b6031..2ac8774 100644 --- a/lindenmayer-system/lindenmeyer-system.html +++ b/lindenmayer-system/lindenmeyer-system.html @@ -129,6 +129,40 @@ function sierpinski() { } + /************************************************ + * Barnsley Fern + ************************************************/ + + function barnsley_fern() { + project.clear(); + var r = generate( + 'IXF'.split(''), + [ + ['X'.split(''), 'F+[[X]-X]-F[-FX]+X'.split('')], + ['F'.split(''), 'FF'.split('')] + ], + 6 + ); + + console.log(r); + var p = draw( + r, + { + 'F': draw_forward(), + 'I': draw_angle_init(25/180 * Math.PI), + '-': draw_angle_turn(-1), + '+': draw_angle_turn(1), + '[': draw_state_push(), + ']': draw_state_pop() + } + ); + p.scale(2); + p.translate(new Point(200, 200)); + p.strokeWidth = 1; + p.strokeColor = '#ff00ff'; + } + + /************************************************ * ************************************************/ @@ -136,6 +170,7 @@ function sierpinski() { globals.dragon_curve = dragon_curve; globals.sierpinski2 = sierpinski2; globals.sierpinski = sierpinski; +globals.barnsley_fern = barnsley_fern; globals.get_svg = function() { return project.exportSVG({asString: true}); @@ -185,6 +220,10 @@ window.onload = function() { window.globals.sierpinski(); }); + document.getElementById('barnsley_fern').addEventListener('click', function() { + window.globals.barnsley_fern(); + }); + } @@ -196,6 +235,7 @@ window.onload = function() { +
diff --git a/lindenmayer-system/lindenmeyer-system.js b/lindenmayer-system/lindenmeyer-system.js index d7e5961..aaee5f3 100644 --- a/lindenmayer-system/lindenmeyer-system.js +++ b/lindenmayer-system/lindenmeyer-system.js @@ -43,25 +43,26 @@ function generate(start, rules, iterations) { } -function draw_initstate(state) { +function draw_initstate(p, state) { if(!('dir' in state)) { state.dir = 0;//Math.PI/2; } + if(!('curpath' in state)) { + p.addChild(new paper.Path()); + state.curpath = p.lastChild; + } } function draw_forward() { return function(p, state) { - draw_initstate(state); - if(p.children.length == 0) { - p.addChild(new paper.Path()); - } - var pp = p.lastChild; - if(pp.length == 0) { - pp.add(new paper.Point(0, 0)); + draw_initstate(p, state); + if(state.curpath.segments.length == 0) { + state.curpath.add(new paper.Point(0, 0)); + console.log('---------------------------'); } - var lastp = pp.segments[pp.segments.length - 1].point; - pp.add(lastp.add( + var lastp = state.curpath.segments[state.curpath.segments.length - 1].point; + state.curpath.add(lastp.add( new paper.Point(Math.sin(state.dir), Math.cos(state.dir)) )); } @@ -70,7 +71,7 @@ function draw_forward() { function draw_turn(angle) { return function(p, state) { - draw_initstate(state); + draw_initstate(p, state); state.dir += angle; } } @@ -78,7 +79,7 @@ function draw_turn(angle) { function draw_angle_turn(fac) { return function(p, state) { - draw_initstate(state); + draw_initstate(p, state); state.dir += state.angle * fac; } } @@ -98,6 +99,30 @@ function draw_angle_add(delta) { } +function draw_state_push() { + return function(p, state) { + if(typeof state.stack == 'undefined') { + state.stack = new Array(); + } + state.stack.push({ + pos: state.curpath.segments[state.curpath.segments.length - 1].point, + dir: state.dir, + }); + } +} + + +function draw_state_pop() { + return function(p, state) { + var s = state.stack.pop(); + state.dir = s.dir; + p.addChild(new paper.Path()); + state.curpath = p.lastChild; + state.curpath.add(s.pos); + } +} + + function draw(word, actions) { var p = new paper.CompoundPath(); var state = {};