Crate lsystem [−] [src]
A Library for specifying an L-System. Formally, L-system formulations consist of a vocabulary set, a starting axiom, and a set of production rules.
In this implementation, LSystem<T>
is a fully formulated L-system on the
alphabet of all instances of type T. Rule sets are any type that
implements the trait LRules<T>
.
Examples
Here is how you would write the original algae system used by Lindenmayer:
use lsystem::{LSystem, LRules, MapRules}; let mut rules = MapRules::new(); rules.set_str('A', "AB"); rules.set_str('B', "A"); let axiom = "A".chars().collect(); let mut system = LSystem::new(rules, axiom); let out = system.next().unwrap(); let expected: Vec<char> = "AB".chars().collect(); assert_eq!(expected, out); let out = system.next().unwrap(); let expected: Vec<char> = "ABA".chars().collect(); assert_eq!(expected, out); let out = system.next().unwrap(); let expected: Vec<char> = "ABAAB".chars().collect(); assert_eq!(expected, out);
This is how you can write the Pythagoras Tree system:
use lsystem::{LSystem, LRules, MapRules}; let mut rules = MapRules::new(); rules.set_str('1', "11"); rules.set_str('0', "1[0]0"); let axiom = "0".chars().collect(); let mut system = LSystem::new(rules, axiom); let out = system.next().unwrap(); let expected: Vec<char> = "1[0]0".chars().collect(); assert_eq!(expected, out); let out = system.next().unwrap(); let expected: Vec<char> = "11[1[0]0]1[0]0".chars().collect(); assert_eq!(expected, out); let out = system.next().unwrap(); let expected: Vec<char> = "1111[11[1[0]0]1[0]0]11[1[0]0]1[0]0".chars().collect(); assert_eq!(expected, out);
The MapRules struct is not restricted to strings. You can just as easily use any type that can be stored in a hashmap.
use lsystem::{LSystem, LRules, MapRules}; let mut rules = MapRules::new(); rules.set(0, vec![1, 0]); rules.set(1, vec![0, 1, 1]); let axiom = vec![0]; let mut system = LSystem::new(rules, axiom); let out = system.next().unwrap(); let expected = vec![1, 0]; assert_eq!(expected, out); let out = system.next().unwrap(); let expected = vec![0, 1, 1, 1, 0]; assert_eq!(expected, out);
Structs
LSystem |
A type containing the full specification for an L-system. |
MapRules |
A simple production ruleset that maps an atom to an atom string using a lookup table. |
Traits
LRules |
A set of production rule for an L-system, which maps an item to a list of items which will replace it in the L-system state. |
Functions
show |
A convenience function to print out the String representation of a char vector. |