cambecc/air

how to use inverseDistanceWeighting when result is a number not a vector

Holdmyfire opened this issue · 0 comments

function inverseDistanceWeighting(points, k) {

    // Build a space partitioning tree to use for quick lookup of closest neighbors.
    var tree = kdTree(points, 2, 0);

    // Define special scratch objects for intermediate calculations to avoid unnecessary array allocations.
    var temp = [];
    var nearestNeighbors = [];
    for (var i = 0; i < k; i++) {
        nearestNeighbors.push({});
    }

    function clear() {
        for (var i = 0; i < k; i++) {
            var n = nearestNeighbors[i];
            n.point = null;
            n.distance2 = Infinity;
        }
    }

    // Return a function that interpolates a vector for the point (x, y) and stores it in "result".
    return function(x, y, result) {
        var weightSum = 0;

        clear();  // reset our scratch objects

// temp[0] = x;
// temp[1] = y;
temp = result;

        nearest(temp, tree, nearestNeighbors);  // calculate nearest neighbors

        // Sum up the values at each nearest neighbor, adjusted by the inverse square of the distance.
        for (var i = 0; i < k; i++) {
            var neighbor = nearestNeighbors[i];
            var sample = neighbor.point[2];
            var d2 = neighbor.distance2;
            if (d2 === 0) {  // (x, y) is exactly on top of a point.
                //result[0] = sample[0];
                //result[1] = sample[1];
            	result = sample;
                return result;
            }
            var weight = 1 / d2;
            //temp[0] = sample[0];
            //temp[1] = sample[1];
            temp = sample;
            result = addVectors(result, scaleVector(temp, weight));
            weightSum += weight;
        }

        // Divide by the total weight to calculate an average, which is our interpolated result.
        //return scaleVector(result, 1 / weightSum);
        return result/weightSum;
    }
}

I'm a student. I'm so sorry that how amateur am I. I don't know how to solve the temp and the "result = addVectors(result, scaleVector(temp, weight));" Can you tell me how to use inverseDistanceWeighting when result is a number not a vector?