DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Closest Line Point //JavaScript Function

09.09.2005
| 6538 views |
  • submit to reddit
        <a href="http://jsfromhell.com/math/closest-line-point">
Given a dot and a line, it returns the nearest dot over the line.

[UPDATED CODE AND HELP CAN BE FOUND HERE]
</a>


//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/math/closest-line-point [v1.0]

closestLinePoint = function( px, py, x, y, angle ){
	var tg = ( ( angle %= 360 ) < 0 && ( angle += 180 ), Math.tan( -angle * Math.PI / 180 ) );
	return angle < 45 || angle > 135 ? { x: px, y: ( px - x ) * tg + y } : { x: ( py - y ) / tg + x, y: py };
};