473,325 Members | 2,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

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<=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
2 4536
Heres a link to the koch island:

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

Dec 12 '05 #2
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Matthew Wilson | last post by:
Hi- I've been goofing off with some basic recursive landscape generating stuff, drawing the output to a tk window. You can run the program like so: from pts import * >>> draw(5) #will...
2
by: Greg | last post by:
The following article from microsoft describes a workaroind for a bug in hte beta version of VISUAL PYTHON DOT NET ...
31
by: CYBER | last post by:
Hello Is there any other way under python to create blocks ?? instead of def sth(x): return x
1
by: Erwin S. Andreasen | last post by:
I have a Python application server that manages several different data sets, permits various reports to be run on them and the data to be downloaded as tab-delimetered files, all via a web...
90
by: Ben Finney | last post by:
Howdy all, How can a (user-defined) class ensure that its instances are immutable, like an int or a tuple, without inheriting from those types? What caveats should be observed in making...
17
by: ToddLMorgan | last post by:
I'm just starting out with python, after having a long history with Java. I was wondering if there were any resources or tips from anyone out there in Python-land that can help me make the...
0
by: hide1713 | last post by:
Hi everyone We are making a disk less workstation environment which support python.We use PXE DHCP TFTP to boot up disk less system.There are three choices as i know to set up a python environment...
1
by: ideamonk | last post by:
Just wrote this program on weekend http://ideamonk.blogspot.com/2008/08/recursion-fractals.html please give me more suggestions and ideas about recursive designs and fractals. thanks
11
by: Rafe | last post by:
Hi, I'm working within an application (making a lot of wrappers), but the application is not case sensitive. For example, Typing obj.name, obj.Name, or even object.naMe is all fine (as far as...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.