Skip to content

Commit

Permalink
Add support for the “turn around” command
Browse files Browse the repository at this point in the history
  • Loading branch information
Amphiluke committed Jan 3, 2024
1 parent 25696d2 commit 2769d6e
Show file tree
Hide file tree
Showing 14 changed files with 99 additions and 40 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@ If you rather prefer using ES modules in a browser, just choose the “esm” bu
</script>
```

## Supported commands

The following turtle commands are currently supported by lindsvg:

| Command | Description |
| ------------------- | ----------------------------------------------------- |
| `F` | Move forward one step with drawing a line |
| `B` | Move forward one step without drawing a line |
| `+` | Turn left by turning angle (theta) |
| `-` | Turn right by turning angle (theta) |
| `\|` | Reverse direction (turn by 180 degrees) |
| `[` | 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 |

## API &amp; examples

The module exports two pairs of methods.
Expand Down
25 changes: 18 additions & 7 deletions dist/lindsvg.cjs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/*!
lindsvg v1.3.3
lindsvg v1.4.0
https://amphiluke.github.io/lindsvg/
(c) 2023 Amphiluke
(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 @@ -117,6 +117,7 @@ let ctrlRules = {
B: "",
"+": "+",
"-": "-",
"|": "|",
"[": "[",
"]": "]",
};
Expand All @@ -136,7 +137,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 @@ -169,7 +170,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 All @@ -195,6 +196,10 @@ class Turtle {
this.alpha += factor * this.theta;
}

reverse(repeatCount = 1) {
this.alpha += (repeatCount % 2) * Math.PI;
}

pushStack(repeatCount = 1) {
for (; repeatCount > 0; repeatCount--) {
this.stack.push({x: this.x, y: this.y, alpha: this.alpha});
Expand Down Expand Up @@ -263,6 +268,9 @@ function getPathData(tokens, turtle) {
case "-":
turtle.rotate(-tokenLength);
break;
case "|":
turtle.reverse(tokenLength);
break;
case "[":
turtle.pushStack(tokenLength);
break;
Expand Down Expand Up @@ -312,6 +320,9 @@ function getMultiPathData(tokens, turtle) {
case "-":
turtle.rotate(-tokenLength);
break;
case "|":
turtle.reverse(tokenLength);
break;
case "[":
branchLevel += tokenLength;
turtle.pushStack(tokenLength);
Expand Down
25 changes: 18 additions & 7 deletions dist/lindsvg.esm.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*!
lindsvg v1.3.3
lindsvg v1.4.0
https://amphiluke.github.io/lindsvg/
(c) 2023 Amphiluke
(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 @@ -115,6 +115,7 @@ let ctrlRules = {
B: "",
"+": "+",
"-": "-",
"|": "|",
"[": "[",
"]": "]",
};
Expand All @@ -134,7 +135,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 @@ -167,7 +168,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 All @@ -193,6 +194,10 @@ class Turtle {
this.alpha += factor * this.theta;
}

reverse(repeatCount = 1) {
this.alpha += (repeatCount % 2) * Math.PI;
}

pushStack(repeatCount = 1) {
for (; repeatCount > 0; repeatCount--) {
this.stack.push({x: this.x, y: this.y, alpha: this.alpha});
Expand Down Expand Up @@ -261,6 +266,9 @@ function getPathData(tokens, turtle) {
case "-":
turtle.rotate(-tokenLength);
break;
case "|":
turtle.reverse(tokenLength);
break;
case "[":
turtle.pushStack(tokenLength);
break;
Expand Down Expand Up @@ -310,6 +318,9 @@ function getMultiPathData(tokens, turtle) {
case "-":
turtle.rotate(-tokenLength);
break;
case "|":
turtle.reverse(tokenLength);
break;
case "[":
branchLevel += tokenLength;
turtle.pushStack(tokenLength);
Expand Down
6 changes: 3 additions & 3 deletions dist/lindsvg.esm.min.js

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

25 changes: 18 additions & 7 deletions dist/lindsvg.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*!
lindsvg v1.3.3
lindsvg v1.4.0
https://amphiluke.github.io/lindsvg/
(c) 2023 Amphiluke
(c) 2024 Amphiluke
*/
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
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 @@ -121,6 +121,7 @@ https://amphiluke.github.io/lindsvg/
B: "",
"+": "+",
"-": "-",
"|": "|",
"[": "[",
"]": "]",
};
Expand All @@ -140,7 +141,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 @@ -173,7 +174,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 All @@ -199,6 +200,10 @@ https://amphiluke.github.io/lindsvg/
this.alpha += factor * this.theta;
}

reverse(repeatCount = 1) {
this.alpha += (repeatCount % 2) * Math.PI;
}

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

0 comments on commit 2769d6e

Please sign in to comment.