Define different types of productions
mainLindenmayer.js supports several production formats to handle different L-System complexities:
1. String-Based
Simple replacement using a string.
myLsystem.setProduction('F', 'FF');
2. Array-Based (Parametric)
Used for parametric L-Systems where symbols carry custom parameters. Each object in the array must have a symbol property.
myLsystem.setProduction('F', [
{symbol: 'F', params: [5,6,1]},
{symbol: 'F', params: [1,0,0]}
]);3. Object-Based (Context-Sensitive, Conditional, Stochastic)
Wrap a successor in an object to add advanced logic:
- Context-Sensitive: Use
leftCtxandrightCtxto define required neighbors. - Conditional: Use a
conditionfunction that returns a boolean. - Stochastic: Use the
successorsarray withweightproperties to define probabilities.
// Context-sensitive
myLsystem.setProduction('F', { successor: 'FF', leftCtx: 'FB' });
// Conditional
myLsystem.setProduction('F', { successor: 'FF', condition: () => true });
// Stochastic
myLsystem.setProduction('B', {
successors: [
{weight: 50, successor: 'X'},
{weight: 50, successor: 'Y'}
]
});4. Function-Based
Provides maximum flexibility. The function receives an info object and returns a successor (String or Array).
The info object contains:
index: Current index in the axiom.part: The current symbol part (the full object if using Array-based axioms).params: Shorthand forpart.params.currentAxiom: Reference to the current axiom.
myLsystem.setProduction('F', ({index, part}) => {
// Access custom parameters from 'part'
return [{symbol: 'F', size: part.size / 2}];
});