Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Holdmyfire opened this issue Jan 9, 2017 · 0 comments

Comments

@Holdmyfire
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant