0

Why does my Dragon curve not look like a dragon curve?

Dragon Curve

Here is the implementation in python with order 10:

def setupForDragonCurve():
    turtle.hideturtle()
    turtle.tracer(1e3, 0)
    turtle.penup()
    turtle.goto(0, -turtle.window_height()/5)
    turtle.pendown()

def generateDragonCurve(n, result='[FX]'):
    for _ in range(n):
        result = result.replace('Y', 'FX-Y')
        result = result.replace('X', 'X+YF')
    return result

def drawDragonCurve(cmds, size):
    stack = []
    for cmd in cmds:
        if cmd=='F':
            turtle.forward(size)
        elif cmd=='-':
            turtle.left(90)
        elif cmd=='+':
            turtle.right(90)
        elif cmd=='X':
            pass
        elif cmd=='Y':
            pass
        elif cmd=='[':
            stack.append((turtle.position(), turtle.heading()))
        elif cmd==']':
            position, heading = stack.pop()
            turtle.penup()
            turtle.setposition(position)
            turtle.setheading(heading)
            turtle.pendown()
        else:
            raise ValueError('Unknown Cmd: {}'.format(ord(cmd)))
    turtle.update()

Here is what it's supposed to look like at order 10:

Dragon Curve

EDIT: Here is what I get with order 1, with a larger scaled curve:

Dragon Curve Order 1

unor
  • 92,415
  • 26
  • 211
  • 360
jsetting32
  • 1,632
  • 2
  • 20
  • 45
  • 2
    Have you tried a lower-order curve? It might be easier to see the problem that way. – Jesse Rusak May 08 '14 at 02:39
  • I meant, like, order 1. Reduce it to the simplest case where you see a problem. – Jesse Rusak May 08 '14 at 02:44
  • it seems that I may have implemented the drawing function wrong? I'm not sure, I can draw a fractal plant perfectly as well as a pentigree snowflake... but i just can't get this one :( – jsetting32 May 08 '14 at 02:44
  • I think i got it... I f'd up, I think Wiki was wrong with one of the rules.... Instead of 'X' -> 'X+YF+' i did 'X' -> 'X+YF' – jsetting32 May 08 '14 at 02:51
  • Nvm... it's still wrong... You think it's possibly something wrong with my rules? I'm stuck :/ There really isn't anything to check other than the rules right? – jsetting32 May 08 '14 at 02:53

1 Answers1

1

When you run

    result = result.replace('Y', 'FX-Y')
    result = result.replace('X', 'X+YF')

the second line replaces the Xs introduced by the first line. (Also, you're using the wrong replacement rules, but you seem to have noticed that already.)

You need to carry out these replacements in such a way that they don't interact with each other, probably by doing them in a combined step. One way to do this would be to use the more advanced substitution capabilities of the re module. Another would be to write your own replacement routine. You could also use a character other than X in the first replacement, so the second replacement doesn't pick it up, then replace that character with X in a third pass.

user2357112
  • 260,549
  • 28
  • 431
  • 505