Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

"Intersection Dot" Between A Dot And A Line //JavaScript Function

DZone's Guide to

"Intersection Dot" Between A Dot And A Line //JavaScript Function

·
Free Resource

Given a line and a dot, it return the coordinates of their intersection.

[UPDATED CODE AND HELP CAN BE FOUND HERE]



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

dotLineIntersection = function( x, y, x0, y0, x1, y1 ){
	if( !( x1 - x0 ) )
		return { x: x0, y: y };
	else if( !( y1 - y0 ) )
		return { x: x, y: y0 };
	var left, tg = -1 / ( ( y1 - y0 ) / ( x1 - x0 ) );
	return { x: left = ( x1 * ( x * tg - y + y0 ) + x0 * ( x * - tg + y - y1 ) ) / ( tg * ( x1 - x0 ) + y0 - y1 ), y: tg * left - tg * x + y };
};
Topics:

Opinions expressed by DZone contributors are their own.

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 }}