2.1.123 • Published 7 days ago

@thi.ng/lsys v2.1.123

Weekly downloads
95
License
Apache-2.0
Repository
github
Last release
7 days ago

lsys

npm version npm downloads Twitter Follow

This project is part of the @thi.ng/umbrella monorepo.

About

Small, functional, highly customizable, iterator based L-System architecture for 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.

Planned features:

  • parametric grammars
  • max expansion length enforcement
  • convergence testing
  • 3D turtle implementation

Status

STABLE - used in production

Search or submit any issues for this package

Blog posts

Installation

yarn add @thi.ng/lsys

ES module import:

<script type="module" src="https://cdn.skypack.dev/@thi.ng/lsys"></script>

Skypack documentation

For Node.js REPL:

# with flag only for < v16
node --experimental-repl-await

> const lsys = await import("@thi.ng/lsys");

Package sizes (gzipped, pre-treeshake): ESM: 728 bytes

Dependencies

API

Generated API docs

Examples
exampleexample
exampleexample
import * as lsys from "@thi.ng/lsys";
import * as g from "@thi.ng/geom";
import * as fs from "fs";

// example L-Systems shown above

const examples = [
    { rules: { s: "[f++f++f]", f: "f+f--f+f" }, delta: Math.PI / 3, iter: 5 },
    { rules: { s: "[f-f-f-f-f-f-f-f]", f: "f---f+f+f+f+f+f+f---f" }, delta: Math.PI / 4, iter: 6 },
    { rules: { s: "[x]", x: "-yf+xfx+fy-", y: "+xf-yfy-fx+" }, delta: Math.PI / 2, iter: 7 },
    { rules: { s: "[a]", a: "a-b--b+a++aa+b-", b: "+a-bb--b-a++a+b" }, delta: Math.PI / 3, iter: 5 }
];

const impl = lsys.TURTLE_IMPL_2D;

examples.forEach(({ rules, delta, iter }, i) =>
    fs.writeFileSync(
        `lsys-ex${i}.svg`,
        g.asSvg(
            g.svgDoc(
                { stroke: "#00f", "stroke-width": 0.25, width: 600, height: 600 },
                ...lsys.interpret(
                    // create turtle instance with customized delta (rot angle)
                    lsys.turtle2d({ delta }),
                    // customize implementation to process syms "a" & "b" as "f"
                    { ...impl, a: impl.f, b: impl.f },
                    // recursively expand start rule "s"
                    lsys.expand(rules, "s", iter)
                    //convert result paths to polylines for SVG export
                ).paths.map(g.polyline)
            )
        )
    )
);

Stochastic behaviors

The built-in default turtle implementation supports some basic 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:

stochastic L-system

import { XsAdd } from "@thi.ng/random";

lsys.interpret(
    // create turtle instance with customized delta (rot angle)
    lsys.turtle2d({
        // initial movement step distance
        step: 20,
        // initial direction
        theta: -PI / 2,
        // rotation offset
        delta: PI / 10,
        // direction jitter (percentage of delta, i.e. here 50%)
        jitter: 0.5,
        // initial survival chance
        aliveProb: 0.999,
        // decay factors for rotation, step, branch survival chance
        decayDelta: 0.98,
        decayStep: 0.85,
        decayAlive: 0.975,
        // use seedable PRNG for deterministic outcome
        rnd: new XsAdd(0x7337c0de)
    }),
    // process syms "a" & "g" as "f"
    { ...impl, a: impl.f, g: impl.f },
    // recursively expand start rule "s" by ping-ponging between f & g
    // (only difference between f & g is swapped branch orientations)
    // see description of all symbols further below
    lsys.expand(
        {
            s: "[f]",
            f: "a[kp!>/-g]/a[kp!>/+g]",
            g: "a[kp!>/+f]/a[kp!>/-f]"
        },
        "s",
        13
    )
)

Default turtle

Options

The turtle2d() function creates a new state object for the L-System interpreter (interpret()). The initial state can be customized by providing a config object with the following options:

/**
 * Current position
 */
pos: Vec;
/**
 * Current direction (in radians)
 */
theta: number;
/**
 * Rotation angle for "+" / "-" symbols
 */
delta: number;
/**
 * Max. random direction change when processing "/" symbol.
 * Normalized percentage of `delta`. Default: 0.25 (25%)
 */
jitter: number;
/**
 * Step distance. Default: 1
 */
step: number;
/**
 * Probability to keep current branch alive when processing "k"
 * symbol. Default: 0.99
 */
aliveProb: number;
/**
 * Decay factor for `delta`. Should be in (0,1) interval.
 * Default: 0.9
 */
decayDelta: number;
/**
 * Decay factor for `step`. Should be in (0,1) interval.
 * Default: 0.9
 */
decayStep: number;
/**
 * Decay factor for `aliveProp`.
 * Default: 0.95
 */
decayAlive: number;
/**
 * PRNG to use for probability checks. Default: SYSTEM
 */
rnd: IRandom;

Symbols

  • 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 - stochastically kill branch
  • p - decay survival chance
  • P - increase survival chance
  • ! - decay step distance
  • ^ - grow step distance
  • [ - start branch / store context on stack
  • ] - end branch / pop context from stack

Authors

Karsten Schmidt

If this project contributes to an academic publication, please cite it as:

@misc{thing-lsys,
  title = "@thi.ng/lsys",
  author = "Karsten Schmidt",
  note = "https://thi.ng/lsys",
  year = 2019
}

License

© 2019 - 2021 Karsten Schmidt // Apache Software License 2.0

2.1.123

7 days ago

2.1.122

10 days ago

2.1.121

20 days ago

2.1.120

21 days ago

2.1.119

22 days ago

2.1.118

27 days ago

2.1.117

28 days ago

2.1.116

1 month ago

2.1.115

1 month ago

2.1.114

1 month ago

2.1.113

1 month ago

2.1.112

1 month ago

2.1.111

2 months ago

2.1.110

2 months ago

2.1.109

2 months ago

2.1.107

2 months ago

2.1.106

2 months ago

2.1.108

2 months ago

2.1.105

2 months ago

2.1.104

2 months ago

2.1.103

2 months ago

2.1.102

2 months ago

2.1.101

2 months ago

2.1.98

3 months ago

2.1.99

3 months ago

2.1.100

3 months ago

2.1.96

3 months ago

2.1.95

3 months ago

2.1.94

4 months ago

2.1.93

4 months ago

2.1.92

4 months ago

2.1.90

4 months ago

2.1.91

4 months ago

2.1.89

4 months ago

2.1.88

4 months ago

2.1.87

4 months ago

2.1.85

5 months ago

2.1.86

5 months ago

2.1.83

5 months ago

2.1.84

5 months ago

2.1.81

6 months ago

2.1.80

6 months ago

2.1.58

9 months ago

2.1.57

9 months ago

2.1.69

8 months ago

2.1.67

8 months ago

2.1.68

8 months ago

2.1.65

8 months ago

2.1.66

8 months ago

2.1.63

8 months ago

2.1.64

8 months ago

2.1.61

8 months ago

2.1.62

8 months ago

2.1.60

8 months ago

2.1.78

6 months ago

2.1.79

6 months ago

2.1.76

6 months ago

2.1.77

6 months ago

2.1.74

6 months ago

2.1.75

6 months ago

2.1.72

7 months ago

2.1.73

6 months ago

2.1.70

7 months ago

2.1.71

7 months ago

2.1.56

10 months ago

2.1.55

10 months ago

2.1.54

11 months ago

2.1.52

1 year ago

2.1.53

12 months ago

2.1.49

1 year ago

2.1.47

1 year ago

2.1.48

1 year ago

2.1.46

1 year ago

2.1.50

1 year ago

2.1.51

1 year ago

2.1.45

1 year ago

2.1.44

1 year ago

2.1.43

1 year ago

2.1.42

1 year ago

2.1.40

1 year ago

2.1.38

1 year ago

2.1.39

1 year ago

2.1.36

1 year ago

2.1.37

1 year ago

2.1.35

1 year ago

2.1.27

2 years ago

2.1.28

1 year ago

2.1.25

2 years ago

2.1.26

2 years ago

2.1.29

1 year ago

2.1.34

1 year ago

2.1.32

1 year ago

2.1.33

1 year ago

2.1.30

1 year ago

2.1.31

1 year ago

2.1.23

2 years ago

2.1.24

2 years ago

2.1.22

2 years ago

2.1.16

2 years ago

2.1.17

2 years ago

2.1.15

2 years ago

2.1.18

2 years ago

2.1.19

2 years ago

2.1.21

2 years ago

2.1.20

2 years ago

2.1.14

2 years ago

2.1.12

2 years ago

2.1.13

2 years ago

2.1.10

2 years ago

2.1.11

2 years ago

2.1.9

2 years ago

2.1.8

2 years ago

2.0.9

2 years ago

2.1.2

2 years ago

2.1.1

2 years ago

2.1.4

2 years ago

2.1.3

2 years ago

2.1.6

2 years ago

2.1.5

2 years ago

2.1.7

2 years ago

2.1.0

2 years ago

2.0.7

2 years ago

2.0.8

2 years ago

2.0.4

2 years ago

2.0.6

2 years ago

2.0.3

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

0.2.92

3 years ago

0.2.91

3 years ago

0.2.90

3 years ago

0.2.89

3 years ago

0.2.88

3 years ago

0.2.87

3 years ago

0.2.86

3 years ago

0.2.85

3 years ago

0.2.84

3 years ago

0.2.83

3 years ago

0.2.82

3 years ago

0.2.81

3 years ago

0.2.80

3 years ago

0.2.79

3 years ago

0.2.78

3 years ago

0.2.77

3 years ago

0.2.73

3 years ago

0.2.72

3 years ago

0.2.71

3 years ago

0.2.70

3 years ago

0.2.69

3 years ago

0.2.68

3 years ago

0.2.67

3 years ago

0.2.66

3 years ago

0.2.65

3 years ago

0.2.64

3 years ago

0.2.63

3 years ago

0.2.62

4 years ago

0.2.61

4 years ago

0.2.60

4 years ago

0.2.59

4 years ago

0.2.58

4 years ago

0.2.57

4 years ago

0.2.56

4 years ago

0.2.55

4 years ago

0.2.54

4 years ago

0.2.53

4 years ago

0.2.52

4 years ago

0.2.51

4 years ago

0.2.50

4 years ago

0.2.49

4 years ago

0.2.48

4 years ago

0.2.47

4 years ago

0.2.46

4 years ago

0.2.45

4 years ago

0.2.44

4 years ago

0.2.43

4 years ago

0.2.42

4 years ago

0.2.41

4 years ago

0.2.40

4 years ago

0.2.39

4 years ago

0.2.38

4 years ago

0.2.37

4 years ago

0.2.36

4 years ago

0.2.35

4 years ago

0.2.34

4 years ago

0.2.33

4 years ago

0.2.32

4 years ago

0.2.31

4 years ago

0.2.30

4 years ago

0.2.27

4 years ago

0.2.26

4 years ago

0.2.25

4 years ago

0.2.24

4 years ago

0.2.23

5 years ago

0.2.22

5 years ago

0.2.21

5 years ago

0.2.20

5 years ago

0.2.19

5 years ago

0.2.18

5 years ago

0.2.17

5 years ago

0.2.16

5 years ago

0.2.15

5 years ago

0.2.14

5 years ago

0.2.13

5 years ago

0.2.12

5 years ago

0.2.11

5 years ago

0.2.10

5 years ago

0.2.9

5 years ago

0.2.8

5 years ago

0.2.7

5 years ago

0.2.6

5 years ago

0.2.5

5 years ago

0.2.4

5 years ago

0.2.3

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.0

5 years ago