<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: closest code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 25 Jul 2008 07:26:35 GMT</pubDate>
    <description>DZone Snippets: closest code</description>
    <item>
      <title>Closest Square Point //JavaScript Function</title>
      <link>http://snippets.dzone.com/posts/show/672</link>
      <description>&lt;a href="http://jsfromhell.com/math/closest-square-point"&gt;&lt;br /&gt;Given a dot and a square, it returns the nearest dot over the square.&lt;br /&gt;&lt;br /&gt;[UPDATED CODE AND HELP CAN BE FOUND HERE]&lt;br /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;//+ Jonas Raoni Soares Silva&lt;br /&gt;//@ http://jsfromhell.com/math/closest-square-point [v1.0]&lt;br /&gt;&lt;br /&gt;closestSquarePoint = function( px, py, x, y, w, h ){&lt;br /&gt;	return { x: px &lt; x ? x : px &gt; x + w ? x + w : px, y: py &lt; y ? y : py &gt; y + h ? y + h : py };&lt;br /&gt;};&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 09 Sep 2005 04:56:15 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/672</guid>
      <author>jonasraoni (Jonas Raoni Soares Silva)</author>
    </item>
    <item>
      <title>Closest Polyline Point //Javascript Function</title>
      <link>http://snippets.dzone.com/posts/show/670</link>
      <description>&lt;a href="http://jsfromhell.com/math/closest-polyline-point"&gt;&lt;br /&gt;Given a dot and a set of lines, it returns the nearest dot over one of the given lines.&lt;br /&gt;&lt;br /&gt;[UPDATED CODE AND HELP CAN BE FOUND HERE]&lt;br /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;//+ Jonas Raoni Soares Silva&lt;br /&gt;//@ http://jsfromhell.com/math/closest-polyline-point [v1.0]&lt;br /&gt;&lt;br /&gt;closestPolyLinePoint = function( px, py, x0, y0, x1, y1, etc, etc, etc ){&lt;br /&gt;	function dotLineLength( x, y, x0, y0, x1, y1, o ){&lt;br /&gt;		function lineLength( x, y, x0, y0 ){&lt;br /&gt;			return Math.sqrt( ( x -= x0 ) * x + ( y -= y0 ) * y );&lt;br /&gt;		}&lt;br /&gt;		if( o &amp;&amp; !( o = function( x, y, x0, y0, x1, y1 ){&lt;br /&gt;			if( !( x1 - x0 ) ) return { x: x0, y: y };&lt;br /&gt;			else if( !( y1 - y0 ) ) return { x: x, y: y0 };&lt;br /&gt;			var left, tg = -1 / ( ( y1 - y0 ) / ( x1 - x0 ) );&lt;br /&gt;			return { x: left = ( x1 * ( x * tg - y + y0 ) + x0 * ( x * - tg + y - y1 ) ) / ( tg * ( x1 - x0 ) + y0 - y1 ), y: tg * left - tg * x + y };&lt;br /&gt;		}( x, y, x0, y0, x1, y1 ), o.x &gt;= Math.min( x0, x1 ) &amp;&amp; o.x &lt;= Math.max( x0, x1 ) &amp;&amp; o.y &gt;= Math.min( y0, y1 ) &amp;&amp; o.y &lt;= Math.max( y0, y1 ) ) ){&lt;br /&gt;			var l1 = lineLength( x, y, x0, y0 ), l2 = lineLength( x, y, x1, y1 );&lt;br /&gt;			return l1 &gt; l2 ? l2 : l1;&lt;br /&gt;		}&lt;br /&gt;		else {&lt;br /&gt;			var a = y0 - y1, b = x1 - x0, c = x0 * y1 - y0 * x1;&lt;br /&gt;			return Math.abs( a * x + b * y + c ) / Math.sqrt( a * a + b * b );&lt;br /&gt;		}&lt;br /&gt;	};&lt;br /&gt;	for( var args = [].slice.call( arguments, 0 ), lines = []; args.length &gt; 4; lines[lines.length] = { y1: args.pop(), x1: args.pop(), y0: args.pop(), x0: args.pop() } );&lt;br /&gt;	if( !lines.length )&lt;br /&gt;		return { x: px, y: py };&lt;br /&gt;	var l, i, o = lines[0], lower = { i: 0, l: dotLineLength( px, py, o.x0, o.y0, o.x1, o.y1 ) };&lt;br /&gt;	for( i in lines )&lt;br /&gt;		if( lower.l &gt; ( l = dotLineLength( px, py, ( o = lines[i] ).x0, o.y0, o.x1, o.y1 ) ) )&lt;br /&gt;			lower = { i: i, l: l };&lt;br /&gt;	py = py &lt; Math.min( ( o = lines[lower.i] ).y0, o.y1 ) ? Math.min( o.y0, o.y1 ) : py &gt; Math.max( o.y0, o.y1 ) ? Math.max( o.y0, o.y1 ) : py;&lt;br /&gt;	px = px &lt; Math.min( o.x0, o.x1 ) ? Math.min( o.x0, o.x1 ) : px &gt; Math.max( o.x0, o.x1 ) ? Math.max( o.x0, o.x1 ) : px;&lt;br /&gt;	return Math.abs( o.x0 - o.x1 ) &lt; 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 }&lt;br /&gt;	: { x: px, y: ( px * ( o.y0 - o.y1 ) - o.y0 * o.x1 + o.x0 * o.y1 ) / ( o.x0 - o.x1 ) };&lt;br /&gt;};&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 09 Sep 2005 04:40:06 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/670</guid>
      <author>jonasraoni (Jonas Raoni Soares Silva)</author>
    </item>
    <item>
      <title>Closest Circle Point //JavaScript Function</title>
      <link>http://snippets.dzone.com/posts/show/669</link>
      <description>&lt;a href="http://jsfromhell.com/math/closest-circle-point"&gt;&lt;br /&gt;Given a dot and a circle, it returns the nearest dot over the circle.&lt;br /&gt;&lt;br /&gt;[UPDATED CODE AND HELP CAN BE FOUND HERE]&lt;br /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;//+ Jonas Raoni Soares Silva&lt;br /&gt;//@ http://jsfromhell.com/math/closest-circle-point [v1.0]&lt;br /&gt;&lt;br /&gt;closestCirclePoint = function( px, py, x, y, ray ){&lt;br /&gt;	var tg = ( x += ray, y += ray, 0 );&lt;br /&gt;	return function( x, y, x0, y0 ){ return Math.sqrt( ( x -= x0 ) * x + ( y -= y0 ) * y );	}( px, py, x, y ) &gt; ray ?&lt;br /&gt;		{ x: Math.cos( tg = Math.atan2( py - y, px - x ) ) * ray + x, y: Math.sin( tg ) * ray + y }&lt;br /&gt;		//{ x: ( px - x ) / ( length / ray ) + x, y: ( py - y ) / ( length / ray ) + y }&lt;br /&gt;		: { x: px, y: py };&lt;br /&gt;};&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 09 Sep 2005 04:36:55 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/669</guid>
      <author>jonasraoni (Jonas Raoni Soares Silva)</author>
    </item>
    <item>
      <title>Closest Line Point //JavaScript Function</title>
      <link>http://snippets.dzone.com/posts/show/668</link>
      <description>&lt;a href="http://jsfromhell.com/math/closest-line-point"&gt;&lt;br /&gt;Given a dot and a line, it returns the nearest dot over the line.&lt;br /&gt;&lt;br /&gt;[UPDATED CODE AND HELP CAN BE FOUND HERE]&lt;br /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;//+ Jonas Raoni Soares Silva&lt;br /&gt;//@ http://jsfromhell.com/math/closest-line-point [v1.0]&lt;br /&gt;&lt;br /&gt;closestLinePoint = function( px, py, x, y, angle ){&lt;br /&gt;	var tg = ( ( angle %= 360 ) &lt; 0 &amp;&amp; ( angle += 180 ), Math.tan( -angle * Math.PI / 180 ) );&lt;br /&gt;	return angle &lt; 45 || angle &gt; 135 ? { x: px, y: ( px - x ) * tg + y } : { x: ( py - y ) / tg + x, y: py };&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Fri, 09 Sep 2005 04:35:05 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/668</guid>
      <author>jonasraoni (Jonas Raoni Soares Silva)</author>
    </item>
  </channel>
</rss>
