#l-system #grammar

bin+lib lsystem

A Library for building L-Systems in rust

2 unstable releases

Uses old Rust 2015

0.2.1 Oct 27, 2015
0.1.0 Oct 26, 2015

#5 in #algae

48 downloads per month

MIT license

15KB
152 lines

L-Systems

Build Status

This library can be used to quickly build Lindenmayer systems (L-systems) from small, copyable atoms. There's really not much to it. Documentation coming soon.


lib.rs:

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);

No runtime deps