Find the bearing angle between two points in a 2D space











up vote
8
down vote

favorite
6












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!










share|cite|improve this question
























  • You probably want to look at the $atan2$ function.
    – rogerl
    Jan 1 '16 at 22:48















up vote
8
down vote

favorite
6












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!










share|cite|improve this question
























  • You probably want to look at the $atan2$ function.
    – rogerl
    Jan 1 '16 at 22:48













up vote
8
down vote

favorite
6









up vote
8
down vote

favorite
6






6





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!










share|cite|improve this question















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!







angle






share|cite|improve this question















share|cite|improve this question













share|cite|improve this question




share|cite|improve this question








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


















  • 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










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$.



enter image description here



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;
}





share|cite|improve this answer























  • 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











Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
});
});
}, "mathjax-editing");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "69"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
noCode: true, onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























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$.



enter image description here



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;
}





share|cite|improve this answer























  • 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















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$.



enter image description here



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;
}





share|cite|improve this answer























  • 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













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$.



enter image description here



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;
}





share|cite|improve this answer














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 + 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;
}






share|cite|improve this answer














share|cite|improve this answer



share|cite|improve this answer








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


















  • 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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Willebadessen

Ida-Boy-Ed-Garten

Residenzschloss Arolsen