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