Skip to content

Commit

Permalink
Use MooseX::Getopts. Add 'init_args => undef'. More docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
davorg committed Dec 1, 2018
1 parent 8545da3 commit bd32bee
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
2 changes: 1 addition & 1 deletion bin/tree
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ use feature 'say';

use SVG::ChristmasTree;

my $tree = SVG::ChristmasTree->new;
my $tree = SVG::ChristmasTree->new_with_options;

say $tree->as_xml;
44 changes: 29 additions & 15 deletions lib/SVG/ChristmasTree.pm
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ Perl extension to draw Christmas trees with SVG
my $tree = SVG::ChristmasTree->new;
print $tree->as_xml;
# Or change things
my tree = SVG::ChristmasTree->new({
layers => 5,
leaf_colour => 'rgb(0,255,0)',
pot_colour => 'rgb(0,0,255)',
});
print $tree->as_xml;
=cut

package SVG::ChristmasTree;
Expand All @@ -25,6 +33,8 @@ use SVG;
use Math::Trig qw[deg2rad tan];
use POSIX 'round';

with 'MooseX::Getopt';

# Constants that we haven't made into attributes yet
use constant {
TREE_WIDTH => 600, # Width of the bottom tree layer
Expand All @@ -48,6 +58,7 @@ has height => (
isa => 'Int',
is => 'ro',
lazy_build => 1,
init_arg => undef,
);

# Height is calculated from all the other stuff
Expand Down Expand Up @@ -75,6 +86,7 @@ has triangle_heights => (
isa => 'ArrayRef',
is => 'ro',
lazy_build => 1,
init_arg => undef,
);

sub _build_triangle_heights {
Expand All @@ -83,14 +95,14 @@ sub _build_triangle_heights {
my @heights;
my $width = TREE_WIDTH;
for (1 .. $self->layers) {
push @heights, $self->triangle_height($width, TOP_ANGLE);
push @heights, $self->_triangle_height($width, TOP_ANGLE);
$width *= LAYER_SIZE_RATIO;
}

return \@heights;
}

sub triangle_height {
sub _triangle_height {
my $self = shift;
my ($base, $top_angle) = @_;

Expand All @@ -104,6 +116,7 @@ has svg => (
isa => 'SVG',
is => 'ro',
lazy_build => 1,
init_arg => undef,
);

sub _build_svg {
Expand Down Expand Up @@ -161,6 +174,7 @@ has triangles => (
isa => 'ArrayRef',
is => 'ro',
lazy_build => 1,
init_arg => undef,
);

sub _build_triangles {
Expand All @@ -171,7 +185,7 @@ sub _build_triangles {

my @triangles;
for (1 .. $self->layers) {
push @triangles, $self->triangle(TOP_ANGLE, $width, $tri_bottom);
push @triangles, $self->_triangle(TOP_ANGLE, $width, $tri_bottom);
$width *= LAYER_SIZE_RATIO;
$tri_bottom -= $triangles[-1]->{h} * LAYER_STACKING;
}
Expand All @@ -187,8 +201,8 @@ sub as_xml {

for (@{$self->triangles}) {
my $h = $self->triangle(TOP_ANGLE, $_->{w}, $_->{b});
$self->bauble($self->mid_y - ($_->{w}/2), $_->{b});
$self->bauble($self->mid_y + ($_->{w}/2), $_->{b});
$self->bauble($self->_mid_y - ($_->{w}/2), $_->{b});
$self->bauble($self->_mid_y + ($_->{w}/2), $_->{b});
$self->coloured_shape(
$_->{x}, $_->{y}, $self->leaf_colour,
);
Expand All @@ -203,10 +217,10 @@ sub pot {
my $pot_top = $self->height - $self->pot_height;

$self->coloured_shape(
[ $self->mid_y - (POT_BOT_WIDTH / 2),
$self->mid_y - (POT_TOP_WIDTH / 2),
$self->mid_y + (POT_TOP_WIDTH / 2),
$self->mid_y + (POT_BOT_WIDTH / 2) ],
[ $self->_mid_y - (POT_BOT_WIDTH / 2),
$self->_mid_y - (POT_TOP_WIDTH / 2),
$self->_mid_y + (POT_TOP_WIDTH / 2),
$self->_mid_y + (POT_BOT_WIDTH / 2) ],
[ $self->height, $pot_top, $pot_top, $self->height ],
$self->pot_colour,
);
Expand All @@ -219,14 +233,14 @@ sub trunk {
my $trunk_top = $trunk_bottom - $self->trunk_length;

$self->coloured_shape(
[ $self->mid_y - (TRUNK_WIDTH / 2), $self->mid_y - (TRUNK_WIDTH / 2),
$self->mid_y + (TRUNK_WIDTH / 2), $self->mid_y + (TRUNK_WIDTH / 2) ],
[ $self->_mid_y - (TRUNK_WIDTH / 2), $self->_mid_y - (TRUNK_WIDTH / 2),
$self->_mid_y + (TRUNK_WIDTH / 2), $self->_mid_y + (TRUNK_WIDTH / 2) ],
[ $trunk_bottom, $trunk_top, $trunk_top, $trunk_bottom ],
$self->trunk_colour,
);
}

sub triangle {
sub _triangle {
my $self = shift;
my ($top_angle, $base, $bottom) = @_;

Expand All @@ -237,7 +251,7 @@ sub triangle {
# If I remember my trig correctly...
my $height = ($base / 2) / tan($top_angle);

$x = [ $self->mid_y - ($base / 2), $self->mid_y, $self->mid_y + ($base / 2) ];
$x = [ $self->_mid_y - ($base / 2), $self->_mid_y, $self->_mid_y + ($base / 2) ];
$y = [ $bottom, $bottom - $height, $bottom ];

return {
Expand All @@ -264,13 +278,13 @@ sub bauble {
);
}

sub mid_y {
sub _mid_y {
my $self = shift;

return $self->width / 2;
}

sub coloured_shape {
sub _coloured_shape {
my $self = shift;
my ($x, $y, $colour) = @_;

Expand Down

0 comments on commit bd32bee

Please sign in to comment.