Skip to content

Commit

Permalink
Add support for the “swap sign” command
Browse files Browse the repository at this point in the history
  • Loading branch information
Amphiluke committed Jan 4, 2024
1 parent 2769d6e commit 35f711b
Show file tree
Hide file tree
Showing 15 changed files with 81 additions and 36 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ The following turtle commands are currently supported by lindsvg:
| `+` | Turn left by turning angle (theta) |
| `-` | Turn right by turning angle (theta) |
| `\|` | Reverse direction (turn by 180 degrees) |
| `!` | Reverse the meaning of `+` and `-` |
| `[` | Push current state of the turtle onto the stack |
| `]` | Pop a state from the stack and apply it to the turtle |
| `A`,`C``E`,`G``Z` | Auxiliary user-defined rules |
Expand Down
23 changes: 17 additions & 6 deletions dist/lindsvg.cjs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/*!
lindsvg v1.4.0
lindsvg v1.5.0
https://amphiluke.github.io/lindsvg/
(c) 2024 Amphiluke
*/
'use strict';

let messages = {
AXIOM: "Axiom may only contain the following characters: A..Z,+,-,|,[,]",
RULE: "Production rules may only contain the following characters: A..Z,+,-,|,[,]",
AXIOM: "Axiom may only contain the following characters: A..Z,+,-,|,!,[,]",
RULE: "Production rules may only contain the following characters: A..Z,+,-,|,!,[,]",
LETTER: "Allowed alphabet letters are: A..Z",
ALPHA: "The “alpha” parameter must be a finite number",
THETA: "The “theta” parameter must be a finite number",
Expand All @@ -21,7 +21,7 @@ function checkLetter(letter, msg = messages.LETTER) {
return letterRE.test(letter) || msg;
}

let ruleRE = /^[A-Z+\-[\]|]*$/;
let ruleRE = /^[A-Z+\-|![\]]*$/;
function checkRule(rule, msg = messages.RULE) {
return ruleRE.test(rule) || msg;
}
Expand Down Expand Up @@ -118,6 +118,7 @@ let ctrlRules = {
"+": "+",
"-": "-",
"|": "|",
"!": "!",
"[": "[",
"]": "]",
};
Expand All @@ -137,7 +138,7 @@ let defaults = {
*/
function cleanCodeword(codeword) {
// Remove auxiliary drawing-indifferent letters
let cleanCodeword = codeword.replace(/[^FB[\]+-|]/g, "");
let cleanCodeword = codeword.replace(/[^FB[\]+-|!]/g, "");
do {
codeword = cleanCodeword;
// Remove useless brackets that don’t contain F commands or other brackets (preserving bracket balance!)
Expand Down Expand Up @@ -170,7 +171,7 @@ function generateCodeword(lsParams) {
* @return {String[]}
*/
function tokenizeCodeword(codeword) {
return codeword.match(/([FB[\]+-|])\1*/g); // tokenize
return codeword.match(/([FB[\]+-|!])\1*/g); // tokenize
}

class Turtle {
Expand Down Expand Up @@ -200,6 +201,10 @@ class Turtle {
this.alpha += (repeatCount % 2) * Math.PI;
}

swapSigns(repeatCount = 1) {
this.theta *= (-1) ** repeatCount;
}

pushStack(repeatCount = 1) {
for (; repeatCount > 0; repeatCount--) {
this.stack.push({x: this.x, y: this.y, alpha: this.alpha});
Expand Down Expand Up @@ -271,6 +276,9 @@ function getPathData(tokens, turtle) {
case "|":
turtle.reverse(tokenLength);
break;
case "!":
turtle.swapSigns(tokenLength);
break;
case "[":
turtle.pushStack(tokenLength);
break;
Expand Down Expand Up @@ -323,6 +331,9 @@ function getMultiPathData(tokens, turtle) {
case "|":
turtle.reverse(tokenLength);
break;
case "!":
turtle.swapSigns(tokenLength);
break;
case "[":
branchLevel += tokenLength;
turtle.pushStack(tokenLength);
Expand Down
23 changes: 17 additions & 6 deletions dist/lindsvg.esm.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*!
lindsvg v1.4.0
lindsvg v1.5.0
https://amphiluke.github.io/lindsvg/
(c) 2024 Amphiluke
*/
let messages = {
AXIOM: "Axiom may only contain the following characters: A..Z,+,-,|,[,]",
RULE: "Production rules may only contain the following characters: A..Z,+,-,|,[,]",
AXIOM: "Axiom may only contain the following characters: A..Z,+,-,|,!,[,]",
RULE: "Production rules may only contain the following characters: A..Z,+,-,|,!,[,]",
LETTER: "Allowed alphabet letters are: A..Z",
ALPHA: "The “alpha” parameter must be a finite number",
THETA: "The “theta” parameter must be a finite number",
Expand All @@ -19,7 +19,7 @@ function checkLetter(letter, msg = messages.LETTER) {
return letterRE.test(letter) || msg;
}

let ruleRE = /^[A-Z+\-[\]|]*$/;
let ruleRE = /^[A-Z+\-|![\]]*$/;
function checkRule(rule, msg = messages.RULE) {
return ruleRE.test(rule) || msg;
}
Expand Down Expand Up @@ -116,6 +116,7 @@ let ctrlRules = {
"+": "+",
"-": "-",
"|": "|",
"!": "!",
"[": "[",
"]": "]",
};
Expand All @@ -135,7 +136,7 @@ let defaults = {
*/
function cleanCodeword(codeword) {
// Remove auxiliary drawing-indifferent letters
let cleanCodeword = codeword.replace(/[^FB[\]+-|]/g, "");
let cleanCodeword = codeword.replace(/[^FB[\]+-|!]/g, "");
do {
codeword = cleanCodeword;
// Remove useless brackets that don’t contain F commands or other brackets (preserving bracket balance!)
Expand Down Expand Up @@ -168,7 +169,7 @@ function generateCodeword(lsParams) {
* @return {String[]}
*/
function tokenizeCodeword(codeword) {
return codeword.match(/([FB[\]+-|])\1*/g); // tokenize
return codeword.match(/([FB[\]+-|!])\1*/g); // tokenize
}

class Turtle {
Expand Down Expand Up @@ -198,6 +199,10 @@ class Turtle {
this.alpha += (repeatCount % 2) * Math.PI;
}

swapSigns(repeatCount = 1) {
this.theta *= (-1) ** repeatCount;
}

pushStack(repeatCount = 1) {
for (; repeatCount > 0; repeatCount--) {
this.stack.push({x: this.x, y: this.y, alpha: this.alpha});
Expand Down Expand Up @@ -269,6 +274,9 @@ function getPathData(tokens, turtle) {
case "|":
turtle.reverse(tokenLength);
break;
case "!":
turtle.swapSigns(tokenLength);
break;
case "[":
turtle.pushStack(tokenLength);
break;
Expand Down Expand Up @@ -321,6 +329,9 @@ function getMultiPathData(tokens, turtle) {
case "|":
turtle.reverse(tokenLength);
break;
case "!":
turtle.swapSigns(tokenLength);
break;
case "[":
branchLevel += tokenLength;
turtle.pushStack(tokenLength);
Expand Down
4 changes: 2 additions & 2 deletions dist/lindsvg.esm.min.js

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

23 changes: 17 additions & 6 deletions dist/lindsvg.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
lindsvg v1.4.0
lindsvg v1.5.0
https://amphiluke.github.io/lindsvg/
(c) 2024 Amphiluke
*/
Expand All @@ -10,8 +10,8 @@ https://amphiluke.github.io/lindsvg/
})(this, (function (exports) { 'use strict';

let messages = {
AXIOM: "Axiom may only contain the following characters: A..Z,+,-,|,[,]",
RULE: "Production rules may only contain the following characters: A..Z,+,-,|,[,]",
AXIOM: "Axiom may only contain the following characters: A..Z,+,-,|,!,[,]",
RULE: "Production rules may only contain the following characters: A..Z,+,-,|,!,[,]",
LETTER: "Allowed alphabet letters are: A..Z",
ALPHA: "The “alpha” parameter must be a finite number",
THETA: "The “theta” parameter must be a finite number",
Expand All @@ -25,7 +25,7 @@ https://amphiluke.github.io/lindsvg/
return letterRE.test(letter) || msg;
}

let ruleRE = /^[A-Z+\-[\]|]*$/;
let ruleRE = /^[A-Z+\-|![\]]*$/;
function checkRule(rule, msg = messages.RULE) {
return ruleRE.test(rule) || msg;
}
Expand Down Expand Up @@ -122,6 +122,7 @@ https://amphiluke.github.io/lindsvg/
"+": "+",
"-": "-",
"|": "|",
"!": "!",
"[": "[",
"]": "]",
};
Expand All @@ -141,7 +142,7 @@ https://amphiluke.github.io/lindsvg/
*/
function cleanCodeword(codeword) {
// Remove auxiliary drawing-indifferent letters
let cleanCodeword = codeword.replace(/[^FB[\]+-|]/g, "");
let cleanCodeword = codeword.replace(/[^FB[\]+-|!]/g, "");
do {
codeword = cleanCodeword;
// Remove useless brackets that don’t contain F commands or other brackets (preserving bracket balance!)
Expand Down Expand Up @@ -174,7 +175,7 @@ https://amphiluke.github.io/lindsvg/
* @return {String[]}
*/
function tokenizeCodeword(codeword) {
return codeword.match(/([FB[\]+-|])\1*/g); // tokenize
return codeword.match(/([FB[\]+-|!])\1*/g); // tokenize
}

class Turtle {
Expand Down Expand Up @@ -204,6 +205,10 @@ https://amphiluke.github.io/lindsvg/
this.alpha += (repeatCount % 2) * Math.PI;
}

swapSigns(repeatCount = 1) {
this.theta *= (-1) ** repeatCount;
}

pushStack(repeatCount = 1) {
for (; repeatCount > 0; repeatCount--) {
this.stack.push({x: this.x, y: this.y, alpha: this.alpha});
Expand Down Expand Up @@ -275,6 +280,9 @@ https://amphiluke.github.io/lindsvg/
case "|":
turtle.reverse(tokenLength);
break;
case "!":
turtle.swapSigns(tokenLength);
break;
case "[":
turtle.pushStack(tokenLength);
break;
Expand Down Expand Up @@ -327,6 +335,9 @@ https://amphiluke.github.io/lindsvg/
case "|":
turtle.reverse(tokenLength);
break;
case "!":
turtle.swapSigns(tokenLength);
break;
case "[":
branchLevel += tokenLength;
turtle.pushStack(tokenLength);
Expand Down
Loading

0 comments on commit 35f711b

Please sign in to comment.