找到两个对象之间的距离

时间:2011-11-03 21:12:55

标签: c++ math distance

对于我的游戏,我需要在特定时间找到两个物体之间的距离。我的问题是,是否可以改进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;
}

8 个答案:

答案 0 :(得分:2)

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;
}

除了用pos(x,2)替换x*x之外,这无法进行优化并保持通用/准确,但如果您不需要通用,则可以更快:

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;
}

答案 1 :(得分:1)

您当前的方法很好,对于新的协调,只需像其他人一样添加:

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维,你有相同的计算距离的公式,但你应该添加新的维度。

答案 2 :(得分:1)

对于3D,只需向距离添加tempz

tempz = (x.z - y.z);
tempz = pow(tempz, 2.0f);
distance = tempx + tempy + tempz;

顺便说一句,“如果没有破产,请不要修理它。”无需担心改变已经有效的方法。

答案 3 :(得分:1)

我觉得一般 pow(foo,2)会慢于 foo * foo

还要问问自己,在计算中是否需要距离或square_of_distance就足够了。

例如,为了找到哪个距离更大,您可以简单地比较square_of_distances而不是实际距离本身,从而节省对 sqrt 的调用

答案 4 :(得分:0)

Pow()用于一般浮点指数,并且对于计算平方是低效的。请改用明显的乘法。

答案 5 :(得分:0)

只需添加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;
}

是你能做的最好的事情。

答案 6 :(得分:0)

如果您按值传递,可能会获得更好的效果:

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);
}

Mooing Duck的答案包含你应该知道的其他一切。

答案 7 :(得分:0)

我不会使用pow函数:

distance = tempx*tempx + tempy*tempy;

如果你需要距离进行碰撞检测,那么你可以考虑使用距离的平方,这样你就不需要调用sqrt()

但要注意“过早优化”和80-20规则(80%的运行时花费在20%的代码中),所以可能首先运行一些性能测试,但前提是你看到你花了很多时间在findDistance函数中,开始优化它。