UNPKG

toosoon-lsystem

Version:

Library providing functionalities for creating and manipulating Lindenmayer systems (L-Systems) using various parameters

130 lines (129 loc) 4.28 kB
import type { Alphabet, Axiom, AxiomParameter, AxiomPart, Command, CommandKey, Commands, DefaultAlphabet, Define, DefineKey, Defines, IgnoredAlphabet, Production, ProductionParameter, ProductionResult, Productions, SuccessorParameter, Symbol } from './types'; export type LSystemParameters<A extends Alphabet, I extends Alphabet = IgnoredAlphabet> = { readonly alphabet?: A; readonly ignoredSymbols?: I; axiom?: AxiomParameter<A | I>; iterations?: number; defines?: { [key in DefineKey]?: Define; }; productions?: { [successorParameter in SuccessorParameter<A>]?: ProductionParameter<A, I>; }; commands?: { [key in CommandKey<A, I>]?: Command<A, I>; }; }; /** * LSystem class * * @exports * @class LSystem */ export default class LSystem<A extends Alphabet = DefaultAlphabet, I extends Alphabet = IgnoredAlphabet> { readonly alphabet: A; readonly ignoredSymbols: I; axiom: Axiom<A | I>; iterations: number; defines: Defines; productions: Productions<A, I>; commands: Commands<A, I>; constructor({ alphabet, ignoredSymbols, axiom, iterations, defines, productions, commands }: LSystemParameters<A, I>); /** * Set the axiom of the L-System * * @param {AxiomParameter} axiom Initial phrase of this L-System */ setAxiom(axiom: AxiomParameter<A | I>): void; /** * Set a define for this L-System * * @param {DefineKey} key Key for defining constant * @param {Define} define A constant value */ setDefine(key: DefineKey, define: Define): void; /** * Set multiple defines for the L-System. * * @param {object} defines Collection of defined constants */ setDefines(defines: { [key in DefineKey]?: Define; }): void; /** * Clear all defines from this L-System */ clearDefines(): void; /** * Set a production for the L-System. * * @param {SuccessorParameter} successorParameter Successor symbol mapped to the production * @param {ProductionParameter} productionParameter Production rule mapped to the symbol */ setProduction(successorParameter: SuccessorParameter<A>, productionParameter: ProductionParameter<A, I>): void; /** * Set multiple productions for this L-System * * @param {object} productions Collection of production rules mapped to symbols */ setProductions(productions: { [successorParameter in SuccessorParameter<A>]?: ProductionParameter<A, I>; }): void; /** * Clear all productions from the L-System */ clearProductions(): void; /** * Return the result of a production rule * * @param {Production} production * @param {AxiomPart} part * @param {number} index * @param {boolean} [recursive=false] * @returns {ProductionResult} */ protected getProductionResult(production: Production<A, I>, part: AxiomPart<A | I>, index: number, recursive?: boolean): ProductionResult<A | I>; /** * Apply productions rules on current axiom. * It corresponds to 1 iteration of this L-System. * * @returns {Axiom} */ protected applyProductions(): Axiom<A | I>; /** * Set a command for this L-System * * @param {Symbol} symbol Symbol used as a key for the command * @param {Command} command Function to be executed for each corresponding symbol */ setCommand(symbol: Symbol<A | I>, command: Command<A, I>): void; /** * Set multiple commands for this L-System * * @param {object} commands Collection of commands mapped to symbols */ setCommands(commands: { [key in CommandKey<A, I>]?: Command<A, I>; }): void; /** * Clear all commands from this L-System */ clearCommands(): void; /** * Execute the commands defined in this L-System */ run(): void; /** * Perform a specified number of iterations on this L-System * * @param {number} [iterations] Number of iterations * @returns {Axiom} */ iterate(iterations?: number): Axiom<A | I>; /** * Get the current axiom of this L-System * * @returns {string} */ getAxiomString(): string; }