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

91
lindenmayer-system/lindenmeyer-system.js

@ -27,7 +27,6 @@ function generate(start, rules, iterations) { @@ -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];
@ -51,19 +50,37 @@ function generate(start, rules, iterations) { @@ -51,19 +50,37 @@ function generate(start, rules, iterations) {
}
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;
}
if(!('curpath' in state)) {
p.addChild(new paper.Path());
state.curpath = p.lastChild;
}
state.stack = [];
state.curpath = null;
}
/*
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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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;
}

Loading…
Cancel
Save