0

I'm currently working with Myro/Calico with Robotics. I'm trying to run a recursive function of a fractal. I'm using Python.

I've been following pseudocode here. Fractal

So far I've tried to implement the first step without recursion. And it runs well

# 1 foot per 2 seconds.  x * 2 = feet desired.  
def fractal(x):
    waitTime = x*2
    turnRight(1, 0.825) #90 degree turn
    forward(1, x/3) #move length/3 steps
    turnLeft(1, 0.55)#60 degree turn
    forward(1, x/3) #move length/3 steps
    turnRight(1, 1.1) #120 degree turn
    forward(1, x/3) #move length/3 steps
    turnLeft(1, 0.55) #60 degree turn
    forward(1, x/3) #move length/3 steps

While this works, my goal is to go through this recursively, but make a smaller curve at each iteration. I attempted to do it, but my robot isn't moving as desired.

Here's my attempt at recursion

def fractal(x):
    waitTime = x*2
    if (x == 1):
        forward(x/3)
    else:
        (x-1)/3
    turnLeft(1,0.55) #60 degrees
    if (x == 1):
        forward(x/3)
    else:
        (x-1)/3
    turnRight(1, 1.1) #120 degree turn
    if (x == 1):
        forward(x/3)
    else:
        (x-1)/3
    turnLeft(1, 0.55)#60 degree turn
    if (x == 1):
        forward(x/3)
    else:
        (x-1)/3

My robot is only turning left and right, but it's not making the full shape. The one without the recursion started the fractal. I just need recursion to go through the whole fractal.

Mark Amery
  • 143,130
  • 81
  • 406
  • 459
user3577397
  • 453
  • 3
  • 12
  • 27
  • How are you calling fractal? And from what I see there is no recursion here. – user2097159 Nov 14 '14 at 17:51
  • I'm putting fractal(3) to specify the feet I want to go. You can put any number in. I tried following the pseudo on the site I linked. How would I make that recursive? – user3577397 Nov 14 '14 at 17:54
  • Just so you know, `else: (x-1)/3` doesn't do anything. Did you mean to do `forward((x-1)/3)` or `x = (x-1)/3` or `fractal((x-1)/3)` or something? – Kevin Nov 14 '14 at 17:55
  • Ohh.. thanks kevin. I meant whatever variable the user puts in , say 5, would become (5-1)/3. That's not happening? I guess I should do x = (x-1)/3? – user3577397 Nov 14 '14 at 17:57
  • I don't believe that is what you want I'm looking at the pseudocode now and you should be calling fractal inside the else but fractal should take 2 inputs. 1 for the length the other for the number of iterations – user2097159 Nov 14 '14 at 17:59
  • 1
    I'm not sure, but I think you need to have two variables to `fractal` instead of just one. You can't easily represent the fractal depth _and_ the width of the curve with just one variable. What if I want a five iteration Koch curve that's 100 pixels wide? What would the call to `fractal` look like, and how would it differ if I wanted a seven iteration curve that's 100 pixels wide? – Kevin Nov 14 '14 at 17:59
  • Oh... I think I know what you're saying. do a whole new fractal(x) inside each else. So one for length, the other for variations.. My robot moves at 2 feet per one second. I can't really specify length.. it's not built in. But I guess I can create some type of length. If I want a length of 5 feet, I would put waitTime*2 in the parameter? That would make it go 10 feet I think. – user3577397 Nov 14 '14 at 18:02

1 Answers1

1

I'm thinking this is what you want to do

x = number of interations
l = lenth(wait time)
def fractal(x, l):
    if (x == 1):
        forward(l/3)
    else:
        fractal((x-1), l/3)
    turnLeft(1,0.55) #60 degrees
    if (x == 1):
        forward(l/3)
    else:
        fractal((x-1), l/3)
    turnRight(1, 1.1) #120 degree turn
    if (x == 1):
        forward(l/3)
    else:
        fractal((x-1), l/3)
    turnLeft(1, 0.55)#60 degree turn
    if (x == 1):
        forward(l/3)
    else:
        fractal((x-1), l/3)
user2097159
  • 862
  • 5
  • 13
  • 1
    No problem recursion is a hard problem to wrap your head around at first. If you're interested in recursion and programming one of the easiest to start with is Fibonacci. – user2097159 Nov 14 '14 at 18:15