Skip to content

Instantly share code, notes, and snippets.

@v3c70r
Created September 19, 2015 18:51
Show Gist options
  • Save v3c70r/c389168cef4382e6a036 to your computer and use it in GitHub Desktop.
Save v3c70r/c389168cef4382e6a036 to your computer and use it in GitHub Desktop.
/**
* @brief Get distance from a point to line
*
* @param p0 The point
* @param p1 Starting point of line
* @param p2 End point of line
*
* @return distance*distance
*/
float pointLineDistance(const glm::vec2 &p0, const glm::vec2 &p1, const glm::vec2 &p2)
{
return
( (p2.y - p1.y) * p0.x - (p2.x-p1.x)*p0.y + p2.x*p1.y - p2.y*p1.x) *
( (p2.y - p1.y) * p0.x - (p2.x-p1.x)*p0.y + p2.x*p1.y - p2.y*p1.x) /
((p2.y - p1.y) * (p2.y - p1.y) + (p2.x-p1.x)*(p2.x-p1.x));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment