Closed rexmalebka closed 4 years ago
Nice suggestion, this was also on my list for sure. Will have a look at it soon!
I merged it, but did make a few adjustments that I think fit the entire library a bit better.
Normal string expansion:
// Koch curve
Algo.linden('F', 2, {F: 'F+F-F-F+F'});
//=> 'F+F-F-F+F+F+F-F-F+F-F+F-F-F+F-F+F-F-F+F+F+F-F-F+F'
// Cantor set
Algo.linden('A', 3, {A: 'ABA', B: 'BBB'});
//=> 'ABABBBABABBBBBBBBBABABBBABA'
// Sierpinski Triangle
Algo.linden('F-G-G', 1, {'F': 'F−G+F+G−F', 'G' : 'GG'});
//=> 'F−G+F+G−F-GG-GG'
L-System with integers and arrays
Algo.linden();
//default => [1, 0, 1, 1, 0]
// Cantor set as 0's and 1's in an array ruleset
Algo.linden(1, 3, {1: [1, 0, 1], 0: [0, 0, 0]});
//=> [1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1]
// Set more complex rules for generating semitones for example
var complexRules = {
0: [0, 3, 7],
3: [-1, 0],
7: [12, 19, 0],
12: [12, 0, 0, 5],
5: [0, -3, 0]
}
Algo.linden(0, 2, complexRules);
//=> [0, 3, 7, -1, 0, 12, 19, 0, -1, 0, 3, 7, 12, 0, 0, 5, 19, 0, 3, 7]
Added L System function
it could be used with:
as it concerns, it works only with character rules only and only for strings, if we would like to use multiple character keys we need to use regex.
for example:
this won't work for
AB : 'X'
ruleAlso I was thinking if this could return a generator so people could yield values whenever they want, removing recursion things, I don't know if this should be kept as simple as possible.