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 snap_vector_midpoint (NR::Point p, NR::Point begin, NR::Point end, double snap)
66 {
67 double length = NR::L2(end - begin);
68 NR::Point be = (end - begin) / length;
69 double r = NR::dot(p - begin, be);
71 if (r < 0.0) return begin;
72 if (r > length) return end;
74 double snapdist = length * snap;
75 double r_snapped = (snap==0) ? r : floor(r/(snapdist + 0.5)) * snapdist;
77 return (begin + r_snapped * be);
78 }
80 double
81 get_offset_between_points (NR::Point p, NR::Point begin, NR::Point end)
82 {
83 double length = NR::L2(end - begin);
84 NR::Point be = (end - begin) / length;
85 double r = NR::dot(p - begin, be);
87 if (r < 0.0) return 0.0;
88 if (r > length) return 1.0;
90 return (r / length);
91 }
93 NR::Point
94 project_on_linesegment(NR::Point const p, NR::Point const p1, NR::Point const p2)
95 {
96 // p_proj = projection of p on the linesegment running from p1 to p2
97 // p_proj = p1 + u (p2 - p1)
98 // calculate u according to "Minimum Distance between a Point and a Line"
99 // see http://local.wasp.uwa.edu.au/~pbourke/geometry/pointline/
101 // Warning: projected points will not necessarily be in between the endpoints of the linesegments!
103 if (p1 == p2) { // to avoid div. by zero below
104 return p;
105 }
107 NR::Point const d1(p-p1); // delta 1
108 NR::Point const d2(p2-p1); // delta 2
109 double const u = (d1[NR::X] * d2[NR::X] + d1[NR::Y] * d2[NR::Y]) / (NR::L2(d2) * NR::L2(d2));
111 return (p1 + u*(p2-p1));
112 }
114 /*
115 Local Variables:
116 mode:c++
117 c-file-style:"stroustrup"
118 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
119 indent-tabs-mode:nil
120 fill-column:99
121 End:
122 */
123 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :