1.0.0 • Published 12 months ago

@rgsoft/turtle v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
12 months ago

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, 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, GAdvances 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
1.0.0

12 months ago