Code

5082c3a109950a09e5e7dbcf83d4e367456512f5
[inkscape.git] / src / libnr / nr-point-fns.cpp
1 #include <libnr/nr-point-fns.h>
2 #include <2geom/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 || IS_NAN(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 }
64 NR::Point *
65 get_snap_vector (NR::Point p, NR::Point o, double snap, double initial)
66 {
67     double r = NR::L2 (p - o);
68     if (r < 1e-3)
69         return NULL;
70     double angle = NR::atan2 (p - o);
71     // snap angle to snaps increments, starting from initial:
72     double a_snapped = initial + floor((angle - initial)/snap + 0.5) * snap;
73     // calculate the new position and subtract p to get the vector:
74     return new NR::Point (o + r * NR::Point(cos(a_snapped), sin(a_snapped)) - p);
75 }
77 NR::Point
78 snap_vector_midpoint (NR::Point p, NR::Point begin, NR::Point end, double snap)
79 {
80     double length = NR::L2(end - begin);
81     NR::Point be = (end - begin) / length;
82     double r = NR::dot(p - begin, be);
84     if (r < 0.0) return begin;
85     if (r > length) return end;
87     double snapdist = length * snap;
88     double r_snapped = (snap==0) ? r : floor(r/(snapdist + 0.5)) * snapdist;
90     return (begin + r_snapped * be);
91 }
93 double
94 get_offset_between_points (NR::Point p, NR::Point begin, NR::Point end)
95 {
96     double length = NR::L2(end - begin);
97     NR::Point be = (end - begin) / length;
98     double r = NR::dot(p - begin, be);
100     if (r < 0.0) return 0.0;
101     if (r > length) return 1.0;
103     return (r / length);
106 NR::Point
107 project_on_linesegment(NR::Point const p, NR::Point const p1, NR::Point const p2) 
109     // p_proj = projection of p on the linesegment running from p1 to p2
110     // p_proj = p1 + u (p2 - p1)
111     // calculate u according to "Minimum Distance between a Point and a Line"
112     // see http://local.wasp.uwa.edu.au/~pbourke/geometry/pointline/        
113     
114     // Warning: projected points will not necessarily be in between the endpoints of the linesegments!  
115     
116     if (p1 == p2) { // to avoid div. by zero below
117         return p;
118     }
119     
120     NR::Point const d1(p-p1); // delta 1
121     NR::Point const d2(p2-p1); // delta 2
122     double const u = (d1[NR::X] * d2[NR::X] + d1[NR::Y] * d2[NR::Y]) / (NR::L2(d2) * NR::L2(d2));
123     
124     return (p1 + u*(p2-p1));
127 /*
128   Local Variables:
129   mode:c++
130   c-file-style:"stroustrup"
131   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
132   indent-tabs-mode:nil
133   fill-column:99
134   End:
135 */
136 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :