Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
JavaScript: Find the angle between two points
var p1 = {
x: 20,
y: 20
};
var p2 = {
x: 40,
y: 40
};
// angle in radians
var angleRadians = Math.atan2(p2.y - p1.y, p2.x - p1.x);
// angle in degrees
var angleDeg = Math.atan2(p2.y - p1.y, p2.x - p1.x) * 180 / Math.PI;
@christophemarois

This comment has been minimized.

Copy link

@christophemarois christophemarois commented Aug 7, 2014

Thanks!

@pencilking2002

This comment has been minimized.

Copy link

@pencilking2002 pencilking2002 commented Sep 24, 2014

Very useful thank you!

@allada

This comment has been minimized.

Copy link

@allada allada commented Jan 11, 2015

+1

@adius

This comment has been minimized.

Copy link

@adius adius commented May 6, 2015

Angle between two points makes no sense. You mean angle between two vectors.

@PAEz

This comment has been minimized.

Copy link

@PAEz PAEz commented Jun 10, 2015

@adius To the educated it makes no sense and to the uneducated it makes perfect sense ;)

@jack2ky

This comment has been minimized.

Copy link

@jack2ky jack2ky commented Oct 28, 2015

@adius Thanks for the clarification. @adius

@Cicim

This comment has been minimized.

Copy link

@Cicim Cicim commented Feb 19, 2016

Great job. Very useful!

@jamesseanwright

This comment has been minimized.

Copy link

@jamesseanwright jamesseanwright commented Feb 27, 2016

Thank you! Really useful for a Maths n00b like me! Googles Math.atan2...

@uatec

This comment has been minimized.

Copy link

@uatec uatec commented Mar 14, 2016

@adius, @PAEz, not the angle between two points OR the angle between two vectors, but the angle of the vector from point A to point B.

@Norik-P

This comment has been minimized.

Copy link

@Norik-P Norik-P commented Mar 18, 2016

Thank you!

@renatoargh

This comment has been minimized.

Copy link

@renatoargh renatoargh commented Mar 30, 2016

+1 :) helpalot

@akaguny

This comment has been minimized.

Copy link

@akaguny akaguny commented Apr 20, 2016

