We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Re: QuadTree implementation, HELP
Page Index Toggle Pages: 1
Re: QuadTree implementation, HELP (Read 1455 times)
Re: QuadTree implementation, HELP
May 18th, 2009, 11:11am
 
I am guessing that you would like to display the values of every child of every parent. Huh

To do that, you might want to use recursion (unless you always know the size of your tree).

A quick search got me this:
http://en.wikipedia.org/wiki/Tree_traversal

While it is not a quad-tree, it shows the basic idea. In this case left and right would be say, child[0] and child[1].
Re: QuadTree implementation, HELP
Reply #1 - May 22nd, 2009, 12:56am
 
Hello check out code below. I try to fill nodes by count(); function and than display it but D.display() displays onyl "0"
Please help me fix this.

if You add println(t.value); statment in _create() function
it will display values from 0 to 20 (for img=4)

I know it needs a little modification to work but i can't find out.




tree D;

//PImage img;
int img = 4;

int a = 0;

void setup() {
 size(100,100);
 noLoop();
 
 //img = loadImage("monalisa.bmp"); // load image with 4x4 size
 //loadPixels();
 
}

void draw() {
 tree D = new tree();
 create();
 
 D.display();
}

class tree {
 public int x,y;
 public int value,cost;
 public int size;
 public tree[] child = new tree[4];
 public tree prnt = null;
 public boolean show = true;
     
//  public tree(int v) {
//    value = v;
//  }
 
 
 
 public void display() {
   println(value);
   
   if(child[0] != null && child[0].show == true) child[0].display();
   if(child[1] != null && child[1].show == true) child[1].display();
   if(child[2] != null && child[2].show == true) child[2].display();
   if(child[3] != null && child[3].show == true) child[3].display();
 }
 
 
   
}

 
 public tree create() {
   return _create(null, 0, 0, 0);
 }
 
 private tree _create(tree prnt, int x, int y, int level) {
   int size = img >> level;
   int v = count();
   
   tree t = new tree();
   t.prnt = prnt;
   t.x = x;
   t.y = y;
   t.size = size;
   
   t.value = v;
   //println(t.value);
             
   if(size > 1) {
     size = img >> ++level;

     t.child[0] = _create(t, x, y, level);
     t.child[1] = _create(t, x+size, y, level);
     t.child[2] = _create(t, x, y+size, level);
     t.child[3] = _create(t, x+size, y+size, level);
   }
 
  return t;
 }
 
public int count() {
 return a++;
}
Re: QuadTree implementation, HELP
Reply #2 - May 22nd, 2009, 1:51am
 
Code:

void draw() {
tree D = new tree();
create();

D.display();
}

...

public tree create() {
return _create(null, 0, 0, 0);
}


some OO confusion here. you are creating a new tree. and then calling create() which creates a new tree and return it. but you are ignoring the return value and calling display on the (empty) tree you created previously (D).

the create code really needs to go into the constructor of the tree class.

(also, class names are traditionally capitalised, variables usually aren't - Tree d = new Tree(); rather than tree D = new tree())
Re: QuadTree implementation, HELP
Reply #3 - May 22nd, 2009, 2:44am
 
I'm just started with object oriented programming. Spent couple of hours reading about quadtrees and looking for any code which help me write my own program.

Please write down correct version of this program.

If You have any idea how to modiffy this code above to make it work corectly. I will be thankfull :]


Re: QuadTree implementation, HELP
Reply #4 - May 22nd, 2009, 3:35am
 
i don't have the time right now.

look into classes and constructors and have another go.
Re: QuadTree implementation, HELP
Reply #5 - May 23rd, 2009, 11:21am
 
i haven't really changed much, just rearranged things and given the Tree a decent constructor. i'm also using the return value from createTree which, i think, was the thing that was wrong with the original.

i get 85 nodes with img = 8 which is
1 level 0 node (size 8)
4 level 1 nodes (size 4)
16 level 2 nodes (size 2)
64 level 3 nodes (size 1)
which looks about right. have also added a level member to the tree but only use it to indent the output.

Code:

Tree tree;

//PImage img;
int img = 8;

int count = 0;

void setup() {
 size(100,100);
 noLoop();

 //img = loadImage("monalisa.bmp"); // load image with 4x4 size
 //loadPixels();

 tree = createTree();
 tree.display();
 println("Count: " + count);
}

void draw() {
}

// create initial node
Tree createTree() {
 Tree t = createNode(null, 0, 0, 0);
 return t;
}

// create nodes (recursively)
Tree createNode(Tree parent, int x, int y, int level) {
 //println("createNode x[" + x + "] y[" + y + "] level[" + level + "]");
 Tree node = new Tree(parent, x, y, level);

 // add child nodes (if size > 1)
 int s = img >> level;
 if (s > 1) {
   s = img >> (level + 1);
   node.child[0] = createNode(node, x, y, level + 1);
   node.child[1] = createNode(node, x + s, y, level + 1);
   node.child[2] = createNode(node, x, y + s, level + 1);
   node.child[3] = createNode(node, x + s, y + s, level + 1);
 }
 return node;
}

// Tree Class
class Tree {
 public int x,y;
 public int value, cost, level;
 public int size;
 public Tree[] child = new Tree[4];
 public Tree parent = null;
 public boolean show = true;

 public Tree() {
   this(null, 0, 0, 0);
 }

 public Tree(Tree parent, int x, int y, int level) {
   //println("New x[" + x + "] y[" + y + "] level[" + level + "]");
   this.parent = parent;
   this.x = x;
   this.y = y;
   this.level = level;
   this.size = img >> level;
   this.value = count;  // NB global count
   count++;
 }

 public void display() {
   String padding = "                         ";
   print(padding.substring(1, (this.level * 2) + 1));
   println("Node[" + this.value + "]: x[" + this.x + "] y[" + this.y + "] size[" + this.size + "]");

   if (child[0] != null && child[0].show == true) {
     child[0].display();
   }
   if (child[1] != null && child[1].show == true) {
     child[1].display();
   }
   if (child[2] != null && child[2].show == true) {
     child[2].display();
   }
   if (child[3] != null && child[3].show == true) {
     child[3].display();
   }
 }
}
Page Index Toggle Pages: 1