]> git.vpit.fr Git - perl/modules/LaTeX-TikZ.git/blob - lib/LaTeX/TikZ/Tools.pm
Just use Mouse instead of Any::Moose
[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 $x, $y>
34
35 Returns true if and only if C<$x> and C<$y> are equal up to L</EPS>.
36
37 =cut
38
39 sub numeq { abs($_[0] - $_[1]) < EPS }
40
41 =head2 C<numcmp $x, $y>
42
43 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>.
44
45 =cut
46
47 sub numcmp { $_[0] < $_[1] - EPS ? -1 : $_[0] > $_[1] + EPS ? 1 : 0 }
48
49 =head2 C<numround $x>
50
51 Returns the closest integer from C<$x> up to L</EPS>.
52
53 =cut
54
55 sub numround {
56  my $x = $_[0];
57  my $i = int $x;
58  $x + EPS < $i + 0.5 ? $i : $i + 1;
59 }
60
61 =head2 C<type_constraint $class>
62
63 Finds the type constraint for C<$class> by first trying to load the relevant F<.pm> file.
64
65 =cut
66
67 sub type_constraint {
68  my ($class) = @_;
69
70  my $file = $class;
71  $file =~ s{::}{/}g;
72  $file .= '.pm';
73  unless ($INC{$file}) {
74   local $@;
75   eval {
76    local $SIG{__DIE__}; # See LaTeX::TikZ::Meta::TypeConstraint::Autocoerce
77    require $file;
78   }
79  }
80
81  find_type_constraint($class);
82 }
83
84 =head1 SEE ALSO
85
86 L<LaTeX::TikZ>.
87
88 =head1 AUTHOR
89
90 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
91
92 You can contact me by mail or on C<irc.perl.org> (vincent).
93
94 =head1 BUGS
95
96 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>.
97 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
98
99 =head1 SUPPORT
100
101 You can find documentation for this module with the perldoc command.
102
103     perldoc LaTeX::TikZ
104
105 =head1 COPYRIGHT & LICENSE
106
107 Copyright 2010 Vincent Pit, all rights reserved.
108
109 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
110
111 =cut
112
113 1; # End of LaTeX::TikZ::Tools