Skip to content

Commit

Permalink
feat(lsys): add g turtle command, update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Feb 26, 2019
1 parent b6a3ca0 commit 4d06992
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
26 changes: 14 additions & 12 deletions packages/lsys/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ This project is part of the
- [Installation](#installation)
- [Dependencies](#dependencies)
- [Usage examples](#usage-examples)
- [Probabilistic behaviors](#probabilistic-behaviors)
- [Stochastic behaviors](#stochastic-behaviors)
- [Default turtle](#default-turtle)
- [Options](#options)
- [Symbols](#symbols)
Expand All @@ -27,9 +27,9 @@ This project is part of the

Small, functional, highly customizable, iterator based
[L-System](https://en.wikipedia.org/wiki/L-system) architecture for
arbitrary rules, with separation between symbol expansion and
interpretation / execution. A base 2D turtle implementation is included.
0.5KB gzipped.
arbitrary rules, basic support for stochastic behaviors and with
separation between symbol expansion and interpretation / execution. A
base 2D turtle implementation is included. 0.6KB gzipped.

Partially based on Clojure version of
[@thi.ng/thingybot](https://github.com/thi-ng/thingybot).
Expand All @@ -38,8 +38,9 @@ Partially based on Clojure version of

ALPHA. Planned features:

- [ ] parametric symbols / expansion / pruning
- [ ] parametric grammars
- [ ] max expansion length enforcement
- [ ] convergence testing
- [ ] 3D turtle implementation

## Installation
Expand Down Expand Up @@ -101,14 +102,14 @@ examples.forEach(({ rules, delta, iter }, i) =>
);
```

### Probabilistic behaviors
### Stochastic behaviors

The built-in default turtle implementation supports some basic
probabilistic features, e.g. randomization of growth direction and
probabilistic branch termination. This enables the
creation of more organic looking structures, like shown in the following example:
stochastic features, e.g. randomization of growth direction and
stochastic branch termination. This enables the creation of more organic
looking structures, like shown in the following example:

![probabilistic L-system](https://raw.githubusercontent.com/thi-ng/umbrella/master/assets/lsys-tree.png)
![stochastic L-system](https://raw.githubusercontent.com/thi-ng/umbrella/master/assets/lsys-tree.png)

```ts
import { XsAdd } from "@thi.ng/random";
Expand Down Expand Up @@ -208,13 +209,14 @@ rnd: IRandom;

### Symbols

- `f` - move forward
- `f` - move forward & add to current path
- `g` - move forward & start new path
- `+` - rotate ccw
- `-` - rotate cw
- `>` - shrink rotation angle offset
- `<` - grow rotation angle offset
- `/` - jitter direction
- `k` - probabilistically kill branch
- `k` - stochastically kill branch
- `p` - decay survival chance
- `P` - increase survival chance
- `!` - decay step distance
Expand Down
9 changes: 8 additions & 1 deletion packages/lsys/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ export const TURTLE_IMPL_2D: RuleImplementations<Turtle2D> = {
ctx.curr.push(ctx.pos);
}
},
"g": (ctx) => {
if (ctx.alive) {
ctx.curr.length > 1 && ctx.paths.push(ctx.curr);
ctx.pos = add([], ctx.pos, cossin(ctx.theta, ctx.step));
ctx.curr = [ctx.pos];
}
},
// rotate ccw
"+": (ctx) => ctx.alive && (ctx.theta += ctx.delta),
// rotate cw
Expand All @@ -100,7 +107,7 @@ export const TURTLE_IMPL_2D: RuleImplementations<Turtle2D> = {
// grow step distance
"^": (ctx) => ctx.alive && (ctx.step /= ctx.decayStep),
"/": (ctx) => ctx.alive && (ctx.theta += ctx.rnd.norm(ctx.delta * ctx.jitter)),
// kill branch (probabilistic)
// kill branch (stochastic)
"k": (ctx) => ctx.alive && (ctx.alive = ctx.rnd.float() < ctx.aliveProb),
// decay alive probability
"p": (ctx) => ctx.alive && (ctx.aliveProb *= ctx.decayAlive),
Expand Down

0 comments on commit 4d06992

Please sign in to comment.