
288
|
第
10
章
示例应用,其中有一些移动的正方形在窗口中来回跳动,该应用能够识别这些正方
形的碰撞。具有交集的正方形进行了高亮显示。
四叉树小结
最好情况、平均情况和最坏情况:
O
(log
n
)
add (node, pt)
if node.region does not contain pt then
return false
if node is leaf node then
if node already contains pt then
❶
return false
if node has < 4 points then
❷
add pt to node
return true
q = quadrant for pt in node
if node is leaf node then
node.subdivide()
❸
return add(node.children[q], pt)
❹
range (node, rect, result)
if rect contains node.region then
❺
add (node, true) to result
else if node is leaf node then
foreach point p in node do
if rect contains p then
add (p, false) to result
❻
else
foreach ...