Code

moving trunk for module inkscape
[inkscape.git] / src / line-snapper.cpp
1 #include "libnr/nr-values.h"
2 #include "libnr/nr-point-fns.h"
3 #include "geom.h"
4 #include "line-snapper.h"
6 Inkscape::LineSnapper::LineSnapper(SPNamedView const *nv, NR::Coord const d) : Snapper(nv, d)
7 {
9 }
11 Inkscape::SnappedPoint Inkscape::LineSnapper::_doFreeSnap(NR::Point const &p,
12                                                           std::list<SPItem const *> const &it) const
13 {
14     /* Snap along x (ie to vertical lines) */
15     Inkscape::SnappedPoint const v = _doConstrainedSnap(p, component_vectors[NR::X], it);
16     /* Snap along y (ie to horizontal lines) */
17     Inkscape::SnappedPoint const h = _doConstrainedSnap(p, component_vectors[NR::Y], it);
19     /* If we snapped to both, combine the two results.  This is so that, for example,
20     ** we snap nicely to the intersection of two guidelines.
21     */
22     if (v.getDistance() < NR_HUGE && h.getDistance() < NR_HUGE) {
23         return SnappedPoint(NR::Point(v.getPoint()[NR::X], h.getPoint()[NR::Y]), hypot(v.getDistance(), h.getDistance()));
24     }
26     /* If we snapped to a vertical line, return that */
27     if (v.getDistance() < NR_HUGE) {
28         return v;
29     }
31     /* Otherwise just return any horizontal snap; if we didn't snap to that either
32     ** we haven't snapped to anything.
33     */
34     return h;
35 }
37 Inkscape::SnappedPoint Inkscape::LineSnapper::_doConstrainedSnap(NR::Point const &p,
38                                                                  NR::Point const &c,
39                                                                  std::list<SPItem const *> const &it) const
40 {
41     Inkscape::SnappedPoint s = SnappedPoint(p, NR_HUGE);
43     NR::Point const v = NR::unit_vector(c);
45     /* Get the lines that we will try to snap to */
46     const LineList lines = _getSnapLines(p);
48     for (LineList::const_iterator i = lines.begin(); i != lines.end(); i++) {
50         /* Normal to the line we're trying to snap along */
51         NR::Point const n(NR::rot90(v));
53         /* Hence constant term of the line we're trying to snap along */
54         NR::Coord const q = dot(n, p);
56         /* Try to intersect this line with the target line */
57         NR::Point t = p;
58         IntersectorKind const k = intersector_line_intersection(n, q, component_vectors[i->first], i->second, t);
60         if (k == INTERSECTS) {
61             const NR::Coord dist = L2(t - p);
62             if (dist < getDistance() && dist < s.getDistance() ) {
63                 s = SnappedPoint(t, dist);
64             }
65         }
66     }
68     return s;
69 }
71 /*
72   Local Variables:
73   mode:c++
74   c-file-style:"stroustrup"
75   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
76   indent-tabs-mode:nil
77   fill-column:99
78   End:
79 */
80 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :