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 bool const &f,
14 std::vector<NR::Point> &points_to_snap,
15 std::list<SPItem const *> const &it) const
16 {
17 /* Snap along x (ie to vertical lines) */
18 Inkscape::SnappedPoint const v = _doConstrainedSnap(t, p, f, points_to_snap, component_vectors[NR::X], it);
19 /* Snap along y (ie to horizontal lines) */
20 Inkscape::SnappedPoint const h = _doConstrainedSnap(t, p, f, points_to_snap, component_vectors[NR::Y], it);
22 /* If we snapped to both, combine the two results. This is so that, for example,
23 ** we snap nicely to the intersection of two guidelines.
24 */
25 if (v.getDistance() < NR_HUGE && h.getDistance() < NR_HUGE) {
26 return SnappedPoint(NR::Point(v.getPoint()[NR::X], h.getPoint()[NR::Y]), hypot(v.getDistance(), h.getDistance()));
27 }
29 /* If we snapped to a vertical line, return that */
30 if (v.getDistance() < NR_HUGE) {
31 return v;
32 }
34 /* Otherwise just return any horizontal snap; if we didn't snap to that either
35 ** we haven't snapped to anything.
36 */
37 return h;
38 }
40 Inkscape::SnappedPoint Inkscape::LineSnapper::_doConstrainedSnap(Inkscape::Snapper::PointType const &t,
41 NR::Point const &p,
42 bool const &f,
43 std::vector<NR::Point> &points_to_snap,
44 ConstraintLine const &c,
45 std::list<SPItem const *> const &it) const
46 {
47 Inkscape::SnappedPoint s = SnappedPoint(p, NR_HUGE);
49 /* Get the lines that we will try to snap to */
50 const LineList lines = _getSnapLines(p);
52 for (LineList::const_iterator i = lines.begin(); i != lines.end(); i++) {
54 /* Normal to the line we're trying to snap along */
55 NR::Point const n(NR::rot90(NR::unit_vector(c.getDirection())));
57 /* Constant term of the line we're trying to snap along */
58 NR::Coord const q = dot(n, c.hasPoint() ? c.getPoint() : p);
60 /* Try to intersect this line with the target line */
61 NR::Point t = p;
62 IntersectorKind const k = intersector_line_intersection(n, q, component_vectors[i->first], i->second, t);
64 if (k == INTERSECTS) {
65 const NR::Coord dist = L2(t - p);
66 if (dist < getDistance() && dist < s.getDistance() ) {
67 s = SnappedPoint(t, dist);
68 }
69 }
70 }
72 return s;
73 }
75 /*
76 Local Variables:
77 mode:c++
78 c-file-style:"stroustrup"
79 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
80 indent-tabs-mode:nil
81 fill-column:99
82 End:
83 */
84 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :