# HG changeset patch # User John Tsiombikas # Date 1450363302 -7200 # Node ID b625f0575d66cd2e8b69a0acdfe9ae26bc186719 # Parent 9f75208b81cd11624885d56037272f590be46f63 point snapping diff -r 9f75208b81cd -r b625f0575d66 src/app.cc --- a/src/app.cc Thu Dec 17 07:22:14 2015 +0200 +++ b/src/app.cc Thu Dec 17 16:41:42 2015 +0200 @@ -353,7 +353,42 @@ case SNAP_GRID: return Vector2(round(p.x / grid_size) * grid_size, round(p.y / grid_size) * grid_size); case SNAP_POINT: - // TODO + { + Curve *nearest_curve = 0; + int nearest_curve_pidx = -1; + float nearest_dist_sq = FLT_MAX; + + if(new_curve) { + // find the closest point, ignoring the last + for(int i=0; isize() - 1; i++) { + Vector2 cp = new_curve->get_point(i); + float distsq = (cp - p).length_sq(); + if(distsq < nearest_dist_sq) { + nearest_curve = new_curve; + nearest_dist_sq = distsq; + nearest_curve_pidx = i; + } + } + } + + + for(size_t i=0; inearest_point(p); + Vector2 cp = curves[i]->get_point(pidx); + float dist_sq = (cp - p).length_sq(); + if(dist_sq < nearest_dist_sq) { + nearest_curve = curves[i]; + nearest_curve_pidx = pidx; + nearest_dist_sq = dist_sq; + } + } + + if(nearest_curve) { + return nearest_curve->get_point(nearest_curve_pidx); + } + } + break; + default: break; }