找到最快的方式2点之间的距离 [英] Find distance between 2 points in fastest way

查看:87
本文介绍了找到最快的方式2点之间的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这code。通过使用距离公式,的Math.sqrt计算2点之间的距离( - X2)^ 2 +(Y - (X1 Y2)^ 2)。我的第一点具有 MMX MMY 协调和第二个具有 OY 协调。我的问题很简单,没​​有任何的更快办法计算吗?

This code calculates the distance between 2 points by using distance formula, Math.sqrt ( (x1 – x2)^2 + (y1 – y2) ^2). My first point has mmx and mmy coordination and second one has ox and oy coordination. My question is simple, is there any FASTER way for calculate this?

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

这是我的code,感谢您的帮助。

This is my code, Thanks for help.

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 =新的点(X1,Y1); 设置起点。我不能使用,因为这样的应用程序调用父类mouseX和mouseY的,所以我只能通过x和y,以我的循环。

Also: pointO = new Point(x1,y1); set start point. I can not use mouseX and mouseY because of the way that the application is called by parent class, so I can just pass x and y to my loop.

推荐答案

调用静态函数是有点贵。您可以保存开销这样做:

Calling a static function is a bit expensive. You can save that overhead by doing this:

private var sqrtFunc = Math.sqrt;

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

这篇关于找到最快的方式2点之间的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