This is L-system implementation done using rust.
My goal was to make it as easy as posslible to write L-systems for the programmers using rust features like match expressions.
To make an L-system you should only implement two traits for for your token. Here's an example for the dragon curve:
#[derive(Copy, Clone, PartialEq)]
enum DragonToken {
Forward,
Rotate(f32),
X,
Y,
Push,
Pop,
}
impl TurtleToken for DragonToken {
fn action(t: Self) -> Vec<TurtleAction> {
use DragonToken as DT;
use TurtleAction as TA;
match t {
DT::Forward => vec![TA::Move(3.0)],
DT::Rotate(rot) => vec![TA::Rotate(rot)],
DT::Push => vec![TA::Push],
DT::Pop => vec![TA::Pop],
_ => vec![],
}
}
}
impl LToken for DragonToken {
fn apply(token: Self) -> Vec<Self> {
use DragonToken as DT;
match token {
DT::X => vec![DT::X, DT::Rotate(PI / 2.0), DT::Y, DT::Forward],
DT::Y => vec![DT::Forward, DT::X, DT::Rotate(-PI / 2.0), DT::Y],
_ => vec![token],
}
}
}You'll get the following picture by runnung a dragon example by cargo r --release --example dragon
It's also possible to implement a stochastic system:
See 'tree' example. Other examples are:




