From 8a574d746268fe049f0378b127feac85f2021de9 Mon Sep 17 00:00:00 2001 From: Frederic Date: Wed, 10 Jul 2019 23:16:07 +0200 Subject: [PATCH] add length noise --- lindenmayer-system/lindenmeyer-system.html | 3 ++- lindenmayer-system/lindenmeyer-system.js | 17 +++++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/lindenmayer-system/lindenmeyer-system.html b/lindenmayer-system/lindenmeyer-system.html index 8e62c9b..63d09ed 100644 --- a/lindenmayer-system/lindenmeyer-system.html +++ b/lindenmayer-system/lindenmeyer-system.html @@ -190,7 +190,7 @@ function sierpinski() { project.clear(); if(typeof num_defects == 'undefined') { num_defects = 0 }; var r = generate( - 'iIjX'.split(''), + 'iIjJX'.split(''), [ ['X'.split(''), 'F[-lfX][cfX][+rfX]'.split('')], //['F'.split(''), 'FF'.split('')], @@ -231,6 +231,7 @@ function sierpinski() { 'i': draw_set_dir(Math.PI), 'I': draw_angle_init(25/180 * Math.PI), 'j': draw_set_turn_noise(1), + 'J': draw_set_length_noise(1), '*': draw_angle_add((Math.random() - 0.5) * 0.01), '-': draw_angle_turn(-1), diff --git a/lindenmayer-system/lindenmeyer-system.js b/lindenmayer-system/lindenmeyer-system.js index 947eb25..501bf5a 100644 --- a/lindenmayer-system/lindenmeyer-system.js +++ b/lindenmayer-system/lindenmeyer-system.js @@ -51,6 +51,7 @@ function draw_initstate(p, state) { if(!('dir' in state)) { state.dir = 0; state.turnnoise = 0; + state.lennoise = 0; } if(!('stepsize' in state)) { state.stepsize = 1; @@ -76,6 +77,13 @@ function draw_set_turn_noise(level) { } +function draw_set_length_noise(level) { + return function(p, state) { + state.lennoise = level; + } +} + + function draw_forward(factor=1) { return function(p, state) { draw_initstate(p, state); @@ -84,10 +92,11 @@ function draw_forward(factor=1) { console.log('---------------------------'); } var lastp = state.curpath.segments[state.curpath.segments.length - 1].point; - state.curpath.add(lastp.add( - new paper.Point(Math.sin(state.dir) * state.stepsize * factor, - Math.cos(state.dir) * state.stepsize * factor) - )); + 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 + ))); } }