1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| double interpolate(const QPointF& queryPoint) { QVector<double> weights; QVector<double> weightedValues; for (int i = 0; i < inputPoints.size(); i++) { double distance = std::sqrt(std::pow(queryPoint.x() - inputPoints[i].x(), 2) + std::pow(queryPoint.y() - inputPoints[i].y(), 2)); if (distance == 0) { return inputAttributes[i]; } double weight = 1.0 / distance; weights.append(weight); weightedValues.append(weight * inputAttributes[i]); } QVector<int> indices(inputPoints.size()); std::iota(indices.begin(), indices.end(), 0); std::sort(indices.begin(), indices.end(), [&](int a, int b) { return weights[a] > weights[b]; }); double numerator = 0.0; double denominator = 0.0; for (int i = 0; i < requiredNeighbors; i++) { int index = indices[i]; numerator += weightedValues[index]; denominator += weights[index]; } return numerator / denominator; }
|