- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
问题:在笛卡尔坐标系(仅限 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/
要求: 我们在数据库中有值 Chennai Baroda Bangalore New Delhi São Paulo, Lisboa San Jose 等... 所以我想将这些字符串转换成一个唯一的短
我需要通过 TCP 加密和发送数据(每条消息从几百字节到几百兆字节)以 block 从 Java 到 C++ 程序,并且需要发送的大小提前发送数据,以便收件人知道何时停止读取当前消息并处理它,然后等待
我不太知道如何问这个问题,因此我将其作为示例: 想象在一个应用程序中您有一个Country对象。此对象有两个属性:Name和'Bordering Countries'集合。稍后可能会添加更多属性,但这
我们正在尝试使用 IoT 中心、流分析和表存储来处理仪表板上的事件驱动图标(开/关/事件)。我无法让实际过程在 6 秒内完成。流分析的水印恒定为 5 秒。该网站声称即时处理。 https://azur
我正在尝试获取一个 JTabbedPane,其中所有选项卡(实际选项卡,而不是组件)具有相同的宽度(最宽标签所需的最小宽度或恒定宽度)。 我试图覆盖 BasicTabbedPaneUI.getTabB
你好安卓开发者, 我正在使用 OpenGLES 1.0 在 Eclipse 中为 Android 开发一个简单的游戏。我正在使用 Samsung Galaxy S2 Android(2.3) 作为开发
我正在尝试使用 ColorMatrix 为整个图像设置一个恒定的色调值。我的目标是使整个图像看起来颜色相同,而不会失去任何区域的亮度。我找到了一种使用 ColorMatrix 来移动图像的色调值的方法
我最近将我的 Postgres 数据库从 Windows 迁移到 CentOS 6.7。在 Windows 上,数据库从不使用太多 CPU,但在 Linux 上,我看到它使用恒定的 ~30% CPU(
你好,我正在用 SDL2 开发一款很棒的游戏,我添加了一个 FPS 调节器以使其在每台机器上都能流畅运行,但后来我创建了一个示例程序来输出 fps(不是调节,只是输出以查看电流的功率机器运行 ) 我在
我在 IntelliJ IDEA 13.0 中开发了一个应用程序,但是当我在我的设备上运行该应用程序时,它会不断等待调试器(如果我在 Debug模式下运行,调试器连接时没问题)但是如果远离计算机,我无
我想在 stat_密度2 图中绘制多个组,其中 alpha 值与每组中的观察计数相关。然而,由 stat_密度2d 形成的水平似乎已标准化为每组中的观察数量。例如, temp <- rbind(mov
我是一名优秀的程序员,十分优秀!