package com.gmx.butai.service;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class idw {
public static void main(String[] args) {
long startTime = System.currentTimeMillis(); //获取开始时间
// int[] Lon = {1,4,2,4,3,1,2,0,0,0,0,8,0,1,0,0,6,6};
// int[] Lat = {4,2,0,1,1,7,1,9,9,9,0,1,1,7,0,0,1,8};
// int[] PM = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18};
// int[][] lon = {{2}};
// int[][] lat = {{1}};
// double[][] m = gridData(Lon, Lat, PM, lon, lat);
//
// long endTime = System.currentTimeMillis(); //获取结束时间
//
// System.out.println("程序运行时间:" + (endTime - startTime) + "ms"); //输出程序运行时间
//
//
// System.out.println(m[0][0]);
int[] xL = {125,130,75,95,105,55,40,30};
int[] yL = {40,90,110,225,310,310,225,140};
int[] valueLeft = {130,200,220,60,230,230,70,170};
int w = 10;
int h = 30;
int xn = 11;
int yn = 12;
int[][] XL = new int[xn][yn];
int[][] YL = new int[xn][yn];
//插入固定个点
// int[][] XL = {{75},{50},{35},{90},{30},{100},{110},{90}};
// int[][] YL = {{65},{100},{180},{170},{265},{265},{125},{170}};
//新加的点:[75,65],[50,100],[35,180],[90,170],[65,265]
// int[][] XL = {{75,75,75,75,75},{50,50,50,50,50},{35,35,35,35,35},{90,90,90,90,90},{65,65,65,65,65}};
// int[][] YL = {{65,100,180,170,265},{65,100,180,170,265},{65,100,180,170,265},{65,100,180,170,265},{65,100,180,170,265}};
double[][] resLeft = new double[160][350];
for (int i = 0; i < xn; i++){
for (int j = 0; j < yn; j++){
XL[i][j] = i * 15;
YL[i][j] = j * 30;
}
}
double[][] m = gridData(xL, yL, valueLeft, XL, YL);
for (int i = 0; i < XL.length; i++){
System.out.print("[");
for (int j = 0; j < XL[0].length; j++){
if(j == XL[0].length - 1){
System.out.print(String.format("%.2f",m[i][j]) +"],");
}else{
System.out.print(String.format("%.2f",m[i][j]) +",");
}
}
System.out.println();
}
long endTime = System.currentTimeMillis(); //获取结束时间
System.out.println("程序运行时间:" + (endTime - startTime) + "ms"); //输出程序运行时间
}
public static double[][] gridData(int[] x, int[] y, int[] z,
int[][] X, int[][] Y) {
int xLength = x.length;
int xSize = X.length;
int ySize = X[0].length;
//半径
int r = 100;
//至少选5个点
int m = 5;
double[][] Z = new double[xSize][ySize];
for (int i = 0; i < xSize; i++) {
for (int j = 0; j < ySize; j++) {
int n = 0;
List list = new ArrayList<Integer>();
//算出预测值的距离的平方
int[] rData = new int[xLength];
int[] rData2 = new int[xLength];
for (int k = 0; k < xLength; k++) {
rData[k] = (X[i][j] - x[k]) * (X[i][j] - x[k])
+ (Y[i][j] - y[k]) * (Y[i][j] - y[k]);
rData2[k] = rData[k];
if(rData[k] <= (r * r)){
list.add(k);
n++;
}
}
if(n < m){ //选择的点少于m个时
Arrays.sort(rData2);
int temp = rData2[m];
for(int q = 0; q < xLength; q++){
if((rData[q] >= (r * r)) && (rData[q] <= temp)){
list.add(q);
n++;
}
}
}
//求权重,插值
double D = 0; //分母
double[] D2 = new double[n]; // 1/(D*D)
for(int q = 0; q < n; q++){
double temp = (double)rData[(int)list.get(q)];
if(temp == 0){
temp = 0.01;
}
D2[q] = 1/(temp*temp);
D += D2[q];
}
for(int q = 0; q < n; q++) {
Z[i][j] += D2[q] / D * z[(int) list.get(q)];
}
}
}
return Z;
}
}