Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to get distance between 2 points with DirectXMath

Using the new XMVECTOR and XMFLOAT3 classes what is the best way to get the distance between 2 points? I couldn't find a function that does it in XMVector* family of functions so I came up with the following :

float distance(const XMFLOAT3& v1,const XMFLOAT3& v2)
{
    XMVECTOR vector1 = XMLoadFloat3(&v1);
    XMVECTOR vector2 = XMLoadFloat3(&v2);
    XMVECTOR vectorSub = XMVectorSubtract(vector1,vector2);
    XMVECTOR length = XMVector3Length(vectorSub);

    float distance = 0.0f;
    XMStoreFloat(&distance,length);
    return distance;
}

Will this be faster than a normal Vector3 class with just 3 floats for x,y,z and then using sqrt because it uses intrinsic optimizations? Namely :

float Distance(const Vector3& point1,const Vector3& point2)
{
    float distance = sqrt( (point1.x - point2.x) * (point1.x - point2.x) +
                            (point1.y - point2.y) * (point1.y - point2.y) +
                            (point1.z - point2.z) * (point1.z - point2.z) );
    return distance;
}
like image 867
Barış Uşaklı Avatar asked Apr 24 '12 04:04

Barış Uşaklı


People also ask

How do you find distance on a graph?

When dealing with horizontal lines, the length of the line is simply the difference between the two points' x-coordinates. In a similar fashion, a vertical line's length can be found by subtracting one of the y-coordinates with the other.


2 Answers

There's only one way to get distance between points. And that's the way you described. vec3 diff = b - a; float distance = sqrtf(dot(diff, diff));

Will this be faster than a normal Vector3 class with just 3 floats for x,y,z and then using sqrt because it uses intrinsic optimizations?

If you're worried about performance, you need to profile your application, instead of trying to guess what will be faster. There's a very good chance that impact of of "less efficient" solution will be completely unnoticeable in your application.

There's also a very good chance that if you start writing routines yourself, you'll make them less efficient or introduce bugs. For example your "hand-written" code might (depending on compiler) perform more floating point calculations than original code that uses "XMVECTOR". In your routine you calculate difference between vectors twice. If compiler doesn't optimize that out, you'll end up with 6 subtractions, 2 additions, 1 sqrtf, and 3 multiplications. In "XMVECTOR" code you'll use 3 subtractions, 2 additions, 3 multiplications, and 1 sqrtf.

For profiling applications you can use AQTime 7 Standard (free at the moment), or gprof (if you're compiling with gcc/g++).

like image 178
SigTerm Avatar answered Oct 26 '22 22:10

SigTerm


D3DXVec3Length(&(Point1-Point2)) is equivalent to the distance formula.

like image 44
user3625178 Avatar answered Oct 26 '22 22:10

user3625178