-1

I am trying to visualise my QuadTree in Java but I can't seem to get the positioning right. Basically, I want to recursively subdivide the canvas into rectangles if a node exists in the tree for that area. Here is what I have currently:

private class QuadTreeDisplay extends Canvas {

    public void paint(Graphics g) {
        render(g);
    }
    private void render(Graphics g) {
        setBackground(Color.white);
        renderNode(g,points.root, 0, 0, getWidth(), getHeight(),"root");
        System.out.println("-----------");
    }
    /** Draw a node on the canvas as a circle at the center, with 4 rectangles around
     *  it subdividing the enclosing rectangle
     *  @param g graphics to draw to
     *  @param n QuadTreeNode to represent
     *  @param x top left of rectangle
     *  @param y top right of rectangle
     *  @param width width of rectangle
     *  @param height height of rectangle
     *  
     */
    private void renderNode(Graphics g, QuadTreeNode n,int x, int y, int width, int height, String co) {
        if(n==null)return;
        int w = width/2, h = height/2;
        g.setColor(Color.black);
        // Draw 4 rectangles per node
        System.out.printf("Rect at %d,%d for %d,%d %s\n",x,y,n.x,n.y,co);
        g.drawString(n.x+","+n.y+"("+x+","+y+") NE", x+2, y+8);
        g.drawString(n.x+","+n.y+"("+w+","+y+") NW", w+2, y+8);
        g.drawString(n.x+","+n.y+"("+x+","+h+") SE", x+1, h-2);

        g.drawRect(x,y,w,h); //NE
        g.drawRect(w,y,w,h); //NW
        g.drawRect(x,h,w,h); //SE
        g.drawRect(w,h,w,h); //SW

        g.setColor(Color.blue);
        g.drawString(n.x+","+n.y+"("+w+","+h+") SW", w+1, h-2);
        g.fillOval(w -2, h-2, 4, 4);

        renderNode(g, n.NE, x, y, w, h, "ne");
        renderNode(g, n.NW, w, y, w, h, "nw");  
        renderNode(g, n.SE, w, h, w, h, "se");
        renderNode(g, n.SW, x, h, w, h, "sw");

    }
}

And here is the output:

enter image description here

Now obviously, a point that is southwest of the root node should be southwest of it in the diagram but I can't seem to position it right.

Jonno_FTW
  • 8,601
  • 7
  • 58
  • 90
  • Do quadtree and screen have the same coordiante system (raising y values are up or low?) – AlexWien Jun 05 '13 at 16:59
  • @AlexWien the coordinate system for each point does not represent their position in the tree. – Jonno_FTW Jun 07 '13 at 05:48
  • Yes it should, you mean the screen coordinate is Not the coordinate of the Point in the tree. – AlexWien Jun 07 '13 at 09:37
  • Does your Quad tree work, beside drawing it? The subsivision code Looks wrong. Compare it with a working quadtree – AlexWien Jun 07 '13 at 09:41
  • I'm sure my quadtree works correctly. The code is here: https://github.com/JonnoFTW/DBSCAN-java/blob/master/src/dbscan_gui/QuadTree.java – Jonno_FTW Jun 08 '13 at 04:39
  • what is the differecne between n.x and x? should that not be identical? is that redundant. describe how your quad coord system is organized? where is left lower of the root quad? (SW corner). – AlexWien Jun 08 '13 at 09:58
  • post your quadnode definition. has it only x,y? or x,y,w,h? – AlexWien Jun 08 '13 at 09:59
  • It only stores x and y coordinates. There are no bounding rectangles. – Jonno_FTW Jun 09 '13 at 12:15
  • I repeat my question: was is the difference between n.x and x? this seems redundant or wrong. The code comment "y top right of rectangle" looks wrong. This should be more precise like x-coordinate of top left corner, and y coordinate of top left corner. This is all so confusing. – AlexWien Jun 09 '13 at 14:18
  • Is this a point quad tree? with n.x,n.y the point stored in the quad cell, or an object quad tree? with multiple objects in each quad cell? – AlexWien Jun 09 '13 at 14:20

1 Answers1

0

Your approach is not ideal.

For quad painting, you should iterate through the quadtree using a copy of your standard search function, inside that method you know the quad nodes rectangle. Then transform that rectangle to a screen rectangle.

drawing the quadtree often shows errors in the quad tree orde itself. (exchanging S with N, East with West, messing up the what you call NE;NW, etc.

The quadtree search function will still work even when you (consistently) exchanged S with W, etc.

I still do not understand why (w,y,w,y) should paint NW. I think this is wrong. if x,y denotes the SW corner of a quad node, then NW child should be: (x,y+w/2,w/2,h/2)

AlexWien
  • 28,470
  • 6
  • 53
  • 83