~lib2geom-hackers/lib2geom/trunk

« back to all changes in this revision

Viewing changes to point-fns.cpp

  • Committer: njh
  • Date: 2006-05-22 11:50:24 UTC
  • Revision ID: svn-v4:4601daaa-0314-0410-9a8b-c964a3c23b6b:trunk/lib2geom:1
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "point-fns.h"
 
2
#include "isnan.h"
 
3
 
 
4
using Geom::Point;
 
5
 
 
6
/** Compute the L infinity, or maximum, norm of \a p. */
 
7
Geom::Coord Geom::LInfty(Point const &p) {
 
8
    Geom::Coord const a(fabs(p[0]));
 
9
    Geom::Coord const b(fabs(p[1]));
 
10
    return ( a < b || isNaN(b)
 
11
             ? b
 
12
             : a );
 
13
}
 
14
 
 
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
Geom::is_zero(Point const &p)
 
21
{
 
22
    return ( p[0] == 0 &&
 
23
             p[1] == 0   );
 
24
}
 
25
 
 
26
bool
 
27
Geom::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.  Geom::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
}
 
34
 
 
35
Geom::Coord Geom::atan2(Point const p) {
 
36
    return std::atan2(p[Geom::Y], p[Geom::X]);
 
37
}
 
38
 
 
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 Geom::unit_vector(Point const &a)
 
49
{
 
50
    Point ret(a);
 
51
    ret.normalize();
 
52
    return ret;
 
53
}
 
54
 
 
55
Geom::Point abs(Geom::Point const &b)
 
56
{
 
57
    Geom::Point ret;
 
58
    for ( int i = 0 ; i < 2 ; i++ ) {
 
59
        ret[i] = fabs(b[i]);
 
60
    }
 
61
    return ret;
 
62
}
 
63
 
 
64
 
 
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 :