[Papervision3D] Spatial distance between points... and optimizing Sqrt?

John Grden neoriley at gmail.com
Fri Aug 24 19:18:06 PDT 2007


LOL Jim YOU ROCK

I love this stuff man - we need a t-shirt ;)



On 8/24/07, Jim Morford <vistainteractive at gmail.com> wrote:
>
> John's algorithm for calculating distance is correct (I've changed the
> variables so they have a "delt" in front of them):
>
>      var deltX :Number = this.x - obj.x;
>      var deltY :Number = this.y - obj.y;
>      var deltZ :Number = this.z - obj.z;
>
>        return Math.sqrt( deltX*deltX + deltY*deltY + deltZ*deltZ );
>
> but there are a few things you can do that can speed things up in certain
> special cases that sometimes come up:
>
> If the points are co-planar with one of the primary axis planes (XY, YZ,
> or XZ) or nearly so - that is, one of the delta measurements (deltX, deltY
> or deltZ) is zero or it's absolute value is less than some low threshold,
> then you can drop that small measurement from the calculation and get the
> same result.  For example, suppose deltX is less than 0.001, say, 0.0003...
> then
>
>        return Math.sqrt( deltY*deltY + deltZ*deltZ );
>
> will give the same result out to seven decimals - pretty darn accurate.
>
> Better yet, if the points are nearly co-linear with one of the primary
> axes (X, Y, or Z) - meaning that TWO of the delta measurements are near
> enough zero - then the Sqrt calculation can be skipped entirely!  Using the
> above example, if deltZ is, say, - 0.00027... then
>
>         return deltY;
>
> will also be as accurate as above.
>
> Patrick's comment "*sqrt is an expensive operation" furrowed my already
> wrinkled brow.  In this day and age, why should Math.sqrt be an expensive
> operation?  Way way back in the days when "four-banger" was geekspeak for a
> calculator with no "advanced" functions (like square root), we relied on an
> old recurive algorithm to get solutions more accurate than the other options
> (slide rules and CRC tables, which were faster).  [Does mean anything to
> anyone, or have I become that old a fart?]
>
> So, being in the speed testing mode that I am (and being a speed freak),
> I compared the algorithm to Math.sqrt().
>
> OMG.  I've posted the results to the optimization blog - I can't believe
> SQRT is THAT slow.
>
>
> _______________________________________________
> Papervision3D mailing list
> Papervision3D at osflash.org
> http://osflash.org/mailman/listinfo/papervision3d_osflash.org
>
>


-- 
[  JPG  ]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://osflash.org/pipermail/papervision3d_osflash.org/attachments/20070824/2f0342f8/attachment.html 


More information about the Papervision3D mailing list