making fractals in python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • evil_daffid

    #1

    making fractals in python

    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<=small est):
    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<=small est):
    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_lengt h, 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


  • evil_daffid

    #2
    Re: making fractals in python

    Heres a link to the koch island:

    A Lindenmayer system, also known as an L-system, is a string rewriting system that can be used to generate fractals with dimension between 1 and 2. Several example fractals generated using Lindenmayer systems are illustrated above.


    Comment

    • Ernst Noch

      #3
      Re: making fractals in python

      evil_daffid wrote:[color=blue]
      > 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:
      >[/color]
      [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_lengt h = line_length
      self.angle = angle
      self.shrink_fac tor=shrink_fact or

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

      def iterate(self):
      self.lstring = self.lstring.re place(rule[0],rule[1])
      self.line_lengt h=self.line_len gth/self.shrink_fac tor

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

      if __name__=='__ma in__':
      start = 'F+F+F+F'
      rule = ('F','F+F-F-FF+F+F-F')
      f = fractal(start,r ule,50,90,2)
      recurse(f,10)

      Comment

      Working...