一个有效的算法来移动鸵鸟沿一条线以恒定的速度 [英] An efficient algorithm to move ostrichs along a line at a constant speed

查看:198
本文介绍了一个有效的算法来移动鸵鸟沿一条线以恒定的速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:移动沿着直线处的对象在笛卡尔恒定速度坐标系(X,Y仅)。更新的速率是不稳定的。移动速度必须接近准确,目标要到非常接近目标。该生产线的来源和目的地可能是任何地方。

鉴于:在源地址和目的地址(X0,X1,Y0,Y1),和的任意值的速度

这是asside:有对SO对此的答案,这是很好的,但它$的总花费的时间旅行是给定p $ psumes。

下面就是我得到的答案:

X0 = 127;
Y0 = 127;
X1 = 257;
Y1 = 188;
速度= 127;
ostrich.x = X //加上沿线有一段距离;
ostrich.y = Y0 //加上沿线有一段距离;
//一个任意大的值,使得每次迭代递增的距离微量
SPEED_VAR = 1000;
xDistPerIteration =(X1  -  X0)/ SPEED_VAR;
yDistPerIteration =(Y1  -  Y0)/ SPEED_VAR;
distanceToTravel =; //毕达哥拉斯theorum
limitX =极限1 = 0; //决定何时停止while循环

//被调用每秒40-60次 无效更新(){   //不断递增鸵鸟的位置   而(limitX<速度和放大器;&安培; limitY<速度){     limitX + = Math.abs(xDistPerIteration);     limitY + = Math.abs(yDistPerIteration);     ostrich.x + = xDistPerIteration;     ostrich.y + = yDistPerIteration;   }   distanceTraveled - =的Math.sqrt(Math.pow(limitX,2)+ Math.pow(limitY,2));   如果(distanceTraveled&所述; = 0)     //鸵鸟在出厂时安全抵达 }

这code能够完成任务,但是它占用专用18%的节目时间在CPU密集型程序。这是垃圾,编程和在性能方面。什么任何想法在这里做?

解决方案
  

这是asside:有一个答案的   SO对此,并且它的好,   然而,$道达尔时刻p $ psumes   花行给出。

基础物理救援

花费的总时间旅行=距离/速度

顺便说一句 Math.hypot(limitX,limitY)的Math.sqrt(Math.pow(limitX较快,2)+ Math.pow (limitY,2))

不过说实在的,虽然循环,你应该重构了

Problem: move an object along a straight line at a constant speed in the Cartesian coordinate system (x,y only). The rate of update is unstable. The movement speed must be close to exact and the object must arrive very close to the destination. The line's source and destination may be anywhere.

Given: the source and destination addresses (x0,x1,y0, y1), and a speed of arbitrary value.

An asside: There is an answer on the SO regarding this, and it's good, however it presumes that total time spend traveling is given.

Here's what I've got:

x0 = 127;
y0 = 127;
x1 = 257;
y1 = 188;
speed = 127;
ostrich.x=x0 //plus some distance along the line;
ostrich.y=y0 // plus some distance along the line;
//An arbitrarily large value so that each iteration increments the distance a minute amount
SPEED_VAR = 1000; 
xDistPerIteration = (x1 - x0) / SPEED_VAR;
yDistPerIteration = (y1 - y0) / SPEED_VAR;
distanceToTravel = ;//Pythagorean theorum
limitX =  limit1 = 0; //determines when to stop the while loop

//get called 40-60 times per second void update(){ //Keep incrementing the ostrich' location while (limitX < speed && limitY < speed) { limitX += Math.abs(xDistPerIteration); limitY += Math.abs(yDistPerIteration); ostrich.x += xDistPerIteration; ostrich.y += yDistPerIteration; } distanceTraveled -= Math.sqrt(Math.pow(limitX, 2) + Math.pow(limitY, 2)); if (distanceTraveled <=0) //ostrich arrived safely at the factory }

This code gets the job done, however it takes up exclusively 18% of program time in a CPU intensive program. It's garbage, programatically and in terms of performance. Any ideas on what to do here?

解决方案

An asside: There is an answer on the SO regarding this, and it's good, however it presumes that total time spend traveling is given.

basic physics to the rescue

total time spent traveling = distance/speed

btw Math.hypot(limitX,limitY) is faster than Math.sqrt(Math.pow(limitX, 2) + Math.pow(limitY, 2))

though really it's that while loop you should refactor out

这篇关于一个有效的算法来移动鸵鸟沿一条线以恒定的速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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