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

About this user

Jonas Raoni Soares Silva http://jsfromhell.com

« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS 

Closest Square Point //JavaScript Function


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

[UPDATED CODE AND HELP CAN BE FOUND HERE]



   1  
   2  //+ Jonas Raoni Soares Silva
   3  //@ http://jsfromhell.com/math/closest-square-point [v1.0]
   4  
   5  closestSquarePoint = function( px, py, x, y, w, h ){
   6  	return { x: px < x ? x : px > x + w ? x + w : px, y: py < y ? y : py > y + h ? y + h : py };
   7  };

Closest Polyline Point //Javascript Function


Given a dot and a set of lines, it returns the nearest dot over one of the given lines.

[UPDATED CODE AND HELP CAN BE FOUND HERE]



   1  
   2  //+ Jonas Raoni Soares Silva
   3  //@ http://jsfromhell.com/math/closest-polyline-point [v1.0]
   4  
   5  closestPolyLinePoint = function( px, py, x0, y0, x1, y1, etc, etc, etc ){
   6  	function dotLineLength( x, y, x0, y0, x1, y1, o ){
   7  		function lineLength( x, y, x0, y0 ){
   8  			return Math.sqrt( ( x -= x0 ) * x + ( y -= y0 ) * y );
   9  		}
  10  		if( o && !( o = function( x, y, x0, y0, x1, y1 ){
  11  			if( !( x1 - x0 ) ) return { x: x0, y: y };
  12  			else if( !( y1 - y0 ) ) return { x: x, y: y0 };
  13  			var left, tg = -1 / ( ( y1 - y0 ) / ( x1 - x0 ) );
  14  			return { x: left = ( x1 * ( x * tg - y + y0 ) + x0 * ( x * - tg + y - y1 ) ) / ( tg * ( x1 - x0 ) + y0 - y1 ), y: tg * left - tg * x + y };
  15  		}( x, y, x0, y0, x1, y1 ), o.x >= Math.min( x0, x1 ) && o.x <= Math.max( x0, x1 ) && o.y >= Math.min( y0, y1 ) && o.y <= Math.max( y0, y1 ) ) ){
  16  			var l1 = lineLength( x, y, x0, y0 ), l2 = lineLength( x, y, x1, y1 );
  17  			return l1 > l2 ? l2 : l1;
  18  		}
  19  		else {
  20  			var a = y0 - y1, b = x1 - x0, c = x0 * y1 - y0 * x1;
  21  			return Math.abs( a * x + b * y + c ) / Math.sqrt( a * a + b * b );
  22  		}
  23  	};
  24  	for( var args = [].slice.call( arguments, 0 ), lines = []; args.length > 4; lines[lines.length] = { y1: args.pop(), x1: args.pop(), y0: args.pop(), x0: args.pop() } );
  25  	if( !lines.length )
  26  		return { x: px, y: py };
  27  	var l, i, o = lines[0], lower = { i: 0, l: dotLineLength( px, py, o.x0, o.y0, o.x1, o.y1 ) };
  28  	for( i in lines )
  29  		if( lower.l > ( l = dotLineLength( px, py, ( o = lines[i] ).x0, o.y0, o.x1, o.y1 ) ) )
  30  			lower = { i: i, l: l };
  31  	py = py < Math.min( ( o = lines[lower.i] ).y0, o.y1 ) ? Math.min( o.y0, o.y1 ) : py > Math.max( o.y0, o.y1 ) ? Math.max( o.y0, o.y1 ) : py;
  32  	px = px < Math.min( o.x0, o.x1 ) ? Math.min( o.x0, o.x1 ) : px > Math.max( o.x0, o.x1 ) ? Math.max( o.x0, o.x1 ) : px;
  33  	return Math.abs( o.x0 - o.x1 ) < Math.abs( o.y0 - o.y1 ) ? { x: ( py * ( o.x0 - o.x1 ) - o.x0 * o.y1 + o.y0 * o.x1 ) / ( o.y0 - o.y1 ), y: py }
  34  	: { x: px, y: ( px * ( o.y0 - o.y1 ) - o.y0 * o.x1 + o.x0 * o.y1 ) / ( o.x0 - o.x1 ) };
  35  };

Closest Circle Point //JavaScript Function


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

[UPDATED CODE AND HELP CAN BE FOUND HERE]



   1  
   2  //+ Jonas Raoni Soares Silva
   3  //@ http://jsfromhell.com/math/closest-circle-point [v1.0]
   4  
   5  closestCirclePoint = function( px, py, x, y, ray ){
   6  	var tg = ( x += ray, y += ray, 0 );
   7  	return function( x, y, x0, y0 ){ return Math.sqrt( ( x -= x0 ) * x + ( y -= y0 ) * y );	}( px, py, x, y ) > ray ?
   8  		{ x: Math.cos( tg = Math.atan2( py - y, px - x ) ) * ray + x, y: Math.sin( tg ) * ray + y }
   9  		//{ x: ( px - x ) / ( length / ray ) + x, y: ( py - y ) / ( length / ray ) + y }
  10  		: { x: px, y: py };
  11  };

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]



   1  
   2  //+ Jonas Raoni Soares Silva
   3  //@ http://jsfromhell.com/math/closest-line-point [v1.0]
   4  
   5  closestLinePoint = function( px, py, x, y, angle ){
   6  	var tg = ( ( angle %= 360 ) < 0 && ( angle += 180 ), Math.tan( -angle * Math.PI / 180 ) );
   7  	return angle < 45 || angle > 135 ? { x: px, y: ( px - x ) * tg + y } : { x: ( py - y ) / tg + x, y: py };
   8  };
   9  

« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS