Reputation:
For my game, I need to find the distance between two objects at certain times. My question is simply can the findDistance function be improved? I don't seem to have any issues, I'm only curious if it could have been done better. Also, if I were to do this in a 3D setting how would I go about calculating it since there is another axis?
#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;
}
Upvotes: 1
Views: 5732
Reputation: 22555
Your current approach is fine and for new coordination just add it like others:
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;
}
In fact for n dimensions, you have the same formula for calculating distance, but you should add new dimensions.
Upvotes: 1
Reputation: 66912
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;
}
Other than replacing pos(x,2)
with x*x
, this cannot be optimized and remain generic/accurate, but if you don't need generic, it can be faster:
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;
}
Upvotes: 2
Reputation: 2935
I would not use the pow
function:
distance = tempx*tempx + tempy*tempy;
and if you need the distance for collision detection, then you might consider using a square of the distances so that you don't need to call sqrt()
.
But be aware of "premature optimization" and the 80-20 rule (80% of runtime is spent in 20% of the code), so maybe first run some performance tests and only if you see that you spend lots of time in the findDistance
function, start to optimize it.
Upvotes: 0
Reputation: 53017
You might get better performance if you pass by 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);
}
Mooing Duck's answer contains everything else you should know.
Upvotes: 0
Reputation: 9446
Just add the z, but you can't really do much better than this. You can replace the pow(x, 2) with x * x, but that's about it. If you don't need to know the distance, just whether two objects have collided then there are some optimizations, but otherwise:
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;
}
Is about the best you can do.
Upvotes: 0
Reputation: 13786
Pow() is for general floating point exponents, and is inefficient for computing squares. Use the obvious multiplication instead.
Upvotes: 0
Reputation: 24403
I feel in general pow( foo , 2 ) will be slower than foo * foo
Also ask yourself if in your calculations you need the distance or square_of_distance is sufficient.
For example to find which distance is greater you can simply compare square_of_distances instead of the the actual distance itself thereby saving a call to sqrt
Upvotes: 1
Reputation: 32878
For 3D, just add a tempz
to the distance:
tempz = (x.z - y.z);
tempz = pow(tempz, 2.0f);
distance = tempx + tempy + tempz;
By the way, "if it ain't broke, don't fix it." No need to worry about changing what already works.
Upvotes: 1