代码
public function query(queryArea:Rectangle):Array
{
var result:Array = [];
for each (var item:IQuadNodeItem in contents)
{
if (queryArea.intersects(item.getRectangle()))
result.push(item);
}
for each (var node:QuadNode in nodes)
{
if (node.isEmpty)
continue;
if (node.bounds.containsRect(queryArea))
{
result = result.concat(node.query(queryArea));
break;
}
if (queryArea.containsRect(node.bounds))
{
result = result.concat(node.subTreeContents);
continue;
}
if (node.bounds.intersects(queryArea))
result = result.concat(node.query(queryArea));
}
return result;
}
public function insert(item:IQuadNodeItem):void
{
if (!bounds.containsRect(item.getRectangle()))
return;
if (nodes.length == 0)
createSubNodes();
for each (var node:QuadNode in nodes)
{
if (node.bounds.containsRect(item.getRectangle()))
{
node.insert(item);
return;
}
}
this.contents.push(item);
}
public function query(queryArea:Rectangle):Array
{
var result:Array = [];
for each (var item:IQuadNodeItem in contents)
{
if (queryArea.intersects(item.getRectangle()))
result.push(item);
}
for each (var node:QuadNode in nodes)
{
if (node.isEmpty)
continue;
if (node.bounds.containsRect(queryArea))
{
result = result.concat(node.query(queryArea));
break;
}
if (queryArea.containsRect(node.bounds))
{
result = result.concat(node.subTreeContents);
continue;
}
if (node.bounds.intersects(queryArea))
result = result.concat(node.query(queryArea));
}
return result;
}
public function insert(item:IQuadNodeItem):void
{
if (!bounds.containsRect(item.getRectangle()))
return;
if (nodes.length == 0)
createSubNodes();
for each (var node:QuadNode in nodes)
{
if (node.bounds.containsRect(item.getRectangle()))
{
node.insert(item);
return;
}
}
this.contents.push(item);
}