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 commented Aug 7, 2014

Thanks!

@pencilking2002

This comment has been minimized.

Copy link

pencilking2002 commented Sep 24, 2014

Very useful thank you!

@allada

This comment has been minimized.

Copy link

allada commented Jan 11, 2015

+1

@adius

This comment has been minimized.

Copy link

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 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 commented Oct 28, 2015

@adius Thanks for the clarification. @adius

@Cicim

This comment has been minimized.

Copy link

Cicim commented Feb 19, 2016

Great job. Very useful!

@jamesseanwright

This comment has been minimized.

Copy link

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

Thank you!

@renatoargh

This comment has been minimized.

Copy link

renatoargh commented Mar 30, 2016

+1 :) helpalot

@akaguny

This comment has been minimized.

Copy link

akaguny commented Apr 20, 2016

it`s the angle beetween 2 radius vectors?

@Samueleroux

This comment has been minimized.

Copy link

Samueleroux commented Apr 26, 2016

@Tanvir-rahman

This comment has been minimized.

Copy link

Tanvir-rahman commented May 25, 2016

Thanks

@chardos

This comment has been minimized.

Copy link

chardos commented Jul 24, 2016

Thanks man!

@ThibaultJanBeyer

This comment has been minimized.

Copy link

ThibaultJanBeyer commented Jul 25, 2016

Awesome! Thanks

@LiamKarlMitchell

This comment has been minimized.

Copy link

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 commented Nov 14, 2016

Thanks

@onewrinkle

This comment has been minimized.

Copy link

onewrinkle commented Dec 27, 2016

Like it. Thanks

@xbeat

This comment has been minimized.

Copy link

xbeat commented Feb 7, 2017

Thanks

@maggie-lagos

This comment has been minimized.

Copy link

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 commented Apr 17, 2017

Thanks.

@afficiona

This comment has been minimized.

Copy link

afficiona commented May 3, 2017

+1

@Dvoikin

This comment has been minimized.

Copy link

Dvoikin commented May 5, 2017

+1

@VladimirMorin

This comment has been minimized.

Copy link

VladimirMorin commented Jun 8, 2017

+1

@guillaumeguerin

This comment has been minimized.

Copy link

guillaumeguerin commented Jun 8, 2017

Awesome !

@parwoto

This comment has been minimized.

Copy link

parwoto commented Jul 8, 2017

Thanks

@zoutepopcorn

This comment has been minimized.

Copy link

zoutepopcorn commented Jul 23, 2017

4 usefull lines of code

Great!

@vuchastyi

This comment has been minimized.

Copy link

vuchastyi commented Aug 13, 2017

#1

@megamanhxh

This comment has been minimized.

Copy link

megamanhxh commented Aug 23, 2017

Thank you for sharing this master piece!

@gheja

This comment has been minimized.

Copy link

gheja commented Sep 2, 2017

+1
Thanks!

@v-braun

This comment has been minimized.

Copy link

v-braun commented Sep 4, 2017

Thx :) 👍

@nidorx

This comment has been minimized.

Copy link

nidorx commented Oct 31, 2017

+1

@mdermksian

This comment has been minimized.

Copy link

mdermksian commented Nov 15, 2017

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

@lomewolf1776

This comment has been minimized.

Copy link

lomewolf1776 commented Nov 16, 2017

@mdermksian why aren't you working on resin systems

@OfirShukrun

This comment has been minimized.

Copy link

OfirShukrun commented Dec 29, 2017

Great one! ty very much!

@IngIeoAndSpare

This comment has been minimized.

Copy link

IngIeoAndSpare commented Jan 17, 2018

+1 thanks! bro!

@freshsid

This comment has been minimized.

Copy link

freshsid commented Jan 22, 2018

Thanks!!

@Pickachu

This comment has been minimized.

Copy link

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 commented Apr 17, 2018

THANK YOU!

@JoshuaCaputo

This comment has been minimized.

Copy link

JoshuaCaputo commented Apr 24, 2018

Been here 100 times :P

@ghost

This comment has been minimized.

Copy link

ghost commented May 31, 2018

very useful

@signalwerk

This comment has been minimized.

Copy link

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.

Copy link

emrys8 commented Sep 18, 2018

Thank you!

@iamusbah

This comment has been minimized.

Copy link

iamusbah commented Oct 16, 2018

thank you very much, simple and efficient

@dpleshkov

This comment has been minimized.

Copy link

dpleshkov commented Nov 21, 2018

Big thanks

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