Math-Fractal-Curve

 view release on metacpan or  search on metacpan

lib/Math/Fractal/Curve.pm  view on Meta::CPAN

Generator subroutines are passed the curve object as first argument. They
may access any attributes of the curve segment they are applied to, but
most interestingly, they may access their {start} and {end} attributes that
hold array references [x,y] of the start- and end points of the distance
they are being applied to.

=head2 EXPORT

None.

=head1 METHODS

=cut

package Math::Fractal::Curve;

use 5.006;
use strict;
use warnings;

use Carp;

our $VERSION = '1.03';


=head2 Constructor new

The new() constructor requires one named argument:

  generator => GENERATOR

where GENERATOR may either be a generator-datastructure as described
earlier or a subroutine reference (or closure) that returns such a
data structure.

Furthermore, new accepts any key/value pairs that will be made attributes
of the curve object.

new() is both a class- and an object method and thus can be used to clone
existing curves. (And is internally used to do so.)

=cut

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

	my $self = {};
	if (ref $proto) {
		$self->{generator} = $proto->{generator};
		if (exists $proto->{end} and exists $proto->{start}) {
			$self->{end} = [@{$proto->{end}}];
			$self->{start} = [@{$proto->{start}}];
		}
	}
	for (my $i = 0; $i < @_; $i+=2) {
		$self->{$_[$i]} = $_[$i+1];
	}

	delete $self->{_edges};
	bless $self => $class;

	if (not exists $self->{generator}) {
		croak "You need to supply a generator subroutine.";
	}
	
	return $self;
}



=head2 Method line

The line() method takes two required named arguments:

  start => [START_X, START_Y],
  end   => [END_X,   END_Y  ]

where START_X, START_Y and END_X, END_Y are the coordinates of the
start- and end points of the distance to create the fractal curve from.

line() stores this data in the {start} and {end} attributes of the
curve object.

=cut

sub line {
	my $self = shift;
	my %args = @_;
	my $start = $args{start};
	my $end = $args{end};
	
	if (not defined $start or not defined $end) {
		croak "You need to supply start- and end point.";
	}

	$self = $self->new(start => $start, end => $end);
	return $self;
}



=head2 Method recurse()

The recurse() method applies the generator to the curve's distance
and returns a reference to an array of new curve objects that represent
the newly generated edges.

=cut

sub recurse {
	my $self = shift;
	my $edges = $self->edges();

	my $obj = [];
	foreach my $e (@$edges) {
		push @$obj, $self->new(
			start => [$e->[0], $e->[1]],
			end   => [$e->[2], $e->[3]],
		);
	}

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

( run in 1.218 second using v1.00-cache-2.02-grep-82fe00e-cpan-1925d2aa809 )