curvedraw

changeset 14:b625f0575d66

point snapping
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 17 Dec 2015 16:41:42 +0200
parents 9f75208b81cd
children 37ab3a4c02f8
files src/app.cc
diffstat 1 files changed, 36 insertions(+), 1 deletions(-) [+]
line diff
     1.1 --- a/src/app.cc	Thu Dec 17 07:22:14 2015 +0200
     1.2 +++ b/src/app.cc	Thu Dec 17 16:41:42 2015 +0200
     1.3 @@ -353,7 +353,42 @@
     1.4  	case SNAP_GRID:
     1.5  		return Vector2(round(p.x / grid_size) * grid_size, round(p.y / grid_size) * grid_size);
     1.6  	case SNAP_POINT:
     1.7 -		// TODO
     1.8 +		{
     1.9 +			Curve *nearest_curve = 0;
    1.10 +			int nearest_curve_pidx = -1;
    1.11 +			float nearest_dist_sq = FLT_MAX;
    1.12 +
    1.13 +			if(new_curve) {
    1.14 +				// find the closest point, ignoring the last
    1.15 +				for(int i=0; i<new_curve->size() - 1; i++) {
    1.16 +					Vector2 cp = new_curve->get_point(i);
    1.17 +					float distsq = (cp - p).length_sq();
    1.18 +					if(distsq < nearest_dist_sq) {
    1.19 +						nearest_curve = new_curve;
    1.20 +						nearest_dist_sq = distsq;
    1.21 +						nearest_curve_pidx = i;
    1.22 +					}
    1.23 +				}
    1.24 +			}
    1.25 +
    1.26 +
    1.27 +			for(size_t i=0; i<curves.size(); i++) {
    1.28 +				int pidx = curves[i]->nearest_point(p);
    1.29 +				Vector2 cp = curves[i]->get_point(pidx);
    1.30 +				float dist_sq = (cp - p).length_sq();
    1.31 +				if(dist_sq < nearest_dist_sq) {
    1.32 +					nearest_curve = curves[i];
    1.33 +					nearest_curve_pidx = pidx;
    1.34 +					nearest_dist_sq = dist_sq;
    1.35 +				}
    1.36 +			}
    1.37 +
    1.38 +			if(nearest_curve) {
    1.39 +				return nearest_curve->get_point(nearest_curve_pidx);
    1.40 +			}
    1.41 +		}
    1.42 +		break;
    1.43 +
    1.44  	default:
    1.45  		break;
    1.46  	}