3

I was reading this post about 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?

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;
}
mikhail-t
  • 4,103
  • 7
  • 36
  • 56
vuvu
  • 4,886
  • 12
  • 50
  • 73
  • out of curiousity: how do you define an angle between two point? – Marco Forberg Jun 25 '13 at 11:50
  • @MarcoForberg, by defining an axis which passes through one of the points and measuring the angle between that axis and the line segment connecting the points. Often times, this is referred to as a [bearing angle](https://en.wikipedia.org/wiki/Bearing_(navigation)). – Richard Oct 15 '13 at 20:44

3 Answers3

13

Math.java it define as

 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.

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

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

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
2

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.

Joni
  • 108,737
  • 14
  • 143
  • 193
-3

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);
mikhail-t
  • 4,103
  • 7
  • 36
  • 56
aaa
  • 1