performance - 以最快的方式查找两点之间的距离

标签 performance flash actionscript-3 math distance

此代码使用距离公式Math.sqrt ( (x1 – x2)^2 + (y1 – y2) ^2) 计算两点之间的距离。我的第一点是 mmxmmy 协调,第二点是 oxoy 协调。我的问题很简单,有没有更快的计算方法?

private function dist(mmx:int, mmy:int, ox:int, oy:int):Number{
  return Math.sqrt((mmx-ox)*(mmx-ox)+(mmy-oy)*(mmy-oy));
}

这是我的代码,感谢您的帮助。

public function moveIT(Xmouse, Ymouse):void{
            f = Point.distance( new Point( Xmouse, Ymouse ), new Point( mainSP.x, mainSP.y ) );// distance between mouse and instance 
            distancePro = Point.distance( pointO, new Point( mainSP.x, mainSP.y ) );// distance from start point 
            if (  f < strtSen ){ // move forward
                tt.stop(); tt.reset(); // delay timer on destination    
                mF = true;  mB = false;
                ag = Math.atan2((Ymouse - mainSP.y),(Xmouse - mainSP.x)); // move-forward angle, between mouse and instance
            }
            if (mF){ /// shoot loop
                if (f > 5){// 5 pixel
                    mainSP.x -= Math.round( (400 /f) + .5 ) * Math.cos(ag);
                    mainSP.y -= Math.round( (400 /f) + .5 ) * Math.sin(ag);
                }
                if (  distancePro > backSen ){// (backSen = max distance)
                    mF = false;         
                    tt.start();// delay timer on destination
                }
            }
            if (mB){ /// return loop
                if ( distancePro < 24 ){//  back angle re-calculation
                    agBACK = Math.atan2((y1 - mainSP.y),(x1 - mainSP.x));                   
                }
                mainSP.x += (Math.cos(agBACK) * rturnSpeed);
                mainSP.y += (Math.sin(agBACK) * rturnSpeed);
                if ( distancePro < 4 ){ // fix position to start point (x1,y1)
                    mB = false;
                    mainSP.x = x1; mainSP.y = y1;
                }
            }
        }
private function scTimer(evt:TimerEvent):void {// timer
            tt.stop();
            agBACK = Math.atan2((y1 - mainSP.y),(x1 - mainSP.x));// move-back angle between start point and instance
            mB = true;
        }

另外:pointO = new Point(x1,y1); 设置起点。由于父类调用应用程序的方式,我不能使用 mouseX 和 mouseY,所以我只能将 x 和 y 传递到我的循环。

最佳答案

我认为如果您内联您的函数而不是进行实际的函数调用,这是最快的方法。

f = Math.sqrt((Xmouse-mainSP.x)*(Xmouse-mainSP.x)+(Ymouse-mainSP.y)*(Ymouse-mainSP.y)); 
distancePro = Math.sqrt((x1-mainSP.x)*(x1-mainSP.x)+(y1-mainSP.y)*(y1-mainSP.y));

使用 Point.distance 可读性更高,但速度要慢几倍。如果你想要速度,你想直接内联你的数学。

关于performance - 以最快的方式查找两点之间的距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7508240/

相关文章:

apache-flex - Actionscript 的 ExternalInterface.addCallback 只能在本地工作,不能在生产中使用

flash - 优化 Actionscript 3 中的碰撞检测。

actionscript-3 - 如何将参数传递给 flex/actionscript 中的事件监听器函数?

performance - 哪个更有效,atan2 或 sqrt?

c# - ASP.NET 中的资源(JS、CSS)压缩、缩小和版本控制

css - .something :first-child as a selector? 的性能

javascript - 如何使用 SWFObject 检测闪光灯

javascript - Jasmine 测试,如何提高性能?

javascript - 通过 JQuery Ajax 加载 ZeroClipboard 或 ZClip(复制到剪贴板按钮)

actionscript-3 - 缓存具有多个参数的方法结果的最佳方法 - 对象作为字典中的键?