Last active
December 25, 2015 04:39
-
-
Save Kobold/8991c008c323ba3d1644 to your computer and use it in GitHub Desktop.
Nate ♥s L-systems
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LSystem(object): | |
def __init__(self, axiom, rules): | |
self.axiom = axiom | |
self.rules = rules | |
self.string = self.axiom | |
self.generation = 0 | |
def reset(self): | |
self.string = self.axiom | |
self.generation = 0 | |
def print_system(self): | |
print u"n = {} : '{}'".format(self.generation, self.string) | |
def step(self): | |
self.string = ''.join([self.rules[char] for char in self.string]) | |
self.generation += 1 | |
ALGEA_AXIOM = 'A' | |
ALGAE_RULE = { | |
'A': 'AB', | |
'B': 'A', | |
} | |
system = LSystem(ALGEA_AXIOM, ALGAE_RULE) | |
for _ in xrange(10): | |
system.print_system() | |
system.step() | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# | |
# Turtle documentation resources: | |
# * Reference: https://docs.python.org/2/library/turtle.html | |
# * Examples: https://michael0x2a.com/blog/turtle-examples | |
# | |
import turtle | |
class LSystem(object): | |
def __init__(self, axiom, rules): | |
self.axiom = axiom | |
self.rules = rules | |
self.string = self.axiom | |
self.generation = 0 | |
def reset(self): | |
self.string = self.axiom | |
self.generation = 0 | |
def step(self): | |
self.string = ''.join([self.rules.get(char, char) for char in self.string]) | |
self.generation += 1 | |
def print_system(self): | |
print u"n = {} : '{}'".format(self.generation, self.string) | |
def interpret_koch_curve(string): | |
"""Turns an l-system string into a turtle drawing.""" | |
t = turtle.Turtle() | |
t.speed(10) | |
t.screen.setworldcoordinates(-10, -10, 1000, 1000) | |
# F means "draw forward", + means "turn left 90°", and − means "turn right 90°" | |
for char in string: | |
if char == 'F': | |
t.forward(20) | |
elif char == '+': | |
t.left(90) | |
elif char == '-': | |
t.right(90) | |
turtle.done() | |
KOCH_AXIOM = 'F' | |
KOCH_RULES = { | |
'F': 'F+F-F-F+F' | |
} | |
koch = LSystem(KOCH_AXIOM, KOCH_RULES) | |
koch.print_system() | |
for _ in xrange(3): | |
koch.step() | |
koch.print_system() | |
interpret_koch_curve(koch.string) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import turtle | |
class LSystem(object): | |
def __init__(self, axiom, rules): | |
self.axiom = axiom | |
self.rules = rules | |
self.string = self.axiom | |
self.generation = 0 | |
def reset(self): | |
self.string = self.axiom | |
self.generation = 0 | |
def step(self): | |
self.string = ''.join([self.rules.get(char, char) for char in self.string]) | |
self.generation += 1 | |
def print_system(self): | |
print u"n = {} : '{}'".format(self.generation, self.string) | |
FRACTAL_AXIOM = 'X' | |
FRACTAL_RULES = { | |
'X': 'F-[[X]+X]+F[+FX]-X', | |
'F': 'FF', | |
} | |
def interpret_fractal_system(string): | |
"""Turns an l-system string into a fractal drawing.""" | |
# * F means "draw forward" | |
# * − means "turn left 25°" | |
# * + means "turn right 25°". | |
# * X does not correspond to any drawing action and is used to | |
# control the evolution of the curve. | |
# * [ corresponds to saving the current values for position and angle, | |
# which are restored when the corresponding ] is executed. | |
t = turtle.Turtle() | |
t.speed(10) | |
t.screen.setworldcoordinates(-10, -1000, 1000, 1000) | |
# The list of saved positions and angles, saved by the '[' command. | |
stack = [] | |
for char in string: | |
if char == 'F': | |
t.forward(20) | |
elif char == '+': | |
t.right(25) | |
elif char == '-': | |
t.left(25) | |
elif char == '[': | |
state = (t.xcor(), t.ycor(), t.heading()) | |
stack.append(state) | |
elif char == ']': | |
x_coord, y_coord, heading = stack.pop() | |
# Stop drawing, move the pen, start drawing again. | |
t.penup() | |
t.setx(x_coord) | |
t.sety(y_coord) | |
t.setheading(heading) | |
t.pendown() | |
else: | |
pass | |
turtle.done() | |
fractal = LSystem(FRACTAL_AXIOM, FRACTAL_RULES) | |
fractal.print_system() | |
for _ in xrange(4): | |
fractal.step() | |
fractal.print_system() | |
interpret_fractal_system(fractal.string) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment