Create a gist now

Instantly share code, notes, and snippets.

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

Thanks!

@pencilking2002

Very useful thank you!

@allada
allada commented Jan 11, 2015

+1

@adius
adius commented May 6, 2015

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

@PAEz
PAEz commented Jun 10, 2015

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

@jack2ky
jack2ky commented Oct 28, 2015

@adius Thanks for the clarification. @adius

@McFarts
McFarts commented Dec 6, 2015

thx sir i love you

@Cicim
Cicim commented Feb 19, 2016

Great job. Very useful!

@jamesseanwright

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

@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
Norik-P commented Mar 18, 2016

Thank you!

@renatoargh

+1 :) helpalot

@akaguny
akaguny commented Apr 20, 2016

it`s the angle beetween 2 radius vectors?

@Samueleroux
Samueleroux commented Apr 26, 2016 edited
@Tanvir-rahman

Thanks

@chardos
chardos commented Jul 24, 2016

Thanks man!

@ThibaultJanBeyer

Awesome! Thanks

@LiamKarlMitchell
LiamKarlMitchell commented Aug 26, 2016 edited

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

Thanks

@onewrinkle

Like it. Thanks

@xbeat
xbeat commented Feb 7, 2017

Thanks

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