Logo Search packages:      
Sourcecode: inkscape version File versions  Download package

void sp_nodepath_add_node_near_point ( Inkscape::NodePath::Path nodepath,
Geom::Point  p 
)

Add a node nearest to point

Definition at line 2084 of file nodepath.cpp.

References create_curve(), NR_LINETO, sp_node_get_side(), sp_node_path_code_from_side(), sp_nodepath_get_node_by_index(), sp_nodepath_line_add_node(), sp_nodepath_node_select(), sp_nodepath_update_handles(), sp_nodepath_update_repr(), and sp_nodepath_update_statusbar().

{
    if (!nodepath) {
        return;
    }

    SPCurve *curve = create_curve(nodepath);   // perhaps we can use nodepath->curve here instead?
    Geom::PathVector const &pathv = curve->get_pathvector();
    boost::optional<Geom::PathVectorPosition> pvpos = Geom::nearestPoint(pathv, p);
    if (!pvpos) {
        g_print ("Possible error?\n");
        return;
    }

    // calculate index for nodepath's representation.
    double int_part;
    double t = std::modf(pvpos->t, &int_part);
    unsigned int segment_index = (unsigned int)int_part + 1;
    for (unsigned int i = 0; i < pvpos->path_nr; ++i) {
        segment_index += pathv[i].size() + 1;
        if (pathv[i].closed()) {
            segment_index += 1;
        }
    }

    curve->unref();

    //find segment to split
    Inkscape::NodePath::Node *e = sp_nodepath_get_node_by_index(nodepath, segment_index);
    if (!e) {
        return;
    }

    //don't know why but t seems to flip for lines
    if (sp_node_path_code_from_side(e, sp_node_get_side(e, -1)) == NR_LINETO) {
        t = 1.0 - t;
    }

    Inkscape::NodePath::Node *n = sp_nodepath_line_add_node(e, t);
    sp_nodepath_node_select(n, FALSE, TRUE);

    /* fixme: adjust ? */
    sp_nodepath_update_handles(nodepath);

    sp_nodepath_update_repr(nodepath, _("Add node"));

    sp_nodepath_update_statusbar(nodepath);
}


Generated by  Doxygen 1.6.0   Back to index