public void getClosestPoint ( Vector2f point , Vector2f result ) { loc . set ( point ) ; loc . sub ( start ) ; float projDistance = vec . dot ( loc ) ; projDistance /= vec . lengthSquared ( ) ; if ( projDistance < 0 ) { result . set ( start ) ; return ; } if ( projDistance > 1 ) { result . set ( end ) ; return ; } result . x = start . getX ( ) + projDistance * vec . getX ( ) ; result . y = start . getY ( ) + projDistance * vec . getY ( ) ; }
Get the closest point on the line to a given point