1 package LaTeX::TikZ::Mod::Clip;
8 LaTeX::TikZ::Mod::Clip - A modifier that clips sequences with a path.
16 our $VERSION = '0.01';
20 use LaTeX::TikZ::Formatter;
21 use LaTeX::TikZ::Mod::Formatted;
23 use LaTeX::TikZ::Interface;
24 use LaTeX::TikZ::Functor;
26 use LaTeX::TikZ::Tools;
30 with 'LaTeX::TikZ::Mod';
34 does => 'LaTeX::TikZ::Set::Op',
38 my $default_formatter = LaTeX::TikZ::Formatter->new(
49 Sub::Name::subname('get_tc' => sub {
52 return $tc{$class} if exists $tc{class};
54 my $tc = LaTeX::TikZ::Tools::type_constraint($class);
55 return unless defined $tc;
61 my $cover_rectangle = Sub::Name::subname('cover_rectangle' => sub {
62 my ($old, $new, $self_tc) = @_;
72 ($x, $X) = ($X, $x) if $x > $X;
73 ($y, $Y) = ($Y, $y) if $y > $Y;
75 if ($self_tc->check($old)) {
76 # The old rectangle covers the new one if and only if it's inside the new.
78 for ($old->from, $old->to) {
80 return 0 if LaTeX::TikZ::Tools::numcmp($r, $x) < 0
81 or LaTeX::TikZ::Tools::numcmp($X, $r) < 0;
83 return 0 if LaTeX::TikZ::Tools::numcmp($i, $y) < 0
84 or LaTeX::TikZ::Tools::numcmp($Y, $i) < 0;
93 my $cover_circle = Sub::Name::subname('cover_circle' => sub {
94 my ($old, $new, $self_tc) = @_;
96 my $c2 = $new->center;
97 my $r2 = $new->radius;
99 if ($self_tc->check($old)) {
100 # The old circle covers the new one if and only if it's inside the new.
102 my $c1 = $old->center;
103 my $r1 = $old->radius;
105 my $d = abs($c1 - $c2);
107 return LaTeX::TikZ::Tools::numcmp($d, $r2) <= 0
108 && LaTeX::TikZ::Tools::numcmp($d + $r1, $r2) <= 0;
115 [ 'LaTeX::TikZ::Set::Rectangle' => $cover_rectangle ],
116 [ 'LaTeX::TikZ::Set::Circle' => $cover_circle ],
120 my ($old, $new) = map $_->clip, @_[0, 1];
123 my $tc = $get_tc->($_->[0]);
124 next unless defined $tc and $tc->check($new);
125 return $_->[1]->($old, $new, $tc);
128 $old->path($default_formatter) eq $new->path($default_formatter);
136 LaTeX::TikZ::Mod::Formatted->new(
138 content => $_[0]->clip->path($_[1]),
142 LaTeX::TikZ::Interface->register(
146 __PACKAGE__->new(clip => $_[0]);
150 LaTeX::TikZ::Functor->default_rule(
151 (__PACKAGE__) => sub {
152 my ($functor, $mod, @args) = @_;
153 $mod->new(clip => $mod->clip->$functor(@args))
157 __PACKAGE__->meta->make_immutable;
161 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
163 You can contact me by mail or on C<irc.perl.org> (vincent).
167 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>.
168 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
172 You can find documentation for this module with the perldoc command.
176 =head1 COPYRIGHT & LICENSE
178 Copyright 2010 Vincent Pit, all rights reserved.
180 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
184 1; # End of LaTeX::TikZ::Mod::Clip