package com.gyee.common.util.algorithm;

import com.gyee.common.model.algorithm.PointVo;

import java.util.*;

/**
 * @ClassName : GDJTLineBuild
 * @Author : xieshengjie
 * @Date: 2021/11/15 12:00
 * @Description :
 */
public class GDJTLineBuild {

    public static List<PointVo> buildLine(double[] arrX, double[] arrY) {
        List<PointVo> points = new ArrayList<>();

        if (arrX.length != arrY.length) {
            return points;
        }

        Map<Double, Double> pointsDic = new HashMap<>();
        Map<Double, Integer> pointsjs = new HashMap<>();

        for (int i = 0; i < arrX.length; i++) {
            if (pointsDic.containsKey(arrX[i])) {
                Double x = pointsDic.get(arrX[i]);
                x += arrY[i];
                pointsDic.put(arrX[i], x);
                Integer js = pointsjs.get(arrX[i]);
                js++;
                pointsjs.put(arrX[i], js);
            } else {
                pointsDic.put(arrX[i], arrY[i]);
                pointsjs.put(arrX[i], 1);
            }
        }

        Set<Double> keys = pointsDic.keySet();
        for (Double key : keys) {
            double b = pointsDic.get(key) / pointsjs.get(key);
            PointVo p = new PointVo();
            p.setX(key);
            p.setY(b);
            points.add(p);
        }

        return points;
    }
}
