找到最快的方式2点之间的距离

编程入门 行业动态 更新时间:2024-10-25 08:17:41
本文介绍了找到最快的方式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点之间的距离

本文发布于:2023-07-27 23:15:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1225222.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:最快   距离   方式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!