Find the bearing angle between two points in a 2D space
up vote
8
down vote
favorite
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

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!
angle
add a comment |
up vote
8
down vote
favorite
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

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!
angle
You probably want to look at the $atan2$ function.
– rogerl
Jan 1 '16 at 22:48
add a comment |
up vote
8
down vote
favorite
up vote
8
down vote
favorite
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

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!
angle
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

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!
angle
angle
edited Jan 1 '16 at 23:43
asked Jan 1 '16 at 22:37
Juan Bonnett
145115
145115
You probably want to look at the $atan2$ function.
– rogerl
Jan 1 '16 at 22:48
add a comment |
You probably want to look at the $atan2$ function.
– rogerl
Jan 1 '16 at 22:48
You probably want to look at the $atan2$ function.
– rogerl
Jan 1 '16 at 22:48
You probably want to look at the $atan2$ function.
– rogerl
Jan 1 '16 at 22:48
add a comment |
1 Answer
1
active
oldest
votes
up vote
12
down vote
accepted
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$.

Then,
$$
(b_1,b_2) = (a_1 + rsintheta, a_2 + rcostheta),
$$
where $r$ is the length of the line segment $AB$. It follows that $theta$ satisfies the equation
$$
tantheta = 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 $thetain[0,2pi)$ is given by
$$
theta = left{
begin{array}{ll}
hat{theta}, & hat{theta} geq 0\
2pi + 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;
}
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 1 '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 1 '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 2 '16 at 4:26
It depends on how you define the angle. Should the angle be in the range $[0,2pi]$? 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 2 '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 2 '16 at 14:45
|
show 3 more comments
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
12
down vote
accepted
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$.

Then,
$$
(b_1,b_2) = (a_1 + rsintheta, a_2 + rcostheta),
$$
where $r$ is the length of the line segment $AB$. It follows that $theta$ satisfies the equation
$$
tantheta = 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 $thetain[0,2pi)$ is given by
$$
theta = left{
begin{array}{ll}
hat{theta}, & hat{theta} geq 0\
2pi + 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;
}
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 1 '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 1 '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 2 '16 at 4:26
It depends on how you define the angle. Should the angle be in the range $[0,2pi]$? 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 2 '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 2 '16 at 14:45
|
show 3 more comments
up vote
12
down vote
accepted
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$.

Then,
$$
(b_1,b_2) = (a_1 + rsintheta, a_2 + rcostheta),
$$
where $r$ is the length of the line segment $AB$. It follows that $theta$ satisfies the equation
$$
tantheta = 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 $thetain[0,2pi)$ is given by
$$
theta = left{
begin{array}{ll}
hat{theta}, & hat{theta} geq 0\
2pi + 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;
}
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 1 '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 1 '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 2 '16 at 4:26
It depends on how you define the angle. Should the angle be in the range $[0,2pi]$? 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 2 '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 2 '16 at 14:45
|
show 3 more comments
up vote
12
down vote
accepted
up vote
12
down vote
accepted
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$.

Then,
$$
(b_1,b_2) = (a_1 + rsintheta, a_2 + rcostheta),
$$
where $r$ is the length of the line segment $AB$. It follows that $theta$ satisfies the equation
$$
tantheta = 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 $thetain[0,2pi)$ is given by
$$
theta = left{
begin{array}{ll}
hat{theta}, & hat{theta} geq 0\
2pi + 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;
}
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$.

Then,
$$
(b_1,b_2) = (a_1 + rsintheta, a_2 + rcostheta),
$$
where $r$ is the length of the line segment $AB$. It follows that $theta$ satisfies the equation
$$
tantheta = 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 $thetain[0,2pi)$ is given by
$$
theta = left{
begin{array}{ll}
hat{theta}, & hat{theta} geq 0\
2pi + 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;
}
edited Jan 3 '16 at 18:29
answered Jan 1 '16 at 22:43
K. Miller
3,623612
3,623612
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 1 '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 1 '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 2 '16 at 4:26
It depends on how you define the angle. Should the angle be in the range $[0,2pi]$? 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 2 '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 2 '16 at 14:45
|
show 3 more comments
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 1 '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 1 '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 2 '16 at 4:26
It depends on how you define the angle. Should the angle be in the range $[0,2pi]$? 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 2 '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 2 '16 at 14:45
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 1 '16 at 22:45
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 1 '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 1 '16 at 22:54
Thank you! I'm gonna try to code this formula and let you know how it went! thanks!
– Juan Bonnett
Jan 1 '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 2 '16 at 4:26
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 2 '16 at 4:26
It depends on how you define the angle. Should the angle be in the range $[0,2pi]$? 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 2 '16 at 13:22
It depends on how you define the angle. Should the angle be in the range $[0,2pi]$? 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 2 '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 2 '16 at 14:45
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 2 '16 at 14:45
|
show 3 more comments
Thanks for contributing an answer to Mathematics Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f1596513%2ffind-the-bearing-angle-between-two-points-in-a-2d-space%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You probably want to look at the $atan2$ function.
– rogerl
Jan 1 '16 at 22:48