Browse Source

represent words as strings instead of arrays

master
Fr3deric 5 years ago
parent
commit
ad236f0e51
  1. 37
      lindenmayer-system/lindenmeyer-system.html
  2. 4
      lindenmayer-system/lindenmeyer-system.js

37
lindenmayer-system/lindenmeyer-system.html

@ -30,10 +30,10 @@ var r = generate('A'.split(''),
function dragon_curve(variant) { function dragon_curve(variant) {
project.clear(); project.clear();
var r = generate( var r = generate(
'FX'.split(''), 'FX',
[ [
['X'.split(''), 'X+YF+'.split('')], ['X', 'X+YF+'],
['Y'.split(''), '-FX-Y'.split('')] ['Y', '-FX-Y']
], ],
12 12
); );
@ -70,17 +70,20 @@ function sierpinski2(num_defects) {
project.clear(); project.clear();
if(typeof num_defects == 'undefined') { num_defects = 0 }; if(typeof num_defects == 'undefined') { num_defects = 0 };
var r = generate( var r = generate(
'IA'.split(''), 'IA',
[ [
['A'.split(''), 'B-A-B'.split('')], ['A', 'B-A-B'],
['B'.split(''), 'A+B+A'.split('')] ['B', 'A+B+A']
], ],
8 8
); );
for(var i=0; i<num_defects; i++) { if(num_defects > 0) {
r[Math.floor(Math.random()*r.length)] = '*'; r = r.split('');
} for(var i=0; i<num_defects; i++) {
r[Math.floor(Math.random()*r.length)] = '*';
}
}
var actions = { var actions = {
'A': draw_forward(), 'A': draw_forward(),
@ -104,10 +107,10 @@ function sierpinski2(num_defects) {
function sierpinski() { function sierpinski() {
project.clear(); project.clear();
var r = generate( var r = generate(
'IF-G-G'.split(''), 'IF-G-G',
[ [
['F'.split(''), 'F-G+F+G-F'.split('')], ['F', 'F-G+F+G-F'],
['G'.split(''), 'GG'.split('')] ['G', 'GG']
], ],
6 6
); );
@ -137,10 +140,10 @@ function sierpinski() {
project.clear(); project.clear();
if(typeof num_defects == 'undefined') { num_defects = 0 }; if(typeof num_defects == 'undefined') { num_defects = 0 };
var r = generate( var r = generate(
'iIXF'.split(''), 'iIXF',
[ [
['X'.split(''), 'F+[[X]-X]-F[-FX]+X'.split('')], ['X', 'F+[[X]-X]-F[-FX]+X'],
['F'.split(''), 'FF'.split('')] ['F', 'FF']
], ],
6 6
); );
@ -190,9 +193,9 @@ function sierpinski() {
project.clear(); project.clear();
if(typeof num_defects == 'undefined') { num_defects = 0 }; if(typeof num_defects == 'undefined') { num_defects = 0 };
var r = generate( var r = generate(
'iIjJX'.split(''), 'iIjJX',
[ [
['X'.split(''), 'F[-lfX][cfX][+rfX]'.split('')], ['X', 'F[-lfX][cfX][+rfX]'],
//['F'.split(''), 'FF'.split('')], //['F'.split(''), 'FF'.split('')],
//['L'.split(''), 'LL'.split('')], //['L'.split(''), 'LL'.split('')],
//['C'.split(''), 'CC'.split('')], //['C'.split(''), 'CC'.split('')],

4
lindenmayer-system/lindenmeyer-system.js

@ -28,13 +28,13 @@ function generate(start, rules, iterations) {
} else { } else {
ruleres = rule[1]; ruleres = rule[1];
} }
newword.push(...ruleres); newword += ruleres;
for(let i=0; i<ruleres.length; i++) { for(let i=0; i<ruleres.length; i++) {
newconsumed.push(true); newconsumed.push(true);
} }
pos += rule[0].length; pos += rule[0].length;
} else { } else {
newword.push(word[pos]); newword += word[pos];
newconsumed.push(false); newconsumed.push(false);
pos += 1; pos += 1;
} }

Loading…
Cancel
Save