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 ConstraintLine const &c,
39 std::list<SPItem const *> const &it) const
40 {
41 Inkscape::SnappedPoint s = SnappedPoint(p, NR_HUGE);
43 /* Get the lines that we will try to snap to */
44 const LineList lines = _getSnapLines(p);
46 for (LineList::const_iterator i = lines.begin(); i != lines.end(); i++) {
48 /* Normal to the line we're trying to snap along */
49 NR::Point const n(NR::rot90(NR::unit_vector(c.getDirection())));
51 /* Constant term of the line we're trying to snap along */
52 NR::Coord const q = dot(n, c.hasPoint() ? c.getPoint() : p);
54 /* Try to intersect this line with the target line */
55 NR::Point t = p;
56 IntersectorKind const k = intersector_line_intersection(n, q, component_vectors[i->first], i->second, t);
58 if (k == INTERSECTS) {
59 const NR::Coord dist = L2(t - p);
60 if (dist < getDistance() && dist < s.getDistance() ) {
61 s = SnappedPoint(t, dist);
62 }
63 }
64 }
66 return s;
67 }
69 /*
70 Local Variables:
71 mode:c++
72 c-file-style:"stroustrup"
73 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
74 indent-tabs-mode:nil
75 fill-column:99
76 End:
77 */
78 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :