398,028 Members | 2,371 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 398,028 IT Pros & Developers. It's quick & easy.

making fractals in python

evil_daffid
P: n/a
hi,
Im reseaching fractals, and how to make them in python using recursion.
I've written a bit of code to make the koch isalnd but something isn't
right, I have the basic shape but there's something wrong with the
recursion i've used, could someone help me.

Here is the code im using:

import turtle
turtle.clear

def koch_line(line_length, smallest):
third = line_length/3
if (third <= smallest):
turtle.forward(line_length)
else:
koch_line(third,smallest)
turtle.right(90)
if(third<=smallest):
turtle.forward(line_length/2)
else:
koch_line(line_length/2, smallest)
turtle.right(90)
if (third <= smallest):
turtle.forward(third)
else:
koch_line(line_length/2, smallest)
turtle.left(90)
if (third <= smallest):
turtle.forward(third)
else:
koch_line(line_length,smallest)
if (third <= smallest):
turtle.forward(line_length)
else:
koch_line(third,smallest)
turtle.right(90)
if(third<=smallest):
turtle.forward(line_length/2)
else:
koch_line(line_length/2, smallest)
turtle.right(90)
if (third <= smallest):
turtle.forward(third)
else:
koch_line(line_length/2, smallest)
turtle.left(90)
if (third <= smallest):
turtle.forward(third)
else:
koch_line(line_length,smallest)
return

def koch(line_length, smallest):
koch_line(line_length, smallest)
turtle.left(90)
koch_line(line_length, smallest)
turtle.left(90)
koch_line(line_length, smallest)
return

koch(500,10)

Thank you for your help
Dec 12 '05 #1
Share this Question
Share on Google+
2 Replies


evil_daffid
P: n/a
Heres a link to the koch island:

http://mathworld.wolfram.com/LindenmayerSystem.html

Dec 12 '05 #2

Ernst Noch
P: n/a
evil_daffid wrote:
hi,
Im reseaching fractals, and how to make them in python using recursion.
I've written a bit of code to make the koch isalnd but something isn't
right, I have the basic shape but there's something wrong with the
recursion i've used, could someone help me.

Here is the code im using:

[Code snipped]

Is this a homework assignment or something?

Anyway, this is a little bit more pythonic and flexible, though my
python has gotten a little bit rusty. It uses L-Systems for state storage.
Recursion is only left in because you asked for it.
Iterate() should get more sophisticated for more complex string
substitutions.

import turtle
turtle.clear

class fractal(object):

def __init__(self, lstring, rule, line_length, angle,
shrink_factor):
self.lstring = lstring
self.rule = rule
self.line_length = line_length
self.angle = angle
self.shrink_factor=shrink_factor

def draw(self):
drawingdict = {'F': lambda: turtle.forward(self.line_length),
'-':lambda: turtle.right(self.angle),
'+':lambda: turtle.left(self.angle),}
for rule in self.lstring:
drawingdict[rule]()

def iterate(self):
self.lstring = self.lstring.replace(rule[0],rule[1])
self.line_length=self.line_length/self.shrink_factor

def recurse(f,smallest):
if f.line_length>=smallest:
turtle.reset()
f.iterate()
f.draw()
recurse(f,smallest)

if __name__=='__main__':
start = 'F+F+F+F'
rule = ('F','F+F-F-FF+F+F-F')
f = fractal(start,rule,50,90,2)
recurse(f,10)
Dec 12 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.