-1

我正在尝试用 Java 可视化我的 QuadTree,但我似乎无法正确定位。基本上,如果该区域的树中存在节点,我想递归地将画布细分为矩形。这是我目前拥有的:

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");

    }
}

这是输出:

在此处输入图像描述

现在显然,根节点西南的一个点应该在图中它的西南,但我似乎无法正确定位它。

4

1 回答 1

0

您的方法并不理想。

对于四边形绘制,您应该使用标准搜索函数的副本遍历四叉树,在该方法中您知道四边形节点矩形。然后将该矩形转换为屏幕矩形。

绘制四叉树通常会显示四叉树顺序本身的错误。(将 S 与 N 交换,将 East 与 West 交换,弄乱你所谓的 NE;NW 等。

即使您(始终)将 S 与 W 等交换,四叉树搜索功能仍然有效。

我仍然不明白为什么 (w,y,w,y) 应该画 NW。我认为这是错误的。如果 x,y 表示四边形节点的 SW 角,则 NW 子节点应为: (x,y+w/2,w/2,h/2)

于 2013-06-08T10:15:11.500 回答