1 #include <libnr/nr-point-fns.h>
2 #include <isnan.h>
4 using NR::Point;
6 /** Compute the L infinity, or maximum, norm of \a p. */
7 NR::Coord NR::LInfty(Point const &p) {
8 NR::Coord const a(fabs(p[0]));
9 NR::Coord const b(fabs(p[1]));
10 return ( a < b || isNaN(b)
11 ? b
12 : a );
13 }
15 /** Returns true iff p is a zero vector, i.e.\ Point(0, 0).
16 *
17 * (NaN is considered non-zero.)
18 */
19 bool
20 NR::is_zero(Point const &p)
21 {
22 return ( p[0] == 0 &&
23 p[1] == 0 );
24 }
26 bool
27 NR::is_unit_vector(Point const &p)
28 {
29 return fabs(1.0 - L2(p)) <= 1e-4;
30 /* The tolerance of 1e-4 is somewhat arbitrary. NR::Point::normalize is believed to return
31 points well within this tolerance. I'm not aware of any callers that want a small
32 tolerance; most callers would be ok with a tolerance of 0.25. */
33 }
35 NR::Coord NR::atan2(Point const p) {
36 return std::atan2(p[NR::Y], p[NR::X]);
37 }
39 /** Returns a version of \a a scaled to be a unit vector (within rounding error).
40 *
41 * The current version tries to handle infinite coordinates gracefully,
42 * but it's not clear that any callers need that.
43 *
44 * \pre a != Point(0, 0).
45 * \pre Neither coordinate is NaN.
46 * \post L2(ret) very near 1.0.
47 */
48 Point NR::unit_vector(Point const &a)
49 {
50 Point ret(a);
51 ret.normalize();
52 return ret;
53 }
55 NR::Point abs(NR::Point const &b)
56 {
57 NR::Point ret;
58 for ( int i = 0 ; i < 2 ; i++ ) {
59 ret[i] = fabs(b[i]);
60 }
61 return ret;
62 }
65 /*
66 Local Variables:
67 mode:c++
68 c-file-style:"stroustrup"
69 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
70 indent-tabs-mode:nil
71 fill-column:99
72 End:
73 */
74 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :