
Product
Announcing Socket Fix 2.0
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
@rgsoft/turtle
Advanced tools
Turtle and L-Systems library
The LSystem
class is an implementation of the
Lindenmayer systems based on an
alphabet of symbols and production rules.
const lsystem = new LSystem(axiom, rules);
The axiom
is the base sentence from which future sentences will be generated
or inferred, and must be composed only by symbols of a defined alphabet. This
alphabet is based on the Turtle Graphics movements.
F, G, H, L, M, D, U, +, -, [, ]
The rules
is an array of strings or instances of the Rule
class, where each
one of them defines a production rule for spawning the next generation
sentence.
The generate
method in the LSystem
class spawns new generations of the
current sentence.
const axiom = 'F';
const rules = [
'F=>F[+F][-G]'
];
const lsystem = new LSystem(axiom, rules);
console.log(lsystem.sentence); // F
lsystem.generate();
console.log(lsystem.sentence); // F[+F][-G]
This method also admits a number argument that indicates the number of generations to generate.
const axiom = 'F';
const rules = [
'F=>FG'
];
const lsystem = new LSystem(axiom, rules);
console.log(lsystem.sentence); // F
lsystem.generate(4);
console.log(lsystem.sentence); // FGGGG
The Rule
class represents a production rule that allows the generation from a
symbol to a string of one or more symbols from the alphabet.
const str = 'F => F[+F][-F]';
const rules = [ new Rule(str) ];
console.log(rule.base); // F
console.log(rule.next); // ['F', '[', '+', 'F', ']', '[', '-', 'F', ']']
The LSystem
class uses this class to maintain and generate the sentences.
The Turtle
class is a clumsy attempt to use a "sentence" driven drawing engine
based on the turtle graphics.
The sentences use the same symbols in the LSystem
and Rule
alphabet, with
each symbol having a specific usage.
Symbol | Action |
---|---|
F , G , H | Advances forward a certain amount of steps drawing a line |
M | Advances forward a certain amount of steps without drawing a line |
L | Draws a tree leaf |
U | Upscale drawing (divides current scale by scale ratio, with initial scale is 1 ) |
D | Downscale drawing (multiplies current scale by scale ratio, with initial scale is 1 ) |
+ | Turn right a certain angle |
- | Turn left a certain angle |
[ | Saves current context |
] | Restores current context |
The render
method reads each symbol in the string, and performs an action
according to the previous table.
const turtle = new Turtle(config);
turtle.render(sentence);
The config
argument for the Turtle
class constructor has these properties:
Property | Type | Description |
---|---|---|
context | CanvasRenderingContext2D | Canvas rendering context used for drawing |
strokeSize | number | Lenght of each step used stroke or move (may be affected by scaling) |
strokeWeight | number | Width of the stroke (may be affected by scaling) |
angle | number | Angle used for each turn (+ and - ) |
scale | ScaleConfig | Scale config |
The ScaleConfig
has this properties:
Property | Type | Description |
---|---|---|
ratio | number | Scale ratio (between 0 and 1 ) to be applied when upscaling or downscaling |
scaleStrokeSize | boolean | Optional (default false ), specifies if the stroke length must be scaled |
scaleStrokeWeight | boolean | Optional (default false ), specifies if the stroke weight must be scaled |
scaleAngle | boolean | Optional (default false ), specifies if the angle must be scaled |
FAQs
Turtle graphics and L-Systems library
We found that @rgsoft/turtle demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.
Product
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.