Algorithm-QuadTree

 view release on metacpan or  search on metacpan

QuadTree.pm  view on Meta::CPAN

package Algorithm::QuadTree;

use strict;
use Carp;

our $VERSION = 0.1;

1;

###############################
#
# sub new() - constructor
#
# Arguments are a hash:
#
# -xmin  => minimum x value
# -xmax  => maximum x value
# -ymin  => minimum y value
# -ymax  => maximum y value
# -depth => depth of tree
#
# Creating a new QuadTree objects automatically
# segments the given area into quadtrees of the
# specified depth.
#
###############################

sub new {
    my $self  = shift;
    my $class = ref($self) || $self;

    my $obj   = bless {} => $class;

    $obj->{BACKREF} = {};
    $obj->{OBJECTS} = [];
    $obj->{ORIGIN}  = [0, 0];
    $obj->{SCALE}   = 1;

    my %args  = @_;

    for my $arg (qw/xmin ymin xmax ymax depth/) {
	unless (exists $args{"-$arg"}) {
	    carp "- must specify $arg";
	    return undef;
	}

	$obj->{uc $arg} = $args{"-$arg"};
    }

    $obj->_segment;

    return $obj;
}

###############################
#
# sub _segment() - private method
#
# This method does the actual segmentation
# and stores everything internally.
#
###############################

sub _segment {
    my $obj = shift;

    $obj->_addLevel(
		    $obj->{XMIN},
		    $obj->{YMIN},
		    $obj->{XMAX},
		    $obj->{YMAX},
		    1,             # current depth
		    0,             # current index
		    undef,         # parent index
		    );

}

###############################
#
# sub _addLevel() - private method
#
# This method segments a given area
# and adds a level to the tree.
#
###############################

sub _addLevel {
    my ($obj,
	$xmin,
	$ymin,
	$xmax,

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 0.973 second using v1.00-cache-1.14-grep-28634ff-cpan-4d46879620f )