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

12,320

Solution 1

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++).

Solution 2

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

Share:
12,320
Barış Uşaklı
Author by

Barış Uşaklı

Talks to computers all day. Co-Founder of http://nodebb.org GitHub | Twitter

Updated on June 04, 2022

Comments

  • Barış Uşaklı
    Barış Uşaklı almost 2 years

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