反距离加权插值方法——C#实现

  正当自己无所事事时,朋友给我布置了一项作业,如下图:
  这里写图片描述
  一乍听,我还挺害怕的,以为很高深的问题,但因为给出了计算公式,实现起来并不难。将上图中的公式拆解开来,并加上输入、输出功能基本上就算完成了。

输入

  用户需要输入离散点个数n和相应的坐标(x,y,z)、公式里面的beta以及目标点的坐标(x,y)
  根据计算需要,先设计点的数据结构:
  

        struct Point{
            public double x;
            public double y;
            public double z;
            public double d;    //该点距目标点的距离
            public double w;    //权重
        }

声明所需要的离散点数组points、变量n和beta以及目标点point;

 /*全局变量*/
 static Point[] points;  //用户存放离散点
 static Point point = new Point();//目标点
 /*局部变量*/
  int n=0;   //离散点个数
  int beta=1;   //beta值,一般设为1或2
/*数据输入*/
 Console.Write("请输入离散点个数n:");
 n = Convert.ToInt32(Console.ReadLine());

  w=new double[n];
  points=new Point[n];

  Console.Write("请输入beta值(建议值1或2):");
  beta = Convert.ToInt32(Console.ReadLine());

  //下面输入每一个离散点的坐标
  Console.WriteLine("****************\n下面输入离散点坐标(格式为:x,y,z:");
  for(int i=0;i<n;i++){
      Console.Write("第{0}个点坐标:",i+1);
      string[]str1=Console.ReadLine().Split(',');
      points[i].x=Convert.ToDouble(str1[0]);
      points[i].y=Convert.ToDouble(str1[1]);
      points[i].z=Convert.ToDouble(str1[2]);
  }

  //下面输入要计算的点的坐标
  Console.Write("离散点输入完成\n最后再输入目标点的坐标(格式为:x,y:\n目标点:");
  string[] str2 = Console.ReadLine().Split(',');
  point.x = Convert.ToDouble(str2[0]);
  point.y = Convert.ToDouble(str2[1]);
  point.z = 0;    //初始化为0

计算过程

  计算过程细分为三个小部分:
  1.计算各离散点至目标点的距离;
  2.计算权重;
  3.计算最终的高程值
 

//计算距离,每一个离散点至目标点的平面距离
static void GetDistance()
{
    for (int i = 0; i < points.Length; i++)
    {
        points[i].d = Math.Sqrt(Math.Pow((point.x - points[i].x), 2) + Math.Pow((point.y - points[i].y), 2));
    }
}
//获取分母,计算中会用到
static double GetFenmu(int beta)
  {
      double fenmu = 0;
      for (int i = 0; i < points.Length; i++)
      {
          fenmu += Math.Pow((1/points[i].d),beta);
      }
      return fenmu;
  }

  //计算权重
  static void GetWeight(int beta)
  {
      //权重是距离的倒数的函数
      double fenmu = GetFenmu(beta);
      for (int i = 0; i < points.Length; i++)
      {
          points[i].w = Math.Pow((1 / points[i].d),beta) / fenmu;
      }
  }
 //得到最终高程值
 static void GetTargetZ()
 {
     for (int i = 0; i < points.Length; i++)
     {
         point.z += points[i].z * points[i].w;
     }
 }

结果输出

  就是展示咯

//纯粹为了展示
static void ShowPoints()
  {
      Console.WriteLine("计算结果:");
      Console.WriteLine("点号\tx\ty\tz\td\tw");
      for (int i = 0; i < points.Length; i++)
      {
          Console.WriteLine("No.{0}\t{1}\t{2}\t{3}\t{4}\t{5}", i + 1, points[i].x, points[i].y, points[i].z, points[i].d.ToString("#0.000"), points[i].w.ToString("#0.000"));
      }
  }

运行结果
下载

  • 18
    点赞
  • 65
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
你可以使用以下代码来实现距离加权插值算法(Inverse Distance Weighting,IDW)的Java实现: ```java import java.util.ArrayList; import java.util.List; public class IDWAlgorithm { // 数据点的结构体 private static class DataPoint { public double x; public double y; public double value; public DataPoint(double x, double y, double value) { this.x = x; this.y = y; this.value = value; } } // 计算两点之间的距离 private static double distance(double x1, double y1, double x2, double y2) { return Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2)); } // 距离加权插值算法 public static double idwInterpolation(List<DataPoint> dataPoints, double x, double y, double power) { double weightedSum = 0.0; double weightSum = 0.0; for (DataPoint point : dataPoints) { double dist = distance(point.x, point.y, x, y); if (dist == 0.0) { // 如果点重合,则直接返回该点的值 return point.value; } double weight = 1.0 / Math.pow(dist, power); weightedSum += weight * point.value; weightSum += weight; } if (weightSum == 0.0) { return 0.0; // 避免除以零错误 } return weightedSum / weightSum; } public static void main(String[] args) { // 示例数据 List<DataPoint> dataPoints = new ArrayList<>(); dataPoints.add(new DataPoint(0.0, 0.0, 5.0)); dataPoints.add(new DataPoint(1.0, 0.0, 3.0)); dataPoints.add(new DataPoint(0.0, 1.0, 2.0)); // 要插值的坐标 double x = 0.5; double y = 0.5; // 插值权重的幂 double power = 2.0; // 执行插值 double interpolatedValue = idwInterpolation(dataPoints, x, y, power); System.out.println("插值结果: " + interpolatedValue); } } ``` 以上是一个简单的距离加权插值算法实现示例。你可以根据自己的需求进行调整和扩展。注意,这只是一个基本的实现,可能需要根据实际情况进行改进。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值