//---------------------------------------------------------- // http://local.wasp.uwa.edu.au/~pbourke/geometry/pointline/ static ofPoint getClosestPointUtil(const ofPoint& p1, const ofPoint& p2, const ofPoint& p3, float* normalizedPosition) { // if p1 is coincident with p2, there is no line if(p1 == p2) { if(normalizedPosition != NULL) { *normalizedPosition = 0; } return p1; } float u = (p3.x - p1.x) * (p2.x - p1.x); u += (p3.y - p1.y) * (p2.y - p1.y); // perfect place for fast inverse sqrt... float len = (p2 - p1).length(); u /= (len * len); // clamp u if(u > 1) { u = 1; } else if(u < 0) { u = 0; } if(normalizedPosition != NULL) { *normalizedPosition = u; } return p1.getInterpolated(p2, u); }
//-------------------------------------------------------------- void testApp:: drawPointOnLine(ofPoint p1, ofPoint p2, float t){ t = ofClamp(t, 0, 1); ofPoint circleCenter = p1.getInterpolated(p2, t); ofCircle(circleCenter, 5); ofLine(p1, p2); ofCircle(p1, 3); ofCircle(p2, 3); }