-
Notifications
You must be signed in to change notification settings - Fork 0
/
weed.js
33 lines (28 loc) · 908 Bytes
/
weed.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// lsys like (X → F+[[X]-X]-F[-FX]+X), (F → FF)
function expandLSystem(input, replacementRules, iteration) {
if (iteration === 0) {
return input;
}
const output = input.split('').reduce((acc, char) => {
if (replacementRules[char]) {
return acc + replacementRules[char];
} else {
return acc + char;
}
} , '');
return expandLSystem(output, replacementRules, iteration - 1);
}
function LSystemToPlumetoScript(input){
return input.replace(/F/g, 'forward 10\n')
.replace(/\+/g, 'rotate 25 deg\n')
.replace(/-/g, 'rotate -25 deg\n')
.replace(/\[/g, 'breadcrumb push\n')
.replace(/\]/g, 'breadcrumb pop\n')
.replace(/X/g, '');
}
const lSystem = expandLSystem('X', {
X: 'F+[[X]-X]-F[-FX]+X',
F: 'FF'
}, 4);
const script = LSystemToPlumetoScript(lSystem);
console.log(script)