java 两点之间的角度与 atan2

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17296066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 01:36:57  来源:igfitidea点击:

Angle between 2 points with atan2

javaatan2

提问by vuvu

I was reading this postabout getting an angle between 2 points and was wondering. I thought atan2 is defined for atan2(y,x) here it is atan2(deltaX, deltaY), why is x first now?

我读这个职位有关获取2点之间的角度,想知道。我认为 atan2 是为 atan2(y,x) 定义的,这里是 atan2(deltaX, deltaY),为什么现在先是 x?

public float getAngle(Point target) {
    float angle = (float) Math.toDegrees(Math.atan2(target.x - x, target.y - y));

    if (angle < 0) {
        angle += 360;
    }

    return angle;
}

回答by Ruchira Gayan Ranaweera

Math.java it define as

Math.java 它定义为

 public static double atan2(double y, double x) {
    return StrictMath.atan2(y, x); // default impl. delegates to StrictMath
  }

and this will return the counter-clock wise angle with respect to X- axis.

这将返回相对于 X 轴的逆时针角度。

If you interchange those two you will get the clock wise angle with respect to X- axis.

如果将这两个互换,您将获得相对于 X 轴的顺时针角度。

In Cartesian coordinate system we consider counter-clock wise angle with respect to X-axis. That is why Math.java use this as above.

在笛卡尔坐标系中,我们考虑相对于 X 轴的逆时针角度。这就是 Math.java 如上所述使用它的原因。

回答by Joni

Swapping the order of the arguments means that instead of the (counter-clockwise) angle with the X-axis you get the (clockwise) angle with the Y-axis. It's not wrong, just unusual.

交换参数的顺序意味着您得到的是与 Y 轴的(顺时针)角度,而不是与 X 轴的(逆时针)角度。这没有错,只是不寻常。

回答by aaa

Try this out:

试试这个:

// ... code
Target start = new Target();
        start.setX(0);
        start.setY(0);
        Target aLine = new Target();
        Target bLine = new Target();
        aLine.setX(-65000);
        aLine.setY(ress.getObstacle().getLine());
        bLine.setX(65000);
        bLine.setY(ress.getObstacle().getLine());
        Line2D line = new Line2D.Float(aLine.getX(), aLine.getY(), bLine.getX(), bLine.getY());

        List<Target> list = new ArrayList<Target>();

        if (!(ress.getObstacle().getLine() == 0)) {
            //check if points are there , if yes just reinitialize a linea-lineb and calculate the same in for:

            String a = "";
            try {
                a = ress.getObstacle().getA().toStrin`enter code here`g();
            } catch (NullPointerException e) {

            }
            if (!(a == "")) {
                aLine.setX(ress.getObstacle().getA().getX());
                aLine.setY(ress.getObstacle().getLine());
                bLine.setX(ress.getObstacle().getB().getX());
                bLine.setY(ress.getObstacle().getLine());
                Line2D lineNew = new Line2D.Float(aLine.getX(), aLine.getY(), bLine.getX(), bLine.getY());

                for (Target t : ress.getTargets()) {
                    Line2D line2 = new Line2D.Float(start.getX(), start.getY(), t.getX(), t.getY());
                    if (!line2.intersectsLine(lineNew)) {
                        list.add(t);
                    }
                }
            } else {
        //-------------------start old part----------------------------------
                 for (Target t : ress.getTargets()) {
                       Line2D line2 = new Line2D.Float(start.getX(), start.getY(), t.getX(), t.getY());
                         if (!line2.intersectsLine(line)) {
                            list.add(t);
                         }
                 }
                 ///////-------end old part
            }

        } else {
            double angA = Math.toDegrees(StrictMath.atan2(ress.getObstacle().getA().getX() - start.getX(), ress.getObstacle().getA().getY() - start.getY()));
            double angB = Math.toDegrees(StrictMath.atan2(ress.getObstacle().getB().getX() - start.getX(), ress.getObstacle().getB().getY() - start.getY()));

            Boolean up = (ress.getObstacle().getA().getY()>0)&(ress.getObstacle().getB().getY()>0);
            Boolean left = (ress.getObstacle().getA().getX()<0)&(ress.getObstacle().getB().getX()<0);
            Boolean right = (ress.getObstacle().getA().getX()>0)&(ress.getObstacle().getB().getX()>0);

            for (Target t : ress.getTargets()) {
                double angT = Math.toDegrees(StrictMath.atan2(t.getX() - start.getX(), t.getY() - start.getY()));

                if (up) {

                    if (!((angT > Math.min(angA,angB)) & (angT < Math.max(angB,angA))))
                        list.add(t);
                } else
                    if (right || left) {
                    if ( !((angT > Math.min(angA,angB)) & (angT< Math.max(angB,angA)))) {
                       list.add(t);
                    }
                } else
                    {
                        if ( ((angT > Math.min(angA,angB)) & (angT< Math.max(angB,angA)))) {
                            list.add(t);
                        }
                    }
            }
        }


        sol.setTargets(list);