]> git.vpit.fr Git - perl/modules/LaTeX-TikZ.git/blob - lib/LaTeX/TikZ/Set/Arc.pm
Reimplement LT::Set::Polyline as a child class of LT::Set::Chain
[perl/modules/LaTeX-TikZ.git] / lib / LaTeX / TikZ / Set / Arc.pm
1 package LaTeX::TikZ::Set::Arc;
2
3 use strict;
4 use warnings;
5
6 =head1 NAME
7
8 LaTeX::TikZ::Set::Arc - A combined set object representing an arc.
9
10 =head1 VERSION
11
12 Version 0.02
13
14 =cut
15
16 our $VERSION = '0.02';
17
18 use Carp          ();
19 use Math::Complex ();
20 use Math::Trig    ();
21
22 use LaTeX::TikZ::Point;
23
24 use LaTeX::TikZ::Set::Circle;
25 use LaTeX::TikZ::Set::Polyline;
26
27 use LaTeX::TikZ::Interface;
28
29 use LaTeX::TikZ::Tools;
30
31 use Any::Moose 'Util::TypeConstraints' => [ 'find_type_constraint' ];
32
33 my $ltp_tc = find_type_constraint('LaTeX::TikZ::Point::Autocoerce');
34
35 LaTeX::TikZ::Interface->register(
36  arc => sub {
37   shift;
38   Carp::confess('Tikz->arc($first_point, $second_point, $center)') if @_ < 3;
39   my ($a, $b, $c) = @_;
40
41   for ($a, $b, $c) {
42    my $p = $ltp_tc->coerce($_);
43    $ltp_tc->assert_valid($p);
44    $_ = Math::Complex->make($p->x, $p->y);
45   }
46
47   my $r = abs($a - $c);
48   Carp::confess("The two first points aren't on a circle of center the last")
49                              unless LaTeX::TikZ::Tools::numeq(abs($b - $c), $r);
50
51   my $set = LaTeX::TikZ::Set::Circle->new(
52    center => $c,
53    radius => $r,
54   );
55
56   my $factor = 1/32;
57
58   my $theta  = (($b - $c) / ($a - $c))->arg;
59   my $points = int(abs($theta) / abs(Math::Trig::acos(0.95)));
60   $theta    /= $points + 1;
61   my $rho    = (1 / cos($theta)) / (1 - $factor);
62
63   my $ua = ($a - $c) * (1 - $factor) + $c;
64   my $ub = ($b - $c) * (1 - $factor) + $c;
65
66   my @outside = map { $_ * $rho + $c } (
67    $a - $c,
68    (map { ($a - $c) * Math::Complex->emake(1, $_ * $theta) } 1 .. $points),
69    $b - $c,
70   );
71
72   $set->clip(
73    LaTeX::TikZ::Set::Polyline->new(
74     points => [ $ua, @outside, $ub ],
75     cycle  => 1,
76    ),
77   );
78  },
79 );
80
81 =head1 SEE ALSO
82
83 L<LaTeX::TikZ>, L<LaTeX::TikZ::Set>.
84
85 =head1 AUTHOR
86
87 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
88
89 You can contact me by mail or on C<irc.perl.org> (vincent).
90
91 =head1 BUGS
92
93 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>.
94 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
95
96 =head1 SUPPORT
97
98 You can find documentation for this module with the perldoc command.
99
100     perldoc LaTeX::TikZ
101
102 =head1 COPYRIGHT & LICENSE
103
104 Copyright 2010 Vincent Pit, all rights reserved.
105
106 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
107
108 =cut
109
110 1; # End of LaTeX::TikZ::Set::Arc