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