Algorithm-Shape-RandomTree
view release on metacpan or search on metacpan
lib/Algorithm/Shape/RandomTree.pm view on Meta::CPAN
my $stem = $self->create_stem;
$branch_num = $self->complexity;
# Create primary branches
foreach my $branch ( 1 .. $branch_num ) {
$self->create_branch( $stem, $level );
}
} else {
# Get the current level's parent branches
# ( i.e. the previous level's branches )
my @parent_branches = $self->filter_branches(
sub { $_->level = ( $level - 1 ) }
);
foreach my $parent ( @parent_branches ) {
# Number of sub branches
my $sub_branches = int( rand( $self->complexity ) );
# Create sub-branches for the current parent branch
foreach my $idx ( 1 .. $sub_branches ) {
$self->create_branch( $parent, $level );
}
}
}
}
# Create Stem: creates the primary branch (stem) for in both recursive and
# linear tree creating algorithms
sub create_stem {
my $self = shift;
lib/Algorithm/Shape/RandomTree.pm view on Meta::CPAN
);
# Add stem to branches collection
$self->add_branch( $stem );
return $stem;
}
# Linear algorithm's branch creation sub
sub create_branch {
my ( $self, $parent, $level ) = @_;
my $start_point = $parent->end_point;
my $verb = $self->verbose;
my ( $dx, $dy ) = $self->calc_new_deltas( $parent );
my ( $x_end, $y_end ) = $self->calc_new_endpoints(
$start_point, $dx, $dy
);
my $end_point = Algorithm::Shape::RandomTree::Branch::Point->new(
x => $x_end, y => $y_end
);
my $number = $self->count_branches + 1; # New branch's num (name)
my $newbranch = Algorithm::Shape::RandomTree::Branch->new(
name => $number,
start_point => $start_point,
end_point => $end_point,
dx => $dx,
dy => $dy,
level => $level,
parent => $parent,
# nodulation => ,
# complexity => ,
);
$self->add_branch( $newbranch );
}
# Calculate New Deltas: uses the parent branch's attributes and random factors
# to modify a new branche's dx and dy values, who determin the angle and length
# of the new branch.
sub calc_new_deltas {
my ( $self, $parent ) = @_;
my $verb = $self->verbose;
# Get parent branch's deltas
my $old_dx = $parent->dx;
my $old_dy = $parent->dy;
# Calculate modifiers:
# These slightly change the dx and dy to create variation and randomness
# in branches lengths and angles.
# Modifiers range from -range_value to +range_value
my $dx_modifier = (
int( rand( $self->dx_range ) * -1 ) +
int( rand( $self->dx_range ) )
);
my $dy_modifier = (
int( rand( $self->dy_range ) * -1 ) +
int( rand( $self->dy_range ) )
);
# If the level is 0, it's the stem's children, so the falloff should be 1.5
# (so that they would still be a bit shorter than the stem).
# otherwise, it should be the level + 1
my $falloff = ( $parent->level == 0 ) ? 1.5 : $parent->level + 1;
# Apply modifiers
my $new_dx = int ( ( $old_dx + $dx_modifier ) / $falloff );
my $new_dy = int ( ( $old_dy + $dy_modifier ) / $falloff );
return( $new_dx, $new_dy );
}
# Calculate New End-points: ( by adding the deltas to the start-points )
sub calc_new_endpoints {
my ( $self, $start_point, $dx, $dy ) = @_;
my $x_end = $dx + $start_point->x;
my $y_end = $dy + $start_point->y;
return( $x_end, $y_end );
}
# The recursive algorithm for creating all non-stem branches
sub create_branches_recursive {
my ( $self, $parent ) = @_;
my $verb = $self->verbose;
my $name = $parent->name;
$verb && print "[create_branches_recursive] on parent: $name\n";
# Create a new branch connected to parent
my $branch = $self->make_branch( $parent );
# Create this branche's sub-branches
if ( $branch->nodulation ) {
foreach my $idx ( 1 .. $branch->complexity ) {
$verb && print qq{
[create_branches_recursive] \tcreating $name 's branches\n
};
$self->create_branches_recursive( $branch );
}
}
}
# Sub for creating single branches used by the recursive algorithm
sub make_branch {
my ( $self, $parent ) = @_;
my $start_point = $parent->end_point;
my $verb = $self->verbose;
my $name = $parent->name;
$verb && print "[make_branche] on parent: $name\n";
my ( $dx, $dy ) = $self->calc_new_deltas( $parent );
my ( $x_end, $y_end ) = $self->calc_new_endpoints(
$start_point, $dx, $dy
);
my $end_point = Algorithm::Shape::RandomTree::Branch::Point->new(
x => $x_end, y => $y_end
);
my $number = $self->count_branches + 1; # New branch's num (name)
my $nodulation = $self->calc_new_nodulation( $parent );
my $complexity = int( rand( $self->complexity ) ); # Calculate new complexity
# Calculate new width, and prevent a less than 1 width
my $falloff = ( $parent->level == 0 ) ? 1.5 : $parent->level + 1;
my $new_width = int ( $self->tree_width / $falloff );
my $width = $new_width ? $new_width : 1;
my $path_str = $self->create_path( $start_point, $end_point, $dx, $dy );
my $newbranch = Algorithm::Shape::RandomTree::Branch->new(
name => $number,
start_point => $start_point,
end_point => $end_point,
dx => $dx,
dy => $dy,
level => $parent->level + 1,
parent => $parent,
nodulation => $nodulation,
complexity => $complexity,
width => $width,
path_string => $path_str,
);
$verb && print "[make_branche] \tmaking branch $number\n";
$self->add_branch( $newbranch );
return $newbranch;
}
sub calc_new_nodulation {
my ( $self, $parent ) = @_;
my $verb = $self->verbose;
my $old = $parent->nodulation;
# Reduce ebbing factor from the parent's nodulation
my $new = $old - $self->ebbing_factor;
return $new;
}
sub create_path {
my ( $self, $start, $end, $dx, $dy ) = @_;
my $x1 = $start->x;
my $y1 = $start->y;
lib/Algorithm/Shape/RandomTree/Branch.pm view on Meta::CPAN
# Deltas: the difference between start and end x and y coordinates
# reflecting the slope of the branch
has [ 'dx', 'dy' ] => ( is => 'ro', isa => 'Int' );
# Level in which this branch stands in a linearly created tree
has 'level' => ( is => 'ro', isa => 'Int' );
# Line thickness
has 'width' => ( is => 'ro', isa => 'Int' );
# Contains a reference to the parent branch
has 'parent' => ( is => 'ro', isa => 'Ref' );
# Nodulation: is the attribute that determins whether this branch will
# continue to create sub-branches
# Complexity: is the number of sub-branches this branch has if nodulation
# is > 0 (otherwise, no new branches will be created on this
# branch, even if it's complexity is > 0
has [ 'nodulation', 'complexity' ] => ( is => 'ro', isa => 'Int' );
# The SVG string representaiton of the params required to create a curved path,
# which will represent the branch's geometry
lib/Algorithm/Shape/RandomTree/Branch.pm view on Meta::CPAN
not really useful on it's own.
=head1 VERSION
Version 0.01
=head1 SYNOPSIS
use Algorithm::Shape::RandomTree::Branch;
my $parent = $tree->branches('b1'); # The parent of the next branch
my $branch = Algorithm::Shape::RandomTree->new(
name => 'b2',
start_point => $parent->end_point,
end_point => $end_point, # Obj: Algorithm::Shape::RandomTree::Branch::Point
dy => 5,
dx => 5,
level => ( $parent->level + 1 ),
width => ( $parent->width - 1 ),
nodulation => ( $parent->nodulation - 1),
complexity => ( $parent->complexity),
path_string => "M $x1 $y1 C 3 3 2 2 $x2 $y2"; # SVG path string
);
=head1 Attributes
=head2 name
=head2 start_point
A Algorithm::Shape::RandomTree::Branch::Point object
( run in 0.257 second using v1.01-cache-2.11-cpan-4d50c553e7e )