Socket
Book a DemoInstallSign in
Socket

@rgsoft/turtle

Package Overview
Dependencies
Maintainers
0
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@rgsoft/turtle

Turtle graphics and L-Systems library

latest
Source
npmnpm
Version
1.0.1
Version published
Maintainers
0
Created
Source

Turtle

Turtle and L-Systems library

L-Systems

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.

Generation

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

Rules

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.

Turtle Graphics

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.

SymbolAction
F, G, HAdvances forward a certain amount of steps drawing a line
MAdvances forward a certain amount of steps without drawing a line
LDraws a tree leaf
UUpscale drawing (divides current scale by scale ratio, with initial scale is 1)
DDownscale 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);

Turtle Config

The config argument for the Turtle class constructor has these properties:

PropertyTypeDescription
contextCanvasRenderingContext2DCanvas rendering context used for drawing
strokeSizenumberLenght of each step used stroke or move (may be affected by scaling)
strokeWeightnumberWidth of the stroke (may be affected by scaling)
anglenumberAngle used for each turn (+ and -)
scaleScaleConfigScale config

The ScaleConfig has this properties:

PropertyTypeDescription
rationumberScale ratio (between 0 and 1) to be applied when upscaling or downscaling
scaleStrokeSizebooleanOptional (default false), specifies if the stroke length must be scaled
scaleStrokeWeightbooleanOptional (default false), specifies if the stroke weight must be scaled
scaleAnglebooleanOptional (default false), specifies if the angle must be scaled

Keywords

turtle

FAQs

Package last updated on 17 Dec 2024

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.