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.

Show comment
Hide comment
@christophemarois

christophemarois commented Aug 7, 2014

Thanks!

@pencilking2002

This comment has been minimized.

Show comment
Hide comment
@pencilking2002

pencilking2002 Sep 24, 2014

Very useful thank you!

pencilking2002 commented Sep 24, 2014

Very useful thank you!

@allada

This comment has been minimized.

Show comment
Hide comment
@allada

allada commented Jan 11, 2015

+1

@adius

This comment has been minimized.

Show comment
Hide comment
@adius

adius May 6, 2015

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

adius commented May 6, 2015

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

@PAEz

This comment has been minimized.

Show comment
Hide comment
@PAEz

PAEz Jun 10, 2015

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

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.

Show comment
Hide comment
@jack2ky

jack2ky Oct 28, 2015

@adius Thanks for the clarification. @adius

jack2ky commented Oct 28, 2015

@adius Thanks for the clarification. @adius

@Cicim

This comment has been minimized.

Show comment
Hide comment
@Cicim

Cicim Feb 19, 2016

Great job. Very useful!

Cicim commented Feb 19, 2016

Great job. Very useful!

@jamesseanwright

This comment has been minimized.

Show comment
Hide comment
@jamesseanwright

jamesseanwright Feb 27, 2016

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

jamesseanwright commented Feb 27, 2016

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

@uatec

This comment has been minimized.

Show comment
Hide comment
@uatec

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

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.

Show comment
Hide comment
@Norik-P

Norik-P Mar 18, 2016

Thank you!

Norik-P commented Mar 18, 2016

Thank you!

@renatoargh

This comment has been minimized.

Show comment
Hide comment
@renatoargh

renatoargh Mar 30, 2016

+1 :) helpalot

renatoargh commented Mar 30, 2016

+1 :) helpalot

@akaguny

This comment has been minimized.

Show comment
Hide comment
@akaguny

akaguny Apr 20, 2016

it`s the angle beetween 2 radius vectors?

akaguny commented Apr 20, 2016

it`s the angle beetween 2 radius vectors?

@Samueleroux

This comment has been minimized.

Show comment
Hide comment
@Samueleroux

Samueleroux commented Apr 26, 2016

@Tanvir-rahman

This comment has been minimized.

Show comment
Hide comment
@Tanvir-rahman

Tanvir-rahman commented May 25, 2016

Thanks

@chardos

This comment has been minimized.

Show comment
Hide comment
@chardos

chardos Jul 24, 2016

Thanks man!

chardos commented Jul 24, 2016

Thanks man!

@ThibaultJanBeyer

This comment has been minimized.

Show comment
Hide comment
@ThibaultJanBeyer

ThibaultJanBeyer Jul 25, 2016

Awesome! Thanks

ThibaultJanBeyer commented Jul 25, 2016

Awesome! Thanks

@LiamKarlMitchell

This comment has been minimized.

Show comment
Hide comment
@LiamKarlMitchell

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

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.

Show comment
Hide comment
@grantfree035

grantfree035 commented Nov 14, 2016

Thanks

@onewrinkle

This comment has been minimized.

Show comment
Hide comment
@onewrinkle

onewrinkle Dec 27, 2016

Like it. Thanks

onewrinkle commented Dec 27, 2016

Like it. Thanks

@xbeat

This comment has been minimized.

Show comment
Hide comment
@xbeat

xbeat commented Feb 7, 2017

Thanks

@maggie-lagos

This comment has been minimized.

Show comment
Hide comment
@maggie-lagos

maggie-lagos Apr 6, 2017

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

maggie-lagos commented Apr 6, 2017

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

@baladkb

This comment has been minimized.

Show comment
Hide comment
@baladkb

baladkb commented Apr 17, 2017

Thanks.

@afficiona

This comment has been minimized.

Show comment
Hide comment
@afficiona

afficiona commented May 3, 2017

+1

@Dvoikin

This comment has been minimized.

Show comment
Hide comment
@Dvoikin

Dvoikin commented May 5, 2017

+1

@VladimirMorin

This comment has been minimized.

Show comment
Hide comment
@VladimirMorin

VladimirMorin commented Jun 8, 2017

+1

@guillaumeguerin

This comment has been minimized.

Show comment
Hide comment
@guillaumeguerin

guillaumeguerin commented Jun 8, 2017

Awesome !

@parwoto

This comment has been minimized.

Show comment
Hide comment
@parwoto

parwoto commented Jul 8, 2017

Thanks

@zoutepopcorn

This comment has been minimized.

Show comment
Hide comment
@zoutepopcorn

zoutepopcorn Jul 23, 2017

4 usefull lines of code

Great!

zoutepopcorn commented Jul 23, 2017

4 usefull lines of code

Great!

@vuchastyi

This comment has been minimized.

Show comment
Hide comment
@vuchastyi

vuchastyi commented Aug 13, 2017

#1

@megamanhxh

This comment has been minimized.

Show comment
Hide comment
@megamanhxh

megamanhxh Aug 23, 2017

Thank you for sharing this master piece!

megamanhxh commented Aug 23, 2017

Thank you for sharing this master piece!

@gheja

This comment has been minimized.

Show comment
Hide comment
@gheja

gheja Sep 2, 2017

+1
Thanks!

gheja commented Sep 2, 2017

+1
Thanks!

@v-braun

This comment has been minimized.

Show comment
Hide comment
@v-braun

v-braun Sep 4, 2017

Thx :) 👍

v-braun commented Sep 4, 2017

Thx :) 👍

@nidorx

This comment has been minimized.

Show comment
Hide comment
@nidorx

nidorx commented Oct 31, 2017

+1

@mdermksian

This comment has been minimized.

Show comment
Hide comment
@mdermksian

mdermksian Nov 15, 2017

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

mdermksian commented Nov 15, 2017

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

@lomewolf1776

This comment has been minimized.

Show comment
Hide comment
@lomewolf1776

lomewolf1776 Nov 16, 2017

@mdermksian why aren't you working on resin systems

lomewolf1776 commented Nov 16, 2017

@mdermksian why aren't you working on resin systems

@OfirShukrun

This comment has been minimized.

Show comment
Hide comment
@OfirShukrun

OfirShukrun Dec 29, 2017

Great one! ty very much!

OfirShukrun commented Dec 29, 2017

Great one! ty very much!

@IngIeoAndSpare

This comment has been minimized.

Show comment
Hide comment
@IngIeoAndSpare

IngIeoAndSpare Jan 17, 2018

+1 thanks! bro!

IngIeoAndSpare commented Jan 17, 2018

+1 thanks! bro!

@freshsid

This comment has been minimized.

Show comment
Hide comment
@freshsid

freshsid commented Jan 22, 2018

Thanks!!

@Pickachu

This comment has been minimized.

Show comment
Hide comment
@Pickachu

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

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.

Show comment
Hide comment
@ghost

ghost Apr 17, 2018

THANK YOU!

ghost commented Apr 17, 2018

THANK YOU!

@JoshuaCaputo

This comment has been minimized.

Show comment
Hide comment
@JoshuaCaputo

JoshuaCaputo Apr 24, 2018

Been here 100 times :P

JoshuaCaputo commented Apr 24, 2018

Been here 100 times :P

@ghost

This comment has been minimized.

Show comment
Hide comment
@ghost

ghost May 31, 2018

very useful

ghost commented May 31, 2018

very useful

@signalwerk

This comment has been minimized.

Show comment
Hide comment
@signalwerk

signalwerk Aug 19, 2018

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

signalwerk commented Aug 19, 2018

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

@emrys8

This comment has been minimized.

Show comment
Hide comment
@emrys8

emrys8 Sep 18, 2018

Thank you!

emrys8 commented Sep 18, 2018

Thank you!

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