Browse Source

indicate stack size with color, some refactoring

master
Fr3deric 5 years ago
parent
commit
7fa736fbeb
  1. 1
      lindenmayer-system/lindenmeyer-system.html
  2. 91
      lindenmayer-system/lindenmeyer-system.js

1
lindenmayer-system/lindenmeyer-system.html

@ -272,7 +272,6 @@ function sierpinski(noise) {
p.scale(80); p.scale(80);
p.translate(new Point(300, 300)); p.translate(new Point(300, 300));
p.strokeWidth = 1; p.strokeWidth = 1;
p.strokeColor = '#ff00ff';
} }

91
lindenmayer-system/lindenmeyer-system.js

@ -27,7 +27,6 @@ function generate(start, rules, iterations) {
ruleres = rule[1](); ruleres = rule[1]();
} else if(typeof rule[1] == 'object') { } else if(typeof rule[1] == 'object') {
var r = Math.floor(Math.random() * rule[1].length); var r = Math.floor(Math.random() * rule[1].length);
console.log(r);
ruleres = rule[1][r]; ruleres = rule[1][r];
}else { }else {
ruleres = rule[1]; ruleres = rule[1];
@ -46,24 +45,42 @@ function generate(start, rules, iterations) {
word = newword; word = newword;
consumed = newconsumed; consumed = newconsumed;
} }
return generate(newword, rules, iterations - 1); return generate(newword, rules, iterations - 1);
} }
function draw_initstate(p, state) { function draw_initstate(grp, state) {
if(!('dir' in state)) { if(!('dir' in state)) {
state.dir = 0; state.dir = 0;
state.turnnoise = 0; state.turnnoise = 0;
state.lennoise = 0; state.lennoise = 0;
} state.pos = new paper.Point(0, 0);
if(!('stepsize' in state)) { state.pathstack = [];
state.stepsize = 1; 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) { function draw_forward(factor=1) {
return function(p, state) { return function(grp, state) {
draw_initstate(p, state); if(state.curpath == null) {
if(state.curpath.segments.length == 0) { var p = new paper.Path();
state.curpath.add(new paper.Point(0, 0)); p.add(state.pos);
console.log('---------------------------'); 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 lastp = state.curpath.segments[state.curpath.segments.length - 1].point;
var noise = 1 + (Math.random() - 0.5) * state.lennoise; var noise = 1 + (Math.random() - 0.5) * state.lennoise;
state.curpath.add(lastp.add(new paper.Point( state.curpath.add(lastp.add(new paper.Point(
Math.sin(state.dir) * state.stepsize * factor * noise, Math.sin(state.dir) * state.stepsize * factor * noise,
Math.cos(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 { } else {
var f = factor; var f = factor;
} }
draw_initstate(p, state);
state.stepsize *= f; state.stepsize *= f;
}; };
} }
@ -120,7 +148,6 @@ function draw_stepsize_mul(factor) {
function draw_turn(angle) { function draw_turn(angle) {
return function(p, state) { return function(p, state) {
draw_initstate(p, state);
state.dir += angle; state.dir += angle;
}; };
} }
@ -128,7 +155,6 @@ function draw_turn(angle) {
function draw_angle_turn(fac) { function draw_angle_turn(fac) {
return function(p, state) { return function(p, state) {
draw_initstate(p, state);
var a = state.angle * (1 + (Math.random() - 0.5) * state.turnnoise); var a = state.angle * (1 + (Math.random() - 0.5) * state.turnnoise);
state.dir += a * fac; state.dir += a * fac;
}; };
@ -150,38 +176,45 @@ function draw_angle_add(delta) {
function draw_state_push() { function draw_state_push() {
return function(p, state) { return function(grp, state) {
if(typeof state.stack == 'undefined') {
state.stack = new Array();
}
state.stack.push({ state.stack.push({
pos: state.curpath.segments[state.curpath.segments.length - 1].point, pos: state.pos,
dir: state.dir, dir: state.dir,
stepsize: state.stepsize, 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() { function draw_state_pop() {
return function(p, state) { return function(grp, state) {
var s = state.stack.pop(); var s = state.stack.pop();
state.dir = s.dir; state.dir = s.dir;
state.stepsize = s.stepsize; state.stepsize = s.stepsize;
p.addChild(new paper.Path()); state.pos = s.pos;
state.curpath = p.lastChild; state.curpath = null;
state.curpath.add(s.pos);
}; };
} }
function draw(word, actions) { function draw(word, actions) {
var p = new paper.CompoundPath(); var grp = new paper.Group();
var state = {}; var state = {};
draw_initstate(grp, state);
for(let w of word) { for(let w of word) {
if(w in actions) { if(w in actions) {
actions[w](p, state); actions[w](grp, state);
} }
} }
return p; return grp;
} }

Loading…
Cancel
Save