lib/LaTeX/TikZ/Set/Op.pm
lib/LaTeX/TikZ/Set/Path.pm
lib/LaTeX/TikZ/Set/Point.pm
+lib/LaTeX/TikZ/Set/Polyline.pm
lib/LaTeX/TikZ/Set/Raw.pm
lib/LaTeX/TikZ/Set/Rectangle.pm
lib/LaTeX/TikZ/Set/Sequence.pm
require LaTeX::TikZ::Set::Point; # point
require LaTeX::TikZ::Set::Line; # line
+ require LaTeX::TikZ::Set::Polyline; # polyline, closed_polyline
require LaTeX::TikZ::Set::Rectangle; # rectangle
require LaTeX::TikZ::Set::Circle; # circle
=> from 'Any'
=> via { __PACKAGE__->new(point => $_) };
+coerce 'LaTeX::TikZ::Point::Autocoerce'
+ => from 'LaTeX::TikZ::Set::Point'
+ => via { $_->point };
+
sub path {
my ($set, $tikz) = @_;
--- /dev/null
+package LaTeX::TikZ::Set::Polyline;
+
+use strict;
+use warnings;
+
+=head1 NAME
+
+LaTeX::TikZ::Set::Polyline - A set object representing a line.
+
+=head1 VERSION
+
+Version 0.01
+
+=cut
+
+our $VERSION = '0.01';
+
+use LaTeX::TikZ::Set::Point;
+
+use Any::Moose;
+use Any::Moose 'Util::TypeConstraints';
+
+with 'LaTeX::TikZ::Set::Op';
+
+subtype 'LaTeX::TikZ::Set::Polyline::Vertices'
+ => as 'ArrayRef[LaTeX::TikZ::Set::Point]'
+ => where { @$_ >= 2 }
+ => message { 'at least two LaTeX::TikZ::Set::Point objects are needed in order to build a polyline' };
+
+coerce 'LaTeX::TikZ::Set::Polyline::Vertices'
+ => from 'ArrayRef[Any]'
+ => via { [ map LaTeX::TikZ::Set::Point->new(point => $_), @$_ ] };
+
+has '_points' => (
+ is => 'ro',
+ isa => 'LaTeX::TikZ::Set::Polyline::Vertices',
+ init_arg => 'points',
+ required => 1,
+ coerce => 1,
+);
+
+sub points { @{$_[0]->_points} }
+
+has 'closed' => (
+ is => 'ro',
+ isa => 'Bool',
+ default => 0,
+);
+
+sub path {
+ my $set = shift;
+
+ join ' -- ', map($_->path(@_), $set->points),
+ ($set->closed ? 'cycle' : ());
+}
+
+use LaTeX::TikZ::Interface polyline => sub {
+ shift;
+
+ __PACKAGE__->new(points => \@_);
+};
+
+use LaTeX::TikZ::Interface closed_polyline => sub {
+ shift;
+
+ __PACKAGE__->new(points => \@_, closed => 1);
+};
+
+__PACKAGE__->meta->make_immutable;
+
+=head1 AUTHOR
+
+Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
+
+You can contact me by mail or on C<irc.perl.org> (vincent).
+
+=head1 BUGS
+
+Please report any bugs or feature requests to C<bug-latex-tikz at rt.cpan.org>, or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=LaTeX-TikZ>.
+I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
+
+=head1 SUPPORT
+
+You can find documentation for this module with the perldoc command.
+
+ perldoc LaTeX::TikZ
+
+=head1 COPYRIGHT & LICENSE
+
+Copyright 2010 Vincent Pit, all rights reserved.
+
+This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
+
+=cut
+
+1; # End of LaTeX::TikZ::Set::Polyline
use strict;
use warnings;
-use Test::More tests => 29;
+use Test::More tests => 30;
BEGIN {
use_ok( 'LaTeX::TikZ' );
use_ok( 'LaTeX::TikZ::Set::Op' );
use_ok( 'LaTeX::TikZ::Set::Path' );
use_ok( 'LaTeX::TikZ::Set::Point' );
+ use_ok( 'LaTeX::TikZ::Set::Polyline' );
use_ok( 'LaTeX::TikZ::Set::Raw' );
use_ok( 'LaTeX::TikZ::Set::Rectangle' );
use_ok( 'LaTeX::TikZ::Set::Sequence' );
use strict;
use warnings;
-use Test::More tests => 5 + 13 + 12;
+use Test::More tests => 5 + 15 + 12;
use LaTeX::TikZ;
my @methods = qw/
raw
path seq
- point line rectangle circle
+ point line polyline closed_polyline rectangle circle
raw_mod
clip layer
width color fill
use strict;
use warnings;
-use Test::More tests => 11 + 2 * 10;
+use Test::More tests => (11 + 2 * 5) + 2 * (10 + 2 * 3);
use Math::Complex;
is_deeply $body, $exp, $desc;
}
+sub failed_valid {
+ my ($tc) = @_;
+ qr/Validation failed for '\Q$tc\E'/;
+}
+
my $o = Tikz->point(0);
my $z = Tikz->point(1+2*i);
\draw (-1cm,2cm) -- (3cm,-4cm) ;
RES
+# Polyline
+
+my $w = Tikz->point(3, -4);
+
+for my $closed (0, 1) {
+ my $polyline = $closed ? 'closed_polyline' : 'polyline';
+ my $cycle = $closed ? '-- cycle ' : '';
+ my $desc = $closed ? 'closed polyline' : 'polyline';
+
+ my $pl = eval {
+ Tikz->$polyline($o, $z);
+ };
+ is $@, '', "creating a $desc from two Tikz points doesn't croak";
+
+ check $pl, "a $desc from two Tikz points", <<"RES";
+\\draw (0cm,0cm) -- (1cm,2cm) $cycle;
+RES
+
+ $pl = eval {
+ Tikz->$polyline($o, $z, $w);
+ };
+ is $@, '', "creating a $desc from three Tikz points doesn't croak";
+
+ check $pl, "a $desc from three Tikz points", <<"RES";
+\\draw (0cm,0cm) -- (1cm,2cm) -- (3cm,-4cm) $cycle;
+RES
+
+ $pl = eval {
+ Tikz->$polyline(-1, (2-3*i), [-4, 5]);
+ };
+ is $@, '', "creating a $desc from three Tikz points doesn't croak";
+
+ check $pl, "a $desc from three Tikz points", <<"RES";
+\\draw (-1cm,0cm) -- (2cm,-3cm) -- (-4cm,5cm) $cycle;
+RES
+
+ $pl = eval {
+ Tikz->$polyline($o);
+ };
+ like $@, qr/at least two LaTeX::TikZ::Set::Point objects are needed in order to build a polyline/, "creating a $desc from only one Tikz point croaks";
+
+ $pl = eval {
+ Tikz->$polyline(qw/foo bar/);
+ };
+ like $@, failed_valid('LaTeX::TikZ::Point::Autocoerce'), "creating a $desc from two string croaks";
+}
+
# Rectangle
my $r = eval {