Never been to DZone Snippets before?

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

"Intersection dot" between a dot and a line //JavaScript Function (See related posts)


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

[UPDATED CODE AND HELP CAN BE FOUND HERE]


   1  
   2  //+ Jonas Raoni Soares Silva
   3  //@ http://jsfromhell.com/math/dot-line-intersection [v1.0]
   4  
   5  dotLineIntersection = function( x, y, x0, y0, x1, y1 ){
   6  	if( !( x1 - x0 ) )
   7  		return { x: x0, y: y };
   8  	else if( !( y1 - y0 ) )
   9  		return { x: x, y: y0 };
  10  	var left, tg = -1 / ( ( y1 - y0 ) / ( x1 - x0 ) );
  11  	return { x: left = ( x1 * ( x * tg - y + y0 ) + x0 * ( x * - tg + y - y1 ) ) / ( tg * ( x1 - x0 ) + y0 - y1 ), y: tg * left - tg * x + y };
  12  };

You need to create an account or log in to post comments to this site.


Click here to browse all 5349 code snippets

Related Posts