package com.xf.soilinformationize.utils;
import cn.hutool.core.convert.Convert;
import java.util.List;
/**
* <p>
* 将区间值重新按照新区间分组
* </p>
*
* @Author REID
* @Blog https://blog.csdn.net/qq_39035773
* @GitHub https://github.com/BeginnerA
* @Data 2021/2/23
* @Version V1.0
**/
public class IntervalUtil {
/**
* 检查数组中的重合, 计算出需要分组的区间值是属于那个
* @param arr 需要分组的区间 如:[1,2]
* @param arrList 区间集合 如:[[1,2],[2,3]]
* @return 分类下标位置 如:0
*/
public static int checkCoincidenceInArray(List<Object> arr, List<List<Object>> arrList) {
int ind = -1;
double maxLen = 0;
for (int i = 0; i < arrList.size(); i++) {
double len = checkCoincidence(arr, arrList.get(i));
if (len >= maxLen) {
ind = i;
maxLen = len;
}
}
return ind;
}
/**
* 重合度
* @param arr1 数组
* @param arr2 数组
* @return 重合度
*/
private static double checkCoincidence(List<Object> arr1, List<Object> arr2) {
if (arr1.size() == 1) {
arr1.add(arr1.get(0));
}
double a = Math.max(Convert.toDouble(arr1.get(0)), Convert.toDouble(arr2.get(0)));
double b = Math.min(Convert.toDouble(arr1.get(1)), Convert.toDouble(arr2.get(1)));
return b - a;
}
}