Last active
August 29, 2015 14:10
-
-
Save DataKinds/dba12ce7620ec9b578ea to your computer and use it in GitHub Desktop.
L-System stuff
This file contains hidden or 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
import turtle, random | |
from lsystemconfig import * | |
stack = [] | |
def iterateLSystem(system, rule): | |
mutatedLSystem = "" | |
for char in system: | |
charAccountedFor = False | |
for oneRule in rule: | |
if oneRule[0] == char: | |
mutatedLSystem += oneRule[1] | |
charAccountedFor = True | |
if not charAccountedFor: | |
mutatedLSystem += char | |
return mutatedLSystem | |
def moveTurtle(instruction): | |
if instruction == '+': | |
turtle.left(ANGLE) | |
elif instruction == '-': | |
turtle.right(ANGLE) | |
elif instruction == '[': | |
stack.append((turtle.position(), turtle.heading())) | |
elif instruction == ']': | |
turtle.penup() | |
turtleInfo = stack.pop() | |
turtle.goto(turtleInfo[0]) | |
turtle.setheading(turtleInfo[1]) | |
turtle.pendown() | |
elif instruction == '?': | |
turtle.setheading(random.randint(0, 360) * ANGLE) | |
elif instruction == '!': | |
if random.choice([True, False]): | |
turtle.left(ANGLE) | |
else: | |
turtle.right(ANGLE) | |
elif (not (instruction in CONSTANTS)): | |
turtle.forward(LENGTH) | |
def saveImage(name): | |
ts = turtle.getscreen() | |
ts.getcanvas().postscript(file = name + ".eps") | |
if __name__ == "__main__": | |
turtle.speed(0) | |
turtle.tracer(10000, 0) | |
#for ang in range(360): | |
turtle.hideturtle() | |
#ANGLE = ang + 1 | |
iterations = [AXIOM] | |
for i in range(MAX_ITERATIONS): | |
iterations.append(iterateLSystem(iterations[-1], RULE)) #apply the function to the last element of the list and repeat | |
print iterations[-1] | |
turtle.penup() | |
turtle.screensize(5000, 5000) | |
turtle.setup(width = 1600, height = 900, startx = 0, starty = 0) | |
#turtle.goto((turtle.screensize()[0])/-2, (turtle.screensize()[1])/-2) | |
turtle.pendown() | |
for instruction in iterations[-1]: #actually draw it now | |
moveTurtle(instruction) | |
turtle.done() | |
#saveImage(str(ANGLE)) | |
#turtle.reset() |
This file contains hidden or 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
MAX_ITERATIONS = 5 | |
AXIOM = "F" | |
CONSTANTS = "" | |
RULE = [('F', "FF-[-F+F+F]+[+F-F-F]")] | |
ANGLE = 22 | |
LENGTH = 5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment