转自:https://blog.csdn.net/zzrrhhh/article/details/78385452?locationNum=8&fps=1
反距离插值C++实现
/*
测试数据
70 140 115.4
115 115 123.1
150 150 113.8
110 170 110.5
90 190 107.2
180 210 131.78
110 150
所求值应为114.946
*/
#include <iostream>
#include <cmath>
using namespace std;
struct point
{
double x;
double y;
double z;
double weight; //权重
double distance; //距离插值点的距离
};
const int r = 1; //反距离的幂值(0.5到3均可)
int n = 0; //点的个数
point p[50]; //存放离散点
point q; //插值点
void Distance() //计算未知点到所有点的距离
{
for(int i = 0; i < n; ++i)
p[i].distance = sqrt((q.x-p[i].x)*(q.x-p[i].x) + (q.y-p[i].y) *(q.y-p[i].y));
}
void Weight() //计算权重
{
double f = 0;
int i = 0;
for(i = 0; i < n; ++i)
f += pow(1.0 / p[i].distance, r);
for(i = 0; i < n; ++i)
p[i].weight = pow(1.0 / p[i].distance, r) / f;
}
void Getval() //得到插值点的权重
{
q.weight = 0;
for(int i = 0; i < n; ++i)
q.z += p[i].weight * p[i].z;
}
int main()
{
cout << "请输入已知点的个数" << endl;
cin >> n;
cout << "请输入已知点的坐标" << endl;
for(int i = 0; i < n; ++i)
cin >> p[i].x >> p[i].y >> p[i].z;
cout << "输入目标点的坐标" << endl;
cin >> q.x >> q.y;
Distance();
Weight();
Getval();
cout << q.z << endl;
return 0;
} --------------------- 本文来自 zzrrhhh 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/zzrrhhh/article/details/78385452?utm_source=copy