<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: The fastest way to find the distance between two points</title>
	<atom:link href="http://www.emanueleferonato.com/2010/07/29/the-fastest-way-to-find-the-distance-between-two-points/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.emanueleferonato.com/2010/07/29/the-fastest-way-to-find-the-distance-between-two-points/</link>
	<description>italian geek and PROgrammer</description>
	<lastBuildDate>Sat, 11 Feb 2012 05:27:38 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
	<item>
		<title>By: Anthony Tambrin</title>
		<link>http://www.emanueleferonato.com/2010/07/29/the-fastest-way-to-find-the-distance-between-two-points/#comment-835771</link>
		<dc:creator>Anthony Tambrin</dc:creator>
		<pubDate>Tue, 17 May 2011 02:15:53 +0000</pubDate>
		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=3187#comment-835771</guid>
		<description>Would it yield faster result if we use negation instead of multiplication?

if($x_dist&lt;0){
	$x_dist = -$x_dist;
}
if($y_dist&lt;0){
	$y_dist = -$y_dist;
}
$distance = $x_dist+$y_dist;</description>
		<content:encoded><![CDATA[<p>Would it yield faster result if we use negation instead of multiplication?</p>
<p>if($x_dist&lt;0){<br />
	$x_dist = -$x_dist;<br />
}<br />
if($y_dist&lt;0){<br />
	$y_dist = -$y_dist;<br />
}<br />
$distance = $x_dist+$y_dist;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chris Churn &#124; Website and application development</title>
		<link>http://www.emanueleferonato.com/2010/07/29/the-fastest-way-to-find-the-distance-between-two-points/#comment-679685</link>
		<dc:creator>Chris Churn &#124; Website and application development</dc:creator>
		<pubDate>Tue, 16 Nov 2010 23:46:07 +0000</pubDate>
		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=3187#comment-679685</guid>
		<description>[...] out the distance, I&#8217;ve used the Euclidean geometry method as described on this great article: http://www.emanueleferonato.com/2010/07/29/the-fastest-way-to-find-the-distance-between-two-points/ This is the function; p1 is the tower x,y. p2 is the mouse (we&#8217;ve got no enemies yet) and [...]</description>
		<content:encoded><![CDATA[<p>[...] out the distance, I&#8217;ve used the Euclidean geometry method as described on this great article: <a href="http://www.emanueleferonato.com/2010/07/29/the-fastest-way-to-find-the-distance-between-two-points/" rel="nofollow">http://www.emanueleferonato.com/2010/07/29/the-fastest-way-to-find-the-distance-between-two-points/</a> This is the function; p1 is the tower x,y. p2 is the mouse (we&#8217;ve got no enemies yet) and [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Justin</title>
		<link>http://www.emanueleferonato.com/2010/07/29/the-fastest-way-to-find-the-distance-between-two-points/#comment-628198</link>
		<dc:creator>Justin</dc:creator>
		<pubDate>Thu, 26 Aug 2010 12:46:50 +0000</pubDate>
		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=3187#comment-628198</guid>
		<description>You failed math due to they are not match result for distance answer.

if($x_dist&lt;0){
	$x_dist *=-1;
}
if($y_dist&lt;0){
	$y_dist *=-1;
}
$distance = $x_dist+$y_dist;</description>
		<content:encoded><![CDATA[<p>You failed math due to they are not match result for distance answer.</p>
<p>if($x_dist&lt;0){<br />
	$x_dist *=-1;<br />
}<br />
if($y_dist&lt;0){<br />
	$y_dist *=-1;<br />
}<br />
$distance = $x_dist+$y_dist;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bokan</title>
		<link>http://www.emanueleferonato.com/2010/07/29/the-fastest-way-to-find-the-distance-between-two-points/#comment-624170</link>
		<dc:creator>Bokan</dc:creator>
		<pubDate>Tue, 17 Aug 2010 14:42:32 +0000</pubDate>
		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=3187#comment-624170</guid>
		<description>&quot;It require a conditional JMP and a multiplication where you just need to set the sign bit of the variable $x_dist to positive.&quot; &gt; I&#039;m wrong for this. Changing sign is not that simple, we must add one unit if the number was negative.
But since we are working on a not accurate algorythm one unit won&#039;t make a big difference.</description>
		<content:encoded><![CDATA[<p>&#8220;It require a conditional JMP and a multiplication where you just need to set the sign bit of the variable $x_dist to positive.&#8221; &gt; I&#8217;m wrong for this. Changing sign is not that simple, we must add one unit if the number was negative.<br />
But since we are working on a not accurate algorythm one unit won&#8217;t make a big difference.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bokan</title>
		<link>http://www.emanueleferonato.com/2010/07/29/the-fastest-way-to-find-the-distance-between-two-points/#comment-624153</link>
		<dc:creator>Bokan</dc:creator>
		<pubDate>Tue, 17 Aug 2010 14:10:35 +0000</pubDate>
		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=3187#comment-624153</guid>
		<description>There is no &quot;myth busted&quot; here. This kind of optimization are for compiled language. This benchmark means nothing when done with PHP because most of the CPU cycles are used to parse the code and not to compute the result.

Creating a fast PHP code is more a matter of knowing how PHP translate the code, seek for variables in his dictionary, reserve and release memory...

It would be interesting to run your PHP tests under HIP HOP PHP or APC cache.

If you write the code in ASM and do it right, computing manathan distance is times faster than just parsing PHP version of the code.

To finish this kind of code is just bad
if($x_dist&lt;0){
	$x_dist *=-1;
}
It require a conditional JMP and a multiplication where you just need to set the sign bit of the variable $x_dist to positive.</description>
		<content:encoded><![CDATA[<p>There is no &#8220;myth busted&#8221; here. This kind of optimization are for compiled language. This benchmark means nothing when done with PHP because most of the CPU cycles are used to parse the code and not to compute the result.</p>
<p>Creating a fast PHP code is more a matter of knowing how PHP translate the code, seek for variables in his dictionary, reserve and release memory&#8230;</p>
<p>It would be interesting to run your PHP tests under HIP HOP PHP or APC cache.</p>
<p>If you write the code in ASM and do it right, computing manathan distance is times faster than just parsing PHP version of the code.</p>
<p>To finish this kind of code is just bad<br />
if($x_dist&lt;0){<br />
	$x_dist *=-1;<br />
}<br />
It require a conditional JMP and a multiplication where you just need to set the sign bit of the variable $x_dist to positive.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Astro75</title>
		<link>http://www.emanueleferonato.com/2010/07/29/the-fastest-way-to-find-the-distance-between-two-points/#comment-613779</link>
		<dc:creator>Astro75</dc:creator>
		<pubDate>Sun, 01 Aug 2010 08:31:56 +0000</pubDate>
		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=3187#comment-613779</guid>
		<description>Actually you don&#039;t need 3 multiplications. Only 2 is enough, because you can store the value needed for distance comparison.</description>
		<content:encoded><![CDATA[<p>Actually you don&#8217;t need 3 multiplications. Only 2 is enough, because you can store the value needed for distance comparison.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kaustav</title>
		<link>http://www.emanueleferonato.com/2010/07/29/the-fastest-way-to-find-the-distance-between-two-points/#comment-613665</link>
		<dc:creator>Kaustav</dc:creator>
		<pubDate>Sun, 01 Aug 2010 04:06:49 +0000</pubDate>
		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=3187#comment-613665</guid>
		<description>I think you are right. Finding distance for ranges of fire like the one used in TD games doesn&#039;t need to be accurate. So taxicab can be used. Otherwise when you need collisions euler MUST be used. The question is optimizing both to be as fast as possible.

Emanuele&#039;s method of squaring the distance you want instead of sqrting the measured distance is the fastest IMO (only 3 multiplications and one addition)

With collision response, you can either calculate response with vectors or trig. Trig is just wayyy too cpu expensive, so most people use vectors.

When you use vectors, to calculate the unit vector, you NEED the magnitude (distance), not the magnitude squared. So i tried to find a fast way to calculate the sqrt.

I got this. Its the newton method; very fast and the number of significant figures DOUBLES with every loop.

function sqrt(n)

a = guess

for (accuracy) {
   a = 0.5(a + (n/a))
}

return a

Generally, the precompiled Math.sqrt is faster than this for general squareroots, since this is the method it uses =)

But, if you have a tile-based game, or broadphase quadtrees, or grids, the ability to guess and determine accuracy really lowers CPU load. Thats because the computer automatically guesses 1, but since you know the squares apart, you can give a much better guess. Also, you generally don&#039;t need 16bit precision. 3 loops on this will give 8 significant figures, (with a good guess), which is more than anyone needs.

Try it!</description>
		<content:encoded><![CDATA[<p>I think you are right. Finding distance for ranges of fire like the one used in TD games doesn&#8217;t need to be accurate. So taxicab can be used. Otherwise when you need collisions euler MUST be used. The question is optimizing both to be as fast as possible.</p>
<p>Emanuele&#8217;s method of squaring the distance you want instead of sqrting the measured distance is the fastest IMO (only 3 multiplications and one addition)</p>
<p>With collision response, you can either calculate response with vectors or trig. Trig is just wayyy too cpu expensive, so most people use vectors.</p>
<p>When you use vectors, to calculate the unit vector, you NEED the magnitude (distance), not the magnitude squared. So i tried to find a fast way to calculate the sqrt.</p>
<p>I got this. Its the newton method; very fast and the number of significant figures DOUBLES with every loop.</p>
<p>function sqrt(n)</p>
<p>a = guess</p>
<p>for (accuracy) {<br />
   a = 0.5(a + (n/a))<br />
}</p>
<p>return a</p>
<p>Generally, the precompiled Math.sqrt is faster than this for general squareroots, since this is the method it uses =)</p>
<p>But, if you have a tile-based game, or broadphase quadtrees, or grids, the ability to guess and determine accuracy really lowers CPU load. Thats because the computer automatically guesses 1, but since you know the squares apart, you can give a much better guess. Also, you generally don&#8217;t need 16bit precision. 3 loops on this will give 8 significant figures, (with a good guess), which is more than anyone needs.</p>
<p>Try it!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Martin</title>
		<link>http://www.emanueleferonato.com/2010/07/29/the-fastest-way-to-find-the-distance-between-two-points/#comment-612741</link>
		<dc:creator>Martin</dc:creator>
		<pubDate>Fri, 30 Jul 2010 23:21:31 +0000</pubDate>
		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=3187#comment-612741</guid>
		<description>Sorry, but I think it is not a question of speed, but a question what kind of result you like to get.

For tile-based games the Taxicab is the right one, if you like to get the real distance you need the euclidean-algo.

Maybe I´m wrong, because I can´t believe that you all ignore the fact, that you can´t compare them.</description>
		<content:encoded><![CDATA[<p>Sorry, but I think it is not a question of speed, but a question what kind of result you like to get.</p>
<p>For tile-based games the Taxicab is the right one, if you like to get the real distance you need the euclidean-algo.</p>
<p>Maybe I´m wrong, because I can´t believe that you all ignore the fact, that you can´t compare them.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: xPy</title>
		<link>http://www.emanueleferonato.com/2010/07/29/the-fastest-way-to-find-the-distance-between-two-points/#comment-612309</link>
		<dc:creator>xPy</dc:creator>
		<pubDate>Fri, 30 Jul 2010 09:13:36 +0000</pubDate>
		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=3187#comment-612309</guid>
		<description>I think that the execution times of sqrt() and abs are compiler dependent. That may means that the fastest method is also compiler dependent.
While experimenting I found that this is the fastest way:

if($valx*$valy&lt;0)
{
$distance=$valx-$valy;	
}
else
{
$distance=$valx+$valy;	
}

tested on php and actionscript.
The result may be a positive or negative number but that is not a problem since you can check your distance like (-$dist&lt;$distance&lt;$dist).</description>
		<content:encoded><![CDATA[<p>I think that the execution times of sqrt() and abs are compiler dependent. That may means that the fastest method is also compiler dependent.<br />
While experimenting I found that this is the fastest way:</p>
<p>if($valx*$valy&lt;0)<br />
{<br />
$distance=$valx-$valy;<br />
}<br />
else<br />
{<br />
$distance=$valx+$valy;<br />
}</p>
<p>tested on php and actionscript.<br />
The result may be a positive or negative number but that is not a problem since you can check your distance like (-$dist&lt;$distance&lt;$dist).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: AL</title>
		<link>http://www.emanueleferonato.com/2010/07/29/the-fastest-way-to-find-the-distance-between-two-points/#comment-611936</link>
		<dc:creator>AL</dc:creator>
		<pubDate>Thu, 29 Jul 2010 21:05:57 +0000</pubDate>
		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=3187#comment-611936</guid>
		<description>I notice that Emanuele used PHP to show that Euclidean is faster than Manhattan, while the other scripts proving that Manhattan is faster than Euclidean are written using AS3.

Could this difference be an issue based on how these two languages are compiled?</description>
		<content:encoded><![CDATA[<p>I notice that Emanuele used PHP to show that Euclidean is faster than Manhattan, while the other scripts proving that Manhattan is faster than Euclidean are written using AS3.</p>
<p>Could this difference be an issue based on how these two languages are compiled?</p>
]]></content:encoded>
	</item>
</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk: enhanced
Database Caching 6/11 queries in 0.008 seconds using disk: basic

Served from: www.emanueleferonato.com @ 2012-02-11 13:50:45 -->
