I’ve tried to implement the diamond-square algorithm in python, but I seem to be bogged down in the workings of the language… I have a ‘quad’ class that has 9 2D (custom) points inside it:
- top left
- top right
- top middle
- bottom left
- bottom right
- bottom middle
- left middle
- centre
- right middle
These 9 are held in a dictionary.
There are also 4 ‘child’ quads in a dictionary for every quad.
- top left
- top right
- bottom left
- bottom right
The problem I have is that when I create the child quads for a quad, they mess up the fields of the parent’s 2D points, and I have no idea why.
Source code (Python 3.2): http://pastebin.com/5Ywz4anY
I’m not a Python expert, but I believe you need to define the Points and Children variables inside the init method if you want them to be instance variables. As it stands now, I think all instances would share the same dictionaries.
See http://legacy.python.org/doc/essays/ppt/acm-ws/sld051.htm
Another example: Instance variables vs. class variables in Python