Giter VIP home page Giter VIP logo

ulsys's Introduction

ulsys

"Micro" LSys. A small Python 3+ library for evaluating Lindenmayer-systems, with a small accompanying turtle implementation for pretty drawings.

ULSYS IS VERY MUCH PRE-ALPHA RIGHT NOW AND THE INTERFACE WILL LIKELY CHANGE A LOT.

Current status

  • Reasonable Turtle API
  • PyX Turtle Backend
  • PoC Simple L-System implementation
  • Package partitioned after L-System type
  • Stochastic L-System support
  • Interopability with Python stdlib turtle package

Goals

These are the desired goals with the library. ulsys is not an attempt to write the most performant, most feature complete L-System evaluator, at least for now. But it strives to be simple to use and good enough for most scenarios.

Primary goals

  • Very simple to use and get running.
  • Evaluating simple Lindenmayer systems (non-stochastic, non-parametric)
  • A small Turtle implementation that easily supports different backends
  • Easy to read code, to serve as an example implementation of an L-System evaluator.
  • At least one vector graphics Turtle backend

Secondary goals

  • Performance
  • Support for stochastic Lindenmayer systems

Examples

Koch Snowflake Vector Image

Use the included "Tri" Koch Snowflake and the PyX library turtle to create an SVG file:

import ulsys
from ulsys import standard
from ulsys import turtle

t = turtle.PyXTurtle()

syms = ulsys.triKochFlake(4)
actions = ulsys.triKochFlake.turtleActions

turtle.mapActions(syms, actions, t)

from pyx import canvas, path, style
c = canvas.canvas()
c.stroke(path.path(*t.paths), [style.linewidth(0.15)])

with open("test.svg", "wb") as f:
    c.writeSVGfile(f)

A stochastic L-System example, that generates a decently realistic plant branch.

import ulsys
from ulsys import stochastic
from ulsys import turtle

t = turtle.PyXTurtle()

syms = stochastic.evaluateSystem(
    "X",
    stochastic.production("X 0.25->F-[[X]+X]+F[+FX]-X", "F->FF", "X 0.25->F+[[XX]+X]-F[-FX]-X", "X 0.5->F[X[FX]]+F[[FX]X]"),
    6)
actions = ulsys.fractalPlant.turtleActions

turtle.mapActions(syms, actions, t)

from pyx import canvas, path, style
c = canvas.canvas()
c.stroke(path.path(*t.paths), [style.linewidth(0.15)])

with open("test.svg", "wb") as f:
    c.writeSVGfile(f)

Here's a more advanced example with a custom TurtleAction that calls circle which adds a circle of a given radius to the PyXTurtle.circle list whenever an "A" is encountered. It creates branches like above, but with the seed fixed over several generations to simulate growth. The circles ends up at certain leaf nodes.

Example 2 was produced in this fashion.

import ulsys
from ulsys import stochastic
from ulsys import turtle
from ulsys.turtle import TurtleAction
import math

import random

seed = random.random()

for n in range(1, 6):

    random.seed(seed)
        
    t = turtle.PyXTurtle()
    
    CircleAction = TurtleAction("circle", [0.15])
    
    syms = stochastic.evaluateSystem(
        "X",
        stochastic.production("X 0.25->F-[[X]+X]+F[+FX]-X", "F->FF", "A->F", "X 0.25->F+[[XX]+X]-F[-FX]-X", "X 0.5->F[X[FXA]]+F[[FX]X]"),
        n)
    turtleActions = {
        "F":TurtleAction.forward(),
        "-":TurtleAction.rotate(-math.pi / 9),
        "+":TurtleAction.rotate( math.pi / 9),
        "[":TurtleAction.push(),
        "]":TurtleAction.pop(),
        "A":CircleAction
    }
    
    turtle.mapActions(syms, turtleActions, t)
    
    from pyx import canvas, path, style, deco, color
    c = canvas.canvas()
    c.stroke(path.path(*t.paths), [style.linewidth(0.15)])
    for circle in t.circles:    
        c.fill(circle, attrs=[deco.filled([color.rgb.red])])
    
    with open("test{}.svg".format(n), "wb") as f:
        c.writeSVGfile(f)

Platform

Python 3+

License

MIT license.

ulsys's People

Contributors

skurmedel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

hartigel

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.