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(Inkscape::Snapper::PointType const &t,
12 NR::Point const &p,
13 std::list<SPItem const *> const &it) const
14 {
15 /* Snap along x (ie to vertical lines) */
16 Inkscape::SnappedPoint const v = _doConstrainedSnap(t, p, component_vectors[NR::X], it);
17 /* Snap along y (ie to horizontal lines) */
18 Inkscape::SnappedPoint const h = _doConstrainedSnap(t, p, component_vectors[NR::Y], it);
20 /* If we snapped to both, combine the two results. This is so that, for example,
21 ** we snap nicely to the intersection of two guidelines.
22 */
23 if (v.getDistance() < NR_HUGE && h.getDistance() < NR_HUGE) {
24 return SnappedPoint(NR::Point(v.getPoint()[NR::X], h.getPoint()[NR::Y]), hypot(v.getDistance(), h.getDistance()));
25 }
27 /* If we snapped to a vertical line, return that */
28 if (v.getDistance() < NR_HUGE) {
29 return v;
30 }
32 /* Otherwise just return any horizontal snap; if we didn't snap to that either
33 ** we haven't snapped to anything.
34 */
35 return h;
36 }
38 Inkscape::SnappedPoint Inkscape::LineSnapper::_doConstrainedSnap(Inkscape::Snapper::PointType const &t,
39 NR::Point const &p,
40 ConstraintLine const &c,
41 std::list<SPItem const *> const &it) const
42 {
43 Inkscape::SnappedPoint s = SnappedPoint(p, NR_HUGE);
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(NR::unit_vector(c.getDirection())));
53 /* Constant term of the line we're trying to snap along */
54 NR::Coord const q = dot(n, c.hasPoint() ? c.getPoint() : 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 :