Git Product home page Git Product logo

lsys-calc's Introduction

How L-systems work

If you haven't played around with L-systems much, then check out Wikipedia or a beautiful visualizer.

The basic idea is to generate a seed string, like 'AB', which will then be replaced by another string, according to rules you define. You could make a set of rules like {A->AB, B->C}, which would turn AB into ABC. You can perform string substitution again and again to generate ever longer strings.

Once you've got a nice long string, you can associate a turtle graphic command with each character and plot your string. They tend to form complex, self-similar shapes like fractals.

Instructions

To begin generating your own L-systems, you'll need just a few commands:

params = {}                  # define system params
sys = new Lsys(params)       # create new system (params argument is optional)
sys.setParams(params)        # update system with params
drawPath = sys.getPath() # get data for plotting
plot(drawPath.x,drawPath.y)  # plot the system    

The params object should look something more like this:

params = {
    seed: "A",
    rules: {
        "A": "F[+F]F",
        "B": "ABF-F",
        "C": "CABF[+F>+F]F"
    },
    n: 1,
    pose: {
        x: 0,
        y: 0,
        orientation: 0
    },
    size: {
        value: 10,
        change: 1.0
    },
    angle: {
        value: 10,
        change: 1
    }
}

As you can see, there 6 fields in the params object:

  1. seed string - starting string
  2. rules object - key is a rule name, value is a rule
  3. n integer - number of times to execute string replacement
  4. pose object - has x and y position properties along with orientation (in degrees)
  5. size object - value is distance between points, change is used to vary this distance with each iteration
  6. angle object - value is angle of turns in path, change is used to vary this angle with each iteration

The rules are the most complex and interesting part of the params object. You can create as many rules as you like, and you can reference one rule from another. If, for instance, you have a rule "A": "B", then every occurence of "A" in a string will be replaced with a "B".

Here's a list of the turtle graphics drawing commands you can include in your rules:

F : Draw a line along current orientation
+ : Increase the orientation angle (counterclockwise)
- : Decrease the orientation angle (clockwise)
| : Flip the orientation angle into opposite direction
[ : Save current position and orientation (can do this multiple times)
] : Return to last saved position and orientation (do this multiple time to go to older saves)
! : Negate angle.value (begin turning in opposite direction)
( : Scale angle.value by (1 - angle.change)
) : Scale angle.value by (1 + angle.change)
< : Scale size.value by (1 + size.change)
> : Scale size.value by (1 - size.change)

lsys-calc's People

Contributors

dela3499 avatar

Stargazers

Michael Anthony avatar Davo avatar

Watchers

James Cloos avatar Michael Anthony avatar  avatar

lsys-calc's Issues

Create Modules / Or remove closure wrappers

Classes aren't accessible to external programs right now. I need to allow a user to import the class. Alternatively, I could just eliminate the closures that wrap each file.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.