Skip to content

Instantly share code, notes, and snippets.

@slembcke
Created December 2, 2011 19:52
Show Gist options
  • Save slembcke/1424609 to your computer and use it in GitHub Desktop.
Save slembcke/1424609 to your computer and use it in GitHub Desktop.
Closest Point on Segment
cpVect a, b; // segment endpoints
cpVect p; // point you are testing
cpVect delta = cpvsub(b, a);
cpFloat closest_t = cpvdot(delta, cpvsub(p, a))/cpvlengthsq(delta);
cpVect closest = cpvlerp(a, b, cpfclamp01(closest_t));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment