-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");
}
}
,這裏是輸出:
現在很明顯,一個點,是西南根節點的應該是圖中的是西南,但我似乎無法正確定位。
四叉樹和屏幕是否具有相同的座標系(上升或下降?) – AlexWien
@AlexWien每個點的座標系不代表它們在樹 –
是的,它應該,你的意思是屏幕coordina te不是樹中點的座標。 – AlexWien