Python Forum
Koch fractal with iteration
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Koch fractal with iteration
#1
I tried this code for generating Koch's curve:
from turtle import *

def koch(length, depth):
   if depth == 0:
     forward(length)
   else:
     koch(length/3, depth-1)
     right(60)
     koch(length/3, depth-1)
     left(120)
     koch(length/3, depth-1)
     right(60)
     koch(length/3, depth-1)

koch(300, 2)
I'm having hard time to write an iterative version of it. Any hints/helps?
Reply
#2
The recursive version starts from the biggest curve, and then breaks each part of the curve into smaller ones. I would start at the bottom, with the smallest curve of just four lines. Calculate the length each line in that for that curve. Each iteration you draw that curve. The trick is to turn correctly at the end of each iteration, so that the next curve is drawn in the right direction.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
This is what I have:
def koch_i(ord):
    segments = nr_seg(ord)
    length = 400/segments
    turns = int(segments/3)
    for i in range(0, turns):
        forward(length)
        left(60)
        forward(length)
        right(120)
        forward(length)
        left(60)
        forward(length)
        if i % 2 == 0:
            left(60)
        else:
            right(120)
It draws order 1, 2, but then not anymore
Reply
#4
Like I said, the turns at the end of the loop are a trick. It's not a simple formula. Think about the turns in the base curve: L60, R120, L60. In The second level, the turns are L60, R120, L60, L60, L60, R120, L60, R120, L60, R120, L60, L60, L60, R120, L60. Now, if we set A to be L60, R120, L60, that's A, L60, A, R120, A, L60, A. Do you see the pattern? Note that you don't have to make a formula to figure out those turns. You could just build up a list of them iteratively.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
(Oct-06-2017, 02:32 PM)ichabod801 Wrote: Like I said, the turns at the end of the loop are a trick. It's not a simple formula. Think about the turns in the base curve: L60, R120, L60. In The second level, the turns are L60, R120, L60, L60, L60, R120, L60, R120, L60, R120, L60, L60, L60, R120, L60. Now, if we set A to be L60, R120, L60, that's A, L60, A, R120, A, L60, A. Do you see the pattern? Note that you don't have to make a formula to figure out those turns. You could just build up a list of them iteratively.

Yes, sure. Just I cannot use lists in this case.
Reply
#6
Look at the pattern. You're modulating by 3, but counting the turn at the end there are four turns: Three turns in the standard sequence, and then a turn in the meta sequence. Keep in mind that you need to look at powers of 4. At 4 you need to go up a level, which will be different. But at 16 you will need to go up two levels, which will be different than going up one level. and so on.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
(Oct-06-2017, 04:46 PM)ichabod801 Wrote: Look at the pattern. You're modulating by 3, but counting the turn at the end there are four turns: Three turns in the standard sequence, and then a turn in the meta sequence. Keep in mind that you need to look at powers of 4. At 4 you need to go up a level, which will be different. But at 16 you will need to go up two levels, which will be different than going up one level. and so on.
I see the pattern, but still cannot figure out the loop.
Reply
#8
Can you use strings?

path = 'f'
path = path.replace('f', 'flfrflf')
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020