Over a million developers have joined DZone.

Closest Line Point //JavaScript Function

·

Given a dot and a line, it returns the nearest dot over the line.

[UPDATED CODE AND HELP CAN BE FOUND HERE]




//+ 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 };
};


Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}