Home > Back-end >  Direction interpolation
Direction interpolation

Time:11-16

enter image description here

I have tree vector A, B, and D. Is there a way to determinate how close vector D is to A, B and C and represent it in value from 0 to 1? Also compare results (A to D, B to D, and C to D) should sum to 1.

All vectors are normalized end represent direction.

CodePudding user response:

If I understand you correctly I think there are two options

  • You forget about them summing up to 1 and rather simply ask "How much does each individual vector match the given result vector D?"

    For this something like the following should do it.

    • Get the Vector3.Dot for each vector against D
    • Map these dot products from the range -1 | 1 into the range 0 | 1

    Something like

    public static float[] GetResults(Vector3[] inputVectors /*A,B,C*/, Vetcor3 resultVector /*D*/)
    {
        var results = new float[inputVectors.Length];
    
        // In a first iteration simply get all Dot products
        for(var i = 0; i < inputVector.Length; i  )
        {
            // 1 -> equal direction
            // -1 -> exactly contrary direction
            // 0 -> perpendicular
            var result = Vector3.Dot(resultVector, inputVectors[i]);
            // map to range 0 to 1
            result = (1f   result) / 2f;
            results[i] = result;
        }
    
        return results;
    }
    

    You might get a result like e.g. [0.7, 0.5, 0.2]

  • Or you actually want as a result an array of probabilities or in simple words: "Which vector matches with the given vector D the most?"

    For this basically do the same as before but additionally

    • Sum them all up and use the result as a factor so that in total you get a value of 100% (1)

    This would mean what you get as results are probabilities not actually degree of equality, you just get the vector which is "closest" to D

    // Linq provides queries against IEnumerable types
    // I used it for a shorthand to sum up all numbers in an array see below
    using System.Linq;
    
    ...
    
    public static float[] GetResults(Vector3[] inputVectors /*A,B,C*/, Vetcor3 resultVector /*D*/)
    {
        ... // Same as above 
    
    
        // Additionally now get the sum of them all using Linq -> this is the inverse factor
        var sum = results.Sum();
        // or without Linq basically
        //var sum = 0f;
        //foreach(var result in results)
        //{
        //    sum  = result;
        //}
    
        // in a second iteration now divide all results by the "sum"
        // => after this they will sum up to exactly 1
        for(var i = 0; i < inputVector.Length; i  )
        {
            results[i] = results[i] / sum;
        }
    
        return results;
    }
    

    For the same values as above you might get something like [0.5, 0.357, 0.143]


What I tried to say before is:

Let's assume you get some really bad matching vectors like e.g. first method would return

0.02, 0.1, 0.01

the second method would return e.g.

0.08, 0.77, 0.15

so this would make you assume that B is a pretty good match while actually it is only the less bad match between three really bad matches.

  • Related