gpt4 book ai didi

java - 一种以恒定速度沿直线移动鸵鸟的有效算法

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:25:38 27 4
gpt4 key购买 nike

问题:在笛卡尔坐标系(仅限 x,y)中以恒定速度沿直线移动物体。更新速度不稳定。移动速度必须接近精确,并且物体必须非常接近目的地。线路的起点和终点可能在任何地方。

给定:源地址和目标地址 (x0,x1,y0, y1),以及任意值的速度。

旁白:SO 上有一个关于此的答案,这很好,但它假定给出了旅行的总时间。

这是我得到的:

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
<br/>//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
}

此代码可以完成工作,但在 CPU 密集型程序中它仅占用 18% 的程序时间。无论是编程方式还是性能方面,它都是垃圾。关于在这里做什么的任何想法?

最佳答案

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.

救援的基础物理学

旅行总时间=距离/速度

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

虽然确实是 while 循环你应该重构掉

关于java - 一种以恒定速度沿直线移动鸵鸟的有效算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6287397/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com