Last active
May 19, 2021 00:28
-
-
Save larsberg/8561823 to your computer and use it in GitHub Desktop.
closest point on line in OF.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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