首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >2点与atan2的夹角

2点与atan2的夹角
EN

Stack Overflow用户
提问于 2013-06-25 11:27:36
回答 3查看 14.6K关注 0票数 3

我读到了这个post,关于两点之间的一个角度,我想知道。我以为atan2是为atan2(y,x)定义的,这里是atan2(deltaX,deltaY),为什么现在是x?

代码语言:javascript
复制
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;
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-06-25 11:45:07

Math.java定义为

代码语言:javascript
复制
 public static double atan2(double y, double x) {
    return StrictMath.atan2(y, x); // default impl. delegates to StrictMath
  }

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

如果你交换这两个,你就会得到相对于X轴的时钟角度。

在笛卡尔坐标系中,我们考虑了相对于X轴的逆时针角度。这就是为什么Math.java像上面一样使用它的原因。

票数 13
EN

Stack Overflow用户

发布于 2013-06-25 11:37:49

交换参数的顺序意味着,用X轴代替(逆时针)角度,你可以得到Y轴的(顺时针方向)角度。这没有错,只是不寻常。

票数 2
EN

Stack Overflow用户

发布于 2018-03-06 16:02:20

试试这个:

代码语言:javascript
复制
// ... 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);
票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17296066

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档