Created
December 17, 2014 07:48
-
-
Save baku89/e04cb28136a2e71b75b2 to your computer and use it in GitHub Desktop.
点と線分の距離を算出. 直線の描画に使える
This file contains 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
float lineSegDistance( vec2 p, vec2 l1, vec2 l2) { | |
float a2 = pow( distance(l1, p), 2.0 ); | |
float b2 = pow( distance(l2, p), 2.0 ); | |
float c = abs( distance(l1, l2) ); | |
float c2 = c * c; | |
if ( a2 + c2 < b2 || b2 + c2 < a2 ) { | |
return 10000000000000000000.0; | |
} | |
float x = ( a2 - b2 + c2) / ( 2.0 * c ); | |
return sqrt( a2 - x*x ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment