1

I do not understand how the Koch Curve is drawn from using this function.

def koch(t, n):
    """Draws a koch curve with length n."""
    if n<3:
        fd(t, n)
        return
    m = n/3.0
    koch(t, m)
    lt(t, 60)
    koch(t, m)
    rt(t, 120)
    koch(t, m)
    lt(t, 60)
    koch(t, m)

The fd(t, n) command means object 't' will move forward by amount 'n'. The rt(t, 120) and lt(t, 60) commands means object 't' will turn right or left by the given angles.

So I gather that the author uses recursion in the function but I do not understand how it reiterates so many times with itself as I am a beginner and have very limited logic skills.

As an example say I called koch(t, 100) the if clause is by passed as n > 3 which leads to the next line of code which is m/3.0 so 100/3.0 is 33.3. This then leads to koch(t, 33.3) and as n > 3 still holds it reiterates again to produce koch(t, 11.1) and so forth until we reiterate it until we come to koch(t, 1.23).

Now as n = 1.23 and the if clause activates as soon as n < 3 we can run through the if conditionals block of code replacing all the koch(t, m) statements with fd(t, 1.23). As I see it fd(), lt(), fd(), rt(), fd, lt(), fd() should be activated only one time as n < 3 as soon as n = 1.23 or does it reiterate again with 1.23 / 3.0 and the code is ran again with koch(t, 0.41)? Maybe because an else clause does not exists to cancel the function, however the function does end and if I choose a higher value for n the koch curve is also larger making me more confused as there I can see no line in the code which tells me to reiterate this function n number of times.

I apologize for the lack of clarity as I do not understand how to explain this clearly.

firebird92
  • 119
  • 8
  • In "turtle graphics", which I imagine is in use here, when the "turtle" (no doubt `t` here) moves forward, it draws on the canvas in which it's moving (if its pen-holding "tail" is down, which no doubt is assumed to be the case here). Do you understand "turtle graphics" at all, e.g how four repetitions of "fd(t, 10); lt(t, 90)" would draw a square? – Alex Martelli Dec 25 '14 at 18:05
  • Sorry I've edited the comment hopefully it makes it more clear. I do not really understand how the reiteration part of the function works – firebird92 Dec 25 '14 at 21:54
  • What do you put for the "t" argument? – Vomer May 27 '20 at 13:05

2 Answers2

0

I think you may be looking at this from the wrong end to try to work it out. Consider first what happens if you call koch(t,1). The if statement evaluates to false, and you can see that something like this is drawn:

_/\_

Now what if you call koch(t,3)? Try on a piece of paper and you'll see that each of the straight lines in the picture above is replaced by similar shape...

xnx
  • 24,509
  • 11
  • 70
  • 109
  • Thanks your comment was quite helpful and was pointing towards the right direction at the time I was trying to figure this out. – firebird92 Dec 27 '14 at 00:58
0

I found out my problem after reading about recursion and testing some print statements in my console. What I did not understand was why choosing a larger n (length) produced a larger fractal. Basically because choosing a larger n produces more nodes (children) on a recursive tree so choosing a larger n will produce more children nodes and only the last nodes (null nodes) when n < 3 occurs will the turtle t begin to draw and by this time there will be many null nodes if n is large.

To understand recursion even further including how recursion works when there are two or more recursive functions in the block of code as posed by this question I have included a link to a helpful thread and hopes it helps anybody else who is stuck on this question and needs help in understanding recursion.

Understanding recursion

Community
  • 1
  • 1
firebird92
  • 119
  • 8