18

I continue developing a 2D Collision Detection System in a programming language (Javascript) and one of the last things I need to sharpen it is to know a formula to find this angle:

NOTE: X and Y increase their value FROM LEFT TO RIGHT AND TOP TO BOTTOM

enter image description here

As you can see the angle is relative to the 0° degree or north pole of the 2D space.

Knowing the coordinates of the two points, how can I know that angle? I might have an idea of finding the bearing to rectangle vertices and stuff like that (I just used them for the system) but I want to know if there is already a simple formula for this.

Thank you beforehand!

2 Answers2

22

Define the bearing angle $\theta$ from a point $A(a_1,a_2)$ to a point $B(b_1,b_2)$ as the angle measured in the clockwise direction from the north line with $A$ as the origin to the line segment $AB$.

enter image description here

Then,

$$ (b_1,b_2) = (a_1 + r\sin\theta, a_2 + r\cos\theta), $$

where $r$ is the length of the line segment $AB$. It follows that $\theta$ satisfies the equation

$$ \tan\theta = \frac{b_1 - a_1}{b_2 - a_2} $$

As suggested by @rogerl we can use the $\mathrm{atan2}$ function to compute $\theta$. Let

$$ \hat{\theta} = \mathrm{atan2}(b_1 - a_1, b_2 - a_2) \in (-\pi,\pi] $$

Then the bearing angle $\theta\in[0,2\pi)$ is given by

$$ \theta = \left\{ \begin{array}{ll} \hat{\theta}, & \hat{\theta} \geq 0\\ 2\pi + \hat{\theta}, & \hat{\theta} < 0 \end{array}\right. $$

Note that the equations are given in terms of Cartesian coordinates, so it is necessary to transform to screen coordinates. I believe the formula for $\hat{\theta}$ in terms of screen coordinates $(a_1,a_2)$ and $(b_1,b_2)$ is $\hat{\theta} = \mathrm{atan2}(b_1 - a_1,a_2 - b_2)$.

You could code this function in C++ as follows.

#include <cmath>

// Computes the bearing in degrees from the point A(a1,a2) to
// the point B(b1,b2). Note that A and B are given in terms of
// screen coordinates.
double bearing(double a1, double a2, double b1, double b2) {
    static const double TWOPI = 6.2831853071795865;
    static const double RAD2DEG = 57.2957795130823209;
    // if (a1 = b1 and a2 = b2) throw an error 
    double theta = atan2(b1 - a1, a2 - b2);
    if (theta < 0.0)
        theta += TWOPI;
    return RAD2DEG * theta;
}
K. Miller
  • 4,688
  • Excuse me good sir, I'm not really used to Math terms. Is there any way you can show me the formula in a way I can understand (as a programmer). Thank you for your quick reply! – Juan Bonnett Jan 01 '16 at 22:45
  • Thank you! I'm gonna try to code this formula and let you know how it went! thanks! – Juan Bonnett Jan 01 '16 at 22:54
  • I just realised that if you have the point to the RIGHT or tothe LEFT, for example, it wont show 90° or 270° but it will always show between 0° and 180°. Any idea why? – Juan Bonnett Jan 02 '16 at 04:26
  • It depends on how you define the angle. Should the angle be in the range $[0,2\pi]$? Relative to what point is it measured from? It is measured from the positive $y$-axis, but do you want the clockwise or counterclockwise direction to be positive? – K. Miller Jan 02 '16 at 13:22
  • I understand now, what we had before was a plain with negative and positive axes. Well, in my case, this software doesn't have that, we go from the top-left corner of the screen 0,0 to whatever the screen size is (: I'm gonna check and try the new formulas and let you know! Thank you again sir! – Juan Bonnett Jan 02 '16 at 14:45
  • @K.Miller can I user this to find the angle between two GPS coordinate ? – devdoe Mar 03 '17 at 10:49
  • @Nil I am not sure. I would need to know more about GPS coordinates and what you want to do. Perhaps you could post your own question and benefit from the responses of the entire community. – K. Miller Mar 03 '17 at 14:35
  • How does this code change when point A(a1,a2) and point B(b1,b2) are lat long coordinates? – Ankit Das Dec 23 '18 at 03:53
  • Here is a good example for lat/lng coordinates that I was able to incorporate into a Java project: https://www.igismap.com/formula-to-find-bearing-or-heading-angle-between-two-points-latitude-longitude/ – alice_silver_man May 24 '19 at 03:17
1

I was trying to do the same thing and K. Miller's answer helped me. Since I did it in R, I thought I'd post my code here. I'm not 100% sure that I did the right thing by returning 360 when it could also be 0. This is the first I've touched trig since high school, make sure it's correct before you use it.

bearing = function(x1=10, y1 = 10, x2=3, y2=3){
  require(NISTunits)
  if((x1 == x2) & (y1 > y2)){
    return(360)
  }else if((y1 == y2) & (x1 < x2)){
    return(90)
  } else if((y1==y2 & x1 > x2)){
    return(270)
  }else if(y1 == y2 & x1 < x2){
    return(180)
  }else if((x1 == x2) & (y1==y2)){
    return(NaN)
  }
  else
  theta = atan2(x2 - x1, y1 - y2)
  if(theta < 0){
    theta = theta + 2*pi
  }
  theta = NISTradianTOdeg(theta)
  return(theta)
}