it`s the angle beetween 2 radius vectors?

@Samueleroux

This comment has been minimized.

Copy link

@Samueleroux Samueleroux commented Apr 26, 2016

@Tanvir-rahman

This comment has been minimized.

Copy link

@Tanvir-rahman Tanvir-rahman commented May 25, 2016

Thanks

@chardos

This comment has been minimized.

Copy link

@chardos chardos commented Jul 24, 2016

Thanks man!

@ThibaultJanBeyer

This comment has been minimized.

Copy link

@ThibaultJanBeyer ThibaultJanBeyer commented Jul 25, 2016

Awesome! Thanks

@LiamKarlMitchell

This comment has been minimized.

Copy link

@LiamKarlMitchell LiamKarlMitchell commented Aug 26, 2016

Thanks
I used Math.atan2(v.e(2), v.e(1)) to get the angle of a Sylvester js unit vector / direction from a vector.
For some reason angleFrom kept giving me null hah.

@grantfree035

This comment has been minimized.

Copy link

@grantfree035 grantfree035 commented Nov 14, 2016

Thanks

@onewrinkle

This comment has been minimized.

Copy link

@onewrinkle onewrinkle commented Dec 27, 2016

Like it. Thanks

@xbeat

This comment has been minimized.

Copy link

@xbeat xbeat commented Feb 7, 2017

Thanks

@maggie-lagos

This comment has been minimized.

Copy link

@maggie-lagos maggie-lagos commented Apr 6, 2017

+1. Thank you!! Apparently it's been a while since geometry 😝

@baladkb

This comment has been minimized.

Copy link

@baladkb baladkb commented Apr 17, 2017

Thanks.

@afficiona-zz

This comment has been minimized.

Copy link

@afficiona-zz afficiona-zz commented May 3, 2017

+1

@Dvoikin

This comment has been minimized.

Copy link

@Dvoikin Dvoikin commented May 5, 2017

+1

@VladimirMorin

This comment has been minimized.

Copy link

@VladimirMorin VladimirMorin commented Jun 8, 2017

+1

@guillaumeguerin

This comment has been minimized.

Copy link

@guillaumeguerin guillaumeguerin commented Jun 8, 2017

Awesome !

@parwoto

This comment has been minimized.

Copy link

@parwoto parwoto commented Jul 8, 2017

Thanks

@zoutepopcorn

This comment has been minimized.

Copy link

@zoutepopcorn zoutepopcorn commented Jul 23, 2017

4 usefull lines of code

Great!

@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Aug 13, 2017

#1

@megamanhxh

This comment has been minimized.

Copy link

@megamanhxh megamanhxh commented Aug 23, 2017

Thank you for sharing this master piece!

@gheja

This comment has been minimized.

Copy link

@gheja gheja commented Sep 2, 2017

+1
Thanks!

@v-braun

This comment has been minimized.

Copy link

@v-braun v-braun commented Sep 4, 2017

Thx :) 👍

@nidorx

This comment has been minimized.

Copy link

@nidorx nidorx commented Oct 31, 2017

+1

@mdermksian

This comment has been minimized.

Copy link

@mdermksian mdermksian commented Nov 15, 2017

+1 really loved this!! helped me out a ton thanks

@lomewolf1776

This comment has been minimized.

Copy link

@lomewolf1776 lomewolf1776 commented Nov 16, 2017

@mdermksian why aren't you working on resin systems

@OfirShukrun

This comment has been minimized.

Copy link

@OfirShukrun OfirShukrun commented Dec 29, 2017

Great one! ty very much!

@IngIeoAndSpare

This comment has been minimized.

Copy link

@IngIeoAndSpare IngIeoAndSpare commented Jan 17, 2018

+1 thanks! bro!

@freshsid

This comment has been minimized.

Copy link

@freshsid freshsid commented Jan 22, 2018

Thanks!!

@Pickachu

This comment has been minimized.

Copy link

@Pickachu Pickachu commented Feb 7, 2018

was using for a design tool here, if you need the angle adjusted by all quadrants (between 0º and 360º) the correct function is:

const angle = (anchor, point) => Math.atan2(anchor.y - point.y, anchor.x - point.x) * 180 / Math.PI + 180;

const a = {
	x: 20,
	y: 20
};

const p = {
	x: 0,
	y: 0
};

angle(a, p); // 225

// angle in degrees, from example, same data
angleDeg = Math.atan2(a.y - p.y, a.x - p.x) * 180 / Math.PI; // 45

as pointed on Stack Overflow by jk.

@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Apr 17, 2018

THANK YOU!

@JoshuaCaputo

This comment has been minimized.

Copy link

@JoshuaCaputo JoshuaCaputo commented Apr 24, 2018

Been here 100 times :P

@ghost

This comment has been minimized.

Copy link

@ghost ghost commented May 31, 2018

very useful

@signalwerk

This comment has been minimized.

Copy link

@signalwerk signalwerk commented Aug 19, 2018

Tanks for that! I just put your code in a online-calculator to use it right in the browser.

@micah-akpan

This comment has been minimized.

Copy link

@micah-akpan micah-akpan commented Sep 18, 2018

Thank you!

@iamusbah

This comment has been minimized.

Copy link

@iamusbah iamusbah commented Oct 16, 2018

thank you very much, simple and efficient

@dpleshkov

This comment has been minimized.

Copy link

@dpleshkov dpleshkov commented Nov 21, 2018

Big thanks

@audinue

This comment has been minimized.

Copy link

@audinue audinue commented Jan 23, 2019

Thanks you for this useful snippet.

@cooperbrown9

This comment has been minimized.

Copy link

@cooperbrown9 cooperbrown9 commented Jan 28, 2019

Thank u!

@Adichilla

This comment has been minimized.

Copy link

@Adichilla Adichilla commented Mar 13, 2019

was using for a design tool here, if you need the angle adjusted by all quadrants (between 0º and 360º) the correct function is:

const angle = (anchor, point) => Math.atan2(anchor.y - point.y, anchor.x - point.x) * 180 / Math.PI + 180;

const a = {
	x: 20,
	y: 20
};

const p = {
	x: 0,
	y: 0
};

angle(a, p); // 225

// angle in degrees, from example, same data
angleDeg = Math.atan2(a.y - p.y, a.x - p.x) * 180 / Math.PI; // 45

as pointed on Stack Overflow by jk.

var user = {
    x: 13.046136,
    y: 80.181772
};

var driver = {
    x: 13.045650,
    y: 80.182865
};

Math.atan2(user.y - driver.y, user.x - driver.x) * 180 / Math.PI + 180

angle will be -66.02778421483718 somewhere between (270deg - 315deg) if apply some condition i can get to know the exact angle.

in your calculation, the angle will be returned 113.97221578516282
so it was failed on negative values.

@barboite

This comment has been minimized.

Copy link

@barboite barboite commented Mar 13, 2020

+∞

@ElmoJones3

This comment has been minimized.

Copy link

@ElmoJones3 ElmoJones3 commented May 20, 2020

Thanks!

@Linux4Life531

This comment has been minimized.

Copy link

@Linux4Life531 Linux4Life531 commented Jun 26, 2020

Thanks!

@nald-dev

This comment has been minimized.

Copy link

@nald-dev nald-dev commented Sep 15, 2020

Massive thanks!!!!!

@man-person

This comment has been minimized.

Copy link

@man-person man-person commented Oct 22, 2020

@adius stfu

@Seh83

This comment has been minimized.

Copy link

@Seh83 Seh83 commented Jan 14, 2021

Thanks

@Dan-Traian

This comment has been minimized.

Copy link

@Dan-Traian Dan-Traian commented Apr 21, 2021

Thank you!

@Extex101

This comment has been minimized.

Copy link

@Extex101 Extex101 commented May 7, 2021

How about the pitch and yaw between 2 3D vectors?
Been trying to figure it out for 3 days now 😕

@seamuskills

This comment has been minimized.

Copy link

@seamuskills seamuskills commented May 18, 2021

very useful!

@Oman395

This comment has been minimized.

Copy link

@Oman395 Oman395 commented Sep 7, 2021

THANK YOU!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment