将向量旋转到目标点

问题描述

基本解释:我有一个路径和一个目标点,我希望路径旋转,以便路径的终点与目标点连接。

示例:

目标点:X:274,Y:290

路径:可能是水平的(例如:X:100,Y:150 到 X:150,Y:150)或可能是垂直的(例如:X:100,Y:100 到 X:100,Y:150)

路径在所有顶点都不是直线,因为它有一些噪音(它基本上是记录的鼠标模式)。

我目前拥有的(代码中):

private List<Movement> movements = new ArrayList(); //Path stored here

private Movement[] generateApplicable(int x,int y) {
    Movement[] updated = null;
    Point target = new Point(x,y);
    startingPoint = new Point(m.getLocation().x,m.getLocation().y);
    Movement lastMovement = movements.get(movements.size()-1);
    double movemenTradius = lastMovement.distance(m.getLocation());
    if(movemenTradius >= m.getLocation().distance(x,y)) {
        Movement chosen = null;
        double angle = -1337;
        for(int i = movements.size() - 1; i >= 0; i--) {
            Movement current = movements.get(i);
            if(current.distance(startingPoint) >= target.distance(startingPoint)) {
                chosen = current;
                continue;
            }
            double radius = current.distance(startingPoint);
            int nX;
            int nY;
            if(chosen != null) {
                if(angle == -1337) {
                    updated = new Movement[i + 2];
                    angle = angleBetweenTwoPointsWithFixedPoint(target.x,target.y,chosen.getPoint().x,chosen.getPoint().y,chosen.getPoint().y);
                    updated[i + 1] = new Movement(x,y,chosen.timeBefore,true);
                } else {
                    nX = (int) (startingPoint.x + (radius + current.x) * Math.cos(angle));
                    nY = (int) (startingPoint.y + (radius + current.y) * Math.sin(angle));
                    updated[i] = new Movement(nX,nY,current.timeBefore,true);
                }
            }
        }
    }
    return updated;
}

我如何获得角度:

public double angleBetweenTwoPointsWithFixedPoint(double point1X,double point1Y,double point2X,double point2Y,double fixedX,double fixedY) {

    double angle1 = Math.atan2(point1Y - fixedY,point1X - fixedX);
    double angle2 = Math.atan2(point2Y - fixedY,point2X - fixedX);

    return angle1 - angle2;
}

上面的代码使路径靠近目标(水平时),但垂直时路径走向错误的方向。

注意 1:Movement 对象包含坐标和移动距离所需的时间

注意 2: (m.getLocation()) 获取鼠标当前位置(这是我希望路径开始的位置)。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)