求两个物体之间的距离
Finding the distance between two objects
对于我的游戏,我需要在特定时间找到两个物体之间的距离。我的问题很简单,findDistance函数可以改进吗?我似乎没有任何问题,我只是好奇它是否可以做得更好。同样,如果我要在3D环境中做这个,我该如何计算呢,因为还有另一个轴?
#include <math.h>
#include <iostream>
struct point{float x, y;};
float findDistance(point &x, point &y)
{
float distance, tempx, tempy;
tempx = (x.x - y.x);
tempx = pow(tempx, 2.0f);
tempy = (x.y - y.y);
tempy = pow(tempy, 2.0f);
distance = tempx + tempy;
distance = sqrt(distance);
return distance;
}
int main()
{
point a, b;
a.x = -2;
a.y = -3;
b.x = -4;
b.y = 4;
std::cout << "The distance between point x and y is: " << findDistance(a, b) << std::endl;
return 0;
}
3d:
float findDistance(point &x, point &y)
{
float distance, tempx, tempy, tempz;
tempx = (x.x - y.x);
tempx = tempx * tempx; //compiler _might_ be able to make this faster
tempy = (x.y - y.y);
tempy = tempy * tempy;
tempz = (x.z - y.z);
tempz = tempz * tempz;
distance = tempx + tempy + tempz;
distance = sqrt(distance);
return distance;
}
除了用x*x
替换pos(x,2)
,这不能优化并保持通用/准确,但如果你不需要通用,它可以更快:
bool distanceLessThan(point &x, point &y, float distance)
{
float tempx, tempy, tempz, tempd;
tempx = std::abs(x.x - y.x);
tempy = std::abs(x.y - y.y);
tempz = std::abs(x.z - y.z);
if (tempx + tempy + tempz < distance) //for small distances, don't square
return true;
if (tempx + tempy + tempz > distance/2) //for large distances, don't square
return false;
tempx = tempx * tempx;
tempy = tempy * tempy;
tempz = tempz * tempz;
tempd = distance * distance;
if (tempx + tempy + tempz < tempd)
return true;
return false;
}
对于3D,只需添加一个tempz
到距离:
tempz = (x.z - y.z);
tempz = pow(tempz, 2.0f);
distance = tempx + tempy + tempz;
顺便说一句,"如果没坏,就不要修它。"
我觉得一般情况下pow(foo, 2)会比foo * foo
慢还要问自己在计算中是否需要距离或square_of_distance是否足够。
例如,要找出哪个距离更大,您可以简单地比较square_of_distances而不是实际距离本身,从而节省了调用sqrt您当前的方法很好,对于新的协调只需像其他人一样添加它:
struct point{float x, y,z;};
float findDistance(point &x, point &y)
{
float distance, tempx, tempy;
tempx = (x.x - y.x);
tempx = pow(tempx, 2.0f);
tempy = (x.y - y.y);
tempy = pow(tempy, 2.0f);
tempz = (x.z - y.z);
tempz = pow(tempz, 2.0f);
distance = tempx + tempy + tmpz;
distance = sqrt(distance);
return distance;
}
事实上,对于n维,你有相同的计算距离的公式,但你应该添加新的维度。
Pow()用于一般浮点指数,对于计算平方是低效的。使用明显的乘法代替。
只是加上z,但你真的不能做得比这更好。你可以用x * x代替pow(x, 2),但仅此而已。如果你不需要知道距离,只需要知道两个物体是否碰撞,那么就有一些优化,但除此之外:
float findDistance(point &x, point &y)
{
float distance, tempx, tempy, tempz;
tempx = x.x - y.x;
tempy = x.y - y.y;
tempz = x.z - y.z;
distance = sqrt(tempx*tempx + tempy*tempy + tempz*tempz);
return distance;
}
是你能做的最好的。
如果通过value:传递,可能会获得更好的性能。
float findDistance(point a, point b)
{
a.x -= b.x;
a.x *= a.x;
a.y -= b.y;
a.y *= a.y
return sqrt(a.x * a.y);
}
移动鸭子的答案包含了你应该知道的一切
我不会使用pow
函数:
distance = tempx*tempx + tempy*tempy;
,如果你需要碰撞检测的距离,那么你可以考虑使用距离的平方,这样你就不需要调用sqrt()
。
但是要注意"过早优化"和80-20规则(80%的运行时花费在20%的代码上),所以也许首先运行一些性能测试,只有当你看到你在findDistance
函数上花费了大量时间时,才开始优化它。
相关文章:
- 用C++程序计算圆锥体的体积、球体的体积、八边形的面积和两点之间的距离
- 计算所有对之间的曼哈顿距离
- 如何使用发送数据包所花费的时间计算两个节点之间的距离?
- 两个有符号数字之间的距离
- 计算车辆之间的距离并设置速度,使距离保持不变,例如 5 米
- 计算两个迭代器之间的距离时"Vector Iterators Incompatible"
- 查找彼此之间具有不同最小距离的随机数
- 相同字符之间的最小距离
- 如何排列二进制字符串以最小化它们之间的距离
- 堆和堆栈之间的距离
- 树节点之间的最大距离中的运行时错误
- 最接近的字符串:在字符串阵列中找到2个字符串之间的最小距离
- 尝试查找数组中点之间的最小距离时的随机垃圾输出
- c++ 从大型数组中读取 3D 坐标并计算它们之间的距离
- 在 OpenCV 中计算 SURF 功能之间的距离
- 计算机如何分配两个变量,我们如何计算两个变量之间的距离?
- 是否有C 功能来计算两个索引之间的距离
- 使用 DirectXMath 获得 2 点之间距离的最佳方法是什么
- <random> 点之间距离最小的uniform_real_distribution
- 确定景观中所有点与具有特定属性的点之间距离的最快方法