Skip to content

Commit

Permalink
Fix tokenization function. Filter out non-renderabe branching levels
Browse files Browse the repository at this point in the history
  • Loading branch information
Amphiluke committed Mar 14, 2020
1 parent 748f995 commit edabdf7
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 8 deletions.
8 changes: 6 additions & 2 deletions dist/lindsvg.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ function createTurtle({x, y, step, alpha, theta}) {
* @return {String[]}
*/
function tokenizeCodeword(codeword) {
return codeword.match(/([FB[\]+-])\1*/g);
return codeword.replace(/[^FB[\]+-]/g, "").match(/([FB[\]+-])\1*/g);
}

function formatCoordinates(x, y) {
Expand Down Expand Up @@ -257,7 +257,7 @@ function getPathData(tokens, turtle) {
function getMultiPathData(tokens, turtle) {
let prevCommand; // used to avoid unnecessary repeating of the commands L and M
let branchLevel = 0;
return tokens.reduce((accumulator, token) => {
let multiPathData = tokens.reduce((accumulator, token) => {
let pathData = accumulator[branchLevel] || "";
let tokenLength = token.length;
switch (token[0]) {
Expand Down Expand Up @@ -299,6 +299,10 @@ function getMultiPathData(tokens, turtle) {
accumulator[branchLevel] = pathData;
return accumulator;
}, ["M" + formatCoordinates(turtle.x, turtle.y)]);
// Some L-systems can produce branching levels which contain no real draw commands (only moves and rotations).
// Such L-systems usually don’t have F commands in their axiom nor they have a production for F (example is
// the Penrose tiling). Having <path> elements with only M commands is meaningless, so filtering them out
return multiPathData.filter(pathData => pathData.includes("L"));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion dist/lindsvg.esm.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions dist/lindsvg.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ https://amphiluke.github.io/l-systems/
* @return {String[]}
*/
function tokenizeCodeword(codeword) {
return codeword.match(/([FB[\]+-])\1*/g);
return codeword.replace(/[^FB[\]+-]/g, "").match(/([FB[\]+-])\1*/g);
}

function formatCoordinates(x, y) {
Expand Down Expand Up @@ -263,7 +263,7 @@ https://amphiluke.github.io/l-systems/
function getMultiPathData(tokens, turtle) {
let prevCommand; // used to avoid unnecessary repeating of the commands L and M
let branchLevel = 0;
return tokens.reduce((accumulator, token) => {
let multiPathData = tokens.reduce((accumulator, token) => {
let pathData = accumulator[branchLevel] || "";
let tokenLength = token.length;
switch (token[0]) {
Expand Down Expand Up @@ -305,6 +305,10 @@ https://amphiluke.github.io/l-systems/
accumulator[branchLevel] = pathData;
return accumulator;
}, ["M" + formatCoordinates(turtle.x, turtle.y)]);
// Some L-systems can produce branching levels which contain no real draw commands (only moves and rotations).
// Such L-systems usually don’t have F commands in their axiom nor they have a production for F (example is
// the Penrose tiling). Having <path> elements with only M commands is meaningless, so filtering them out
return multiPathData.filter(pathData => pathData.includes("L"));
}

/**
Expand Down
Loading

0 comments on commit edabdf7

Please sign in to comment.