2013-05-31 33 views
-1

我正在嘗試使用Java對QuadTree進行可視化,但似乎無法獲得定位權。基本上,如果節點存在於該區域的樹中,我想遞歸地將畫布細分爲矩形。下面是我目前:在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"); 

    } 
} 

,這裏是輸出:

enter image description here

現在很明顯,一個點,是西南根節點的應該是圖中的是西南,但我似乎無法正確定位。

+0

四叉樹和屏幕是否具有相同的座標系(上升或下降?) – AlexWien

+0

@AlexWien每個點的座標系不代表它們在樹 –

+0

是的,它應該,你的意思是屏幕coordina te不是樹中點的座標。 – AlexWien

回答

0

您的方法並不理想。

對於四畫,您應該使用標準搜索函數的副本遍歷四叉樹,在該方法中您知道四節點矩形。 然後將該矩形轉換爲屏幕矩形。

繪製四叉樹通常會在四叉樹或自身中顯示錯誤。 (以N與西方交換S,東,搞亂了你所謂的NE,NW等

四叉樹搜索功能仍然會當你(始終),利用交換的工作,甚至等

我仍然不明白爲什麼(w,y,w,y)應該畫NW,我認爲這是錯的 如果x,y表示四節點的SW角,那麼NW子應該是:(x,y + w/2,w/2,h/2)