当前位置:   article > 正文

XY坐标两点插值(加密)计算(Java)_插值加密

插值加密

已知两个点的坐标,这个两个点可以连成一条直线,目前在这条直线上按照固定距离进行插值,即增加更多点在这条直线上。

package com.demo.utils;

import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * 两点插值计算
 *
 * @author elinx
 * @since 2021-09-24 14:05
 */
public class PointInterUtil {

    @Data
    public static class Point {
        private double x;
        private double y;
    }

    public static List<Point> interpolation(Point prePoint, Point curPoint) {

        double x0 = prePoint.getX();
        double y0 = prePoint.getY();
        double x1 = curPoint.getX();
        double y1 = curPoint.getY();

        double nDisX = Math.abs(x1 - x0);
        double nDisY = Math.abs(y1 - y0);

        List<Point> pointList = new ArrayList<>();

        double stepLength = 1.0;
        double rate = 1.0;
        if (nDisX >= nDisY) {
            //如果x轴方向距离较远,则以x轴坐标计算步长
            if (x0 > x1) {
                stepLength = -stepLength;
            }
            double xTemp = x0 + stepLength;
            for (; x0 > x1 ? xTemp > x1 : xTemp < x1; xTemp += stepLength) {
                double yTemp = rate * (y1 - y0) / (x1 - x0) * (xTemp - x0) + y0;
                Point point = new Point();
                point.setX(xTemp);
                point.setY(yTemp);
                pointList.add(point);
            }
        } else {
            //如果y轴方向距离较远,则以y轴坐标计算步长
            if (y0 > y1) {
                stepLength = -stepLength;
            }
            double yTemp = y0 + stepLength;
            for (; y0 > y1 ? yTemp > y1 : yTemp < y1; yTemp += stepLength) {
                double xTemp = rate * (yTemp - y0) * (x1 - x0) / (y1 - y0) + x0;
                Point point = new Point();
                point.setX(xTemp);
                point.setY(yTemp);
                pointList.add(point);
            }
        }
        return pointList;
    }

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/545305
推荐阅读
相关标签
  

闽ICP备14008679号