Skip to content

Instantly share code, notes, and snippets.

@larsberg
Last active May 19, 2021 00:28
Show Gist options
  • Save larsberg/8561823 to your computer and use it in GitHub Desktop.
Save larsberg/8561823 to your computer and use it in GitHub Desktop.
closest point on line in OF.
//closest point on line
//http://paulbourke.net/geometry/pointlineplane/
ofVec3f closestPointOnLine(ofVec3f p, ofVec3f l0, ofVec3f l1 )
{
if(l0 == l1) return ofVec3f();
float u = (((p.x-l0.x) * (l1.x-l0.x)) + ((p.y-l0.y) * (l1.y-l0.y)) + ((p.z-l0.z) * (l1.z-l0.z))) / l1.distanceSquared( l0 );
if( u < 0.0f )
return l0;
else if(u > 1.0f )
return l1;
return l0 + u * (l1 - l0);//lerp between the end points
}
float DistancePointToLine( ofVec3f p, ofVec3f l0, ofVec3f l1 )
{
return p.distance(closestPointOnLine(p, l0, l1));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment