0

I have a method and am attempting to find the angle between two points in radians.

public double angleTo(Body other)
{
    double angle = Math.atan(Math.abs(this.y - other.y) / Math.abs(this.x - other.x));
    while (angle < 0) {angle += Math.PI;}
    while (angle > Math.PI) {angle -= Math.PI;}
    return angle;
}

However when I call this method for points (0, 0) and (10, 10) I get 20.7758096196980899, which shouldn't be possible.

  • [`atan2`](https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#atan2-double-double-) is better for this calculation because it handles the quadrant normalization for you. – 0x5453 Mar 02 '22 at 19:11
  • _"Angle between two points"_ - that's technically impossible. What you actually want is "the slope (relative to the axes) of a line defined by two points". – Jim Garrison Mar 02 '22 at 19:40

0 Answers0