views:

719

answers:

3

I am trying to work out how to draw the dragons curve, with pythons turtle using the An L-System or Lindenmayer system. I no the code is something like

the Dragon curve; initial state = ‘F’, replacement rule – replace ‘F’ with ‘F+F-F’, number of replacements = 8, length = 5, angle = 60

But have no idea how to put that into code.

+3  A: 

First hit on Google for "dragons curve python":

http://www.pynokio.org/dragon.py.htm

You can probably modify that to work with your plotting program of choice. I'd try matplotlib.

ramanujan
A: 

Well, presumably, you could start by defining:

def replace(s):
    return s.replace('F', 'F+F-F')

Then you can generate your sequence as:

code = 'F'
for i in range(8):
    code = replace(code)

I'm not familiar with turtle so I can't help you there.

John Fouhy
+1  A: 

Draw the dragon curve using turtle module (suggested by @John Fouhy):

#!/usr/bin/env python
import turtle
from functools import partial

nreplacements = 8
angle = 60
step = 5

# generate command
cmd = 'f'
for _ in range(nreplacements):
    cmd = cmd.replace('f', 'f+f-f')

# draw
t = turtle.Turtle()
i2c = {'f': partial(t.forward, step),
       '+': partial(t.left, angle),
       '-': partial(t.right, angle),
}
for c in cmd: i2c[c]()
J.F. Sebastian