]> git.vpit.fr Git - perl/modules/LaTeX-TikZ.git/blob - lib/LaTeX/TikZ/Set/Polyline.pm
Introduce LaTeX::TikZ::Functor
[perl/modules/LaTeX-TikZ.git] / lib / LaTeX / TikZ / Set / Polyline.pm
1 package LaTeX::TikZ::Set::Polyline;
2
3 use strict;
4 use warnings;
5
6 =head1 NAME
7
8 LaTeX::TikZ::Set::Polyline - A set object representing a line.
9
10 =head1 VERSION
11
12 Version 0.01
13
14 =cut
15
16 our $VERSION = '0.01';
17
18 use LaTeX::TikZ::Set::Point;
19
20 use LaTeX::TikZ::Functor;
21
22 use Any::Moose;
23 use Any::Moose 'Util::TypeConstraints';
24
25 with 'LaTeX::TikZ::Set::Op';
26
27 subtype 'LaTeX::TikZ::Set::Polyline::Vertices'
28      => as 'ArrayRef[LaTeX::TikZ::Set::Point]'
29      => where { @$_ >= 2 }
30      => message { 'at least two LaTeX::TikZ::Set::Point objects are needed in order to build a polyline' };
31
32 coerce 'LaTeX::TikZ::Set::Polyline::Vertices'
33     => from 'ArrayRef[Any]'
34     => via { [ map LaTeX::TikZ::Set::Point->new(point => $_), @$_ ] };
35
36 has '_points' => (
37  is       => 'ro',
38  isa      => 'LaTeX::TikZ::Set::Polyline::Vertices',
39  init_arg => 'points',
40  required => 1,
41  coerce   => 1,
42 );
43
44 sub points { @{$_[0]->_points} }
45
46 has 'closed' => (
47  is      => 'ro',
48  isa     => 'Bool',
49  default => 0,
50 );
51
52 sub path {
53  my $set = shift;
54
55  join ' -- ', map($_->path(@_), $set->points),
56               ($set->closed ? 'cycle' : ());
57 }
58
59 use LaTeX::TikZ::Interface polyline => sub {
60  shift;
61
62  __PACKAGE__->new(points => \@_);
63 };
64
65 use LaTeX::TikZ::Interface closed_polyline => sub {
66  shift;
67
68  __PACKAGE__->new(points => \@_, closed => 1);
69 };
70
71 LaTeX::TikZ::Functor->default_rule(
72  (__PACKAGE__) => sub {
73   my ($functor, $set, @args) = @_;
74   $set->new(
75    points => [ map $_->$functor(@args), $set->points ],
76    closed => $set->closed,
77   );
78  }
79 );
80
81 __PACKAGE__->meta->make_immutable;
82
83 =head1 AUTHOR
84
85 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
86
87 You can contact me by mail or on C<irc.perl.org> (vincent).
88
89 =head1 BUGS
90
91 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>.
92 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
93
94 =head1 SUPPORT
95
96 You can find documentation for this module with the perldoc command.
97
98     perldoc LaTeX::TikZ
99
100 =head1 COPYRIGHT & LICENSE
101
102 Copyright 2010 Vincent Pit, all rights reserved.
103
104 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
105
106 =cut
107
108 1; # End of LaTeX::TikZ::Set::Polyline