0

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 1
    I'm inclined to call this a duplicate of [Python: Difference between class and instance attributes](http://stackoverflow.com/q/207000/395760) (I hastily voted so). Of course the question *asked* is different, but the underlying difference is exactly the same and is so commonplace... –  Dec 18 '11 at 07:18

2 Answers2

3

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

Community
  • 1
  • 1
DurhamG
  • 222
  • 2
  • 9
0

Stop using a dictionary. Define these points as members within your __init__ method:

self.ne = TR 

(etc)

Marcin
  • 48,559
  • 18
  • 128
  • 201