Algorithm-QuadTree
view release on metacpan or search on metacpan
lib/Algorithm/QuadTree.pm view on Meta::CPAN
Algorithm::QuadTree implements a quadtree algorithm (QTA) in pure Perl.
Essentially, a I<QTA> is used to access a particular area of a map very quickly.
This is especially useful in finding objects enclosed in a given region, or
in detecting intersection among objects. In fact, I wrote this module to rapidly
search through objects in a L<Tk::Canvas> widget, but have since used it in other
non-Tk programs successfully. It is a classic memory/speed trade-off.
Lots of information about QTAs can be found on the web. But, very briefly,
a quadtree is a hierarchical data model that recursively decomposes a map into
smaller regions. Each node in the tree has 4 children nodes, each of which
represents one quarter of the area that the parent represents. So, the root
node represents the complete map. This map is then split into 4 equal quarters,
each of which is represented by one child node. Each of these children is now
treated as a parent, and its area is recursively split up into 4 equal areas,
and so on up to a desired depth.
Here is a somewhat crude diagram:
------------------------------
|AAA|AAB| | |
|___AA__| AB | |
|AAC|AAD| | |
|___|___A_______| B |
| | | |
lib/Algorithm/QuadTree/PP.pm view on Meta::CPAN
_AQT_deinit
_AQT_addObject
_AQT_findObjects
_AQT_delete
_AQT_clear
);
# recursive method which adds levels to the quadtree
sub _addLevel
{
my ($self, $depth, $parent, @coords) = @_;
my $node = {
PARENT => $parent,
HAS_OBJECTS => 0,
AREA => \@coords,
};
weaken $node->{PARENT} if $parent;
if ($depth < $self->{DEPTH}) {
my ($xmin, $ymin, $xmax, $ymax) = @coords;
my $xmid = $xmin + ($xmax - $xmin) / 2;
my $ymid = $ymin + ($ymax - $ymin) / 2;
$depth += 1;
# segment in the following order:
# top left, top right, bottom left, bottom right
$node->{CHILDREN} = [
lib/Algorithm/QuadTree/PP.pm view on Meta::CPAN
}
sub _AQT_init
{
my $obj = shift;
$obj->{BACKREF} = {};
$obj->{ROOT} = _addLevel(
$obj,
1, #current depth
undef, # parent - none
$obj->{XMIN},
$obj->{YMIN},
$obj->{XMAX},
$obj->{YMAX},
);
}
sub _AQT_deinit
{
# do nothing in PP implementation
( run in 0.224 second using v1.01-cache-2.11-cpan-4d50c553e7e )