C++简单实现反距离插值 |
您所在的位置:网站首页 › 简单排序编码法算权重 › C++简单实现反距离插值 |
转自: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 #include 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 n; cout p[i].x >> p[i].y >> p[i].z; cout q.x >> q.y; Distance(); Weight(); Getval(); cout |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |