]> git.vpit.fr Git - perl/modules/LaTeX-TikZ.git/blob - lib/LaTeX/TikZ/Tools.pm
Make sure POD headings are linkable
[perl/modules/LaTeX-TikZ.git] / lib / LaTeX / TikZ / Tools.pm
1 package LaTeX::TikZ::Tools;
2
3 use strict;
4 use warnings;
5
6 =head1 NAME
7
8 LaTeX::TikZ::Tools - Miscellaneous tools for LaTeX::TikZ classes.
9
10 =head1 VERSION
11
12 Version 0.02
13
14 =cut
15
16 our $VERSION = '0.02';
17
18 use Mouse::Util::TypeConstraints 'find_type_constraint';
19
20 =head1 CONSTANTS
21
22 =head2 C<EPS>
23
24 The numerical accuracy enforced by L</numeq>, L</numcmp> and L</numround>.
25 It is currently set to C<1e-10>.
26
27 =cut
28
29 use constant EPS => 1e-10;
30
31 =head1 FUNCTIONS
32
33 =head2 C<numeq>
34
35     numeq($x, $y)
36
37 Returns true if and only if C<$x> and C<$y> are equal up to L</EPS>.
38
39 =cut
40
41 sub numeq { abs($_[0] - $_[1]) < EPS }
42
43 =head2 C<numcmp>
44
45     numcmp($x, $y)
46
47 Returns a negative number, zero, or a positive number when C<$x> is respectively smaller than, equal to, or greater than C<$y> up to L</EPS>.
48
49 =cut
50
51 sub numcmp { $_[0] < $_[1] - EPS ? -1 : $_[0] > $_[1] + EPS ? 1 : 0 }
52
53 =head2 C<numround>
54
55     numround($x)
56
57 Returns the closest integer from C<$x> up to L</EPS>.
58
59 =cut
60
61 sub numround {
62  my $x = $_[0];
63  my $i = int $x;
64  $x + EPS < $i + 0.5 ? $i : $i + 1;
65 }
66
67 =head2 C<type_constraint>
68
69     my $tc = type_constraint($class)
70
71 Finds the type constraint for C<$class> by first trying to load the relevant F<.pm> file.
72
73 =cut
74
75 sub type_constraint {
76  my ($class) = @_;
77
78  my $file = $class;
79  $file =~ s{::}{/}g;
80  $file .= '.pm';
81  unless ($INC{$file}) {
82   local $@;
83   eval {
84    local $SIG{__DIE__}; # See LaTeX::TikZ::Meta::TypeConstraint::Autocoerce
85    require $file;
86   }
87  }
88
89  find_type_constraint($class);
90 }
91
92 =head1 SEE ALSO
93
94 L<LaTeX::TikZ>.
95
96 =head1 AUTHOR
97
98 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
99
100 You can contact me by mail or on C<irc.perl.org> (vincent).
101
102 =head1 BUGS
103
104 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>.
105 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
106
107 =head1 SUPPORT
108
109 You can find documentation for this module with the perldoc command.
110
111     perldoc LaTeX::TikZ
112
113 =head1 COPYRIGHT & LICENSE
114
115 Copyright 2010,2011,2012,2013,2014,2015 Vincent Pit, all rights reserved.
116
117 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
118
119 =cut
120
121 1; # End of LaTeX::TikZ::Tools