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)
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to