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

commented Aug 7, 2014

Thanks!

@pencilking2002

This comment has been minimized.

Copy link

commented Sep 24, 2014

Very useful thank you!

@allada

This comment has been minimized.

Copy link

commented Jan 11, 2015

+1

@adius

This comment has been minimized.

Copy link

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

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

commented Oct 28, 2015

@adius Thanks for the clarification. @adius

@Cicim

This comment has been minimized.

Copy link

commented Feb 19, 2016

Great job. Very useful!

@jamesseanwright

This comment has been minimized.

Copy link

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

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

commented Mar 18, 2016

Thank you!

@renatoargh

This comment has been minimized.

Copy link

commented Mar 30, 2016

+1 :) helpalot

@akaguny

This comment has been minimized.

Copy link

commented Apr 20, 2016

it`s the angle beetween 2 radius vectors?

@Samueleroux

This comment has been minimized.

Copy link

commented Apr 26, 2016

@Tanvir-rahman

This comment has been minimized.

Copy link

commented May 25, 2016

Thanks

@chardos

This comment has been minimized.

Copy link

commented Jul 24, 2016

Thanks man!

@ThibaultJanBeyer

This comment has been minimized.

Copy link

commented Jul 25, 2016

Awesome! Thanks

@LiamKarlMitchell

This comment has been minimized.

Copy link

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

commented Nov 14, 2016

Thanks

@onewrinkle

This comment has been minimized.

Copy link

commented Dec 27, 2016

Like it. Thanks

@xbeat

This comment has been minimized.

Copy link

commented Feb 7, 2017

Thanks

@maggie-lagos

This comment has been minimized.

Copy link

commented Apr 6, 2017

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

@baladkb

This comment has been minimized.

Copy link

commented Apr 17, 2017

Thanks.

@afficiona

This comment has been minimized.

Copy link

commented May 3, 2017

+1

@Dvoikin

This comment has been minimized.

Copy link

commented May 5, 2017

+1

@VladimirMorin

This comment has been minimized.

Copy link

commented Jun 8, 2017

+1

@guillaumeguerin

This comment has been minimized.

Copy link

commented Jun 8, 2017

Awesome !

@parwoto

This comment has been minimized.

Copy link

commented Jul 8, 2017

Thanks

@zoutepopcorn

This comment has been minimized.

Copy link

commented Jul 23, 2017

4 usefull lines of code

Great!

@vuchastyi

This comment has been minimized.

Copy link

commented Aug 13, 2017

#1

@megamanhxh

This comment has been minimized.

Copy link

commented Aug 23, 2017

Thank you for sharing this master piece!

@gheja

This comment has been minimized.

Copy link

commented Sep 2, 2017

+1
Thanks!

@v-braun

This comment has been minimized.

Copy link

commented Sep 4, 2017

Thx :) 👍

@nidorx

This comment has been minimized.

Copy link

commented Oct 31, 2017

+1

@mdermksian

This comment has been minimized.

Copy link

commented Nov 15, 2017

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

@lomewolf1776

This comment has been minimized.

Copy link

commented Nov 16, 2017

@mdermksian why aren't you working on resin systems

@OfirShukrun

This comment has been minimized.

Copy link

commented Dec 29, 2017

Great one! ty very much!

@IngIeoAndSpare

This comment has been minimized.

Copy link

commented Jan 17, 2018

+1 thanks! bro!

@freshsid

This comment has been minimized.

Copy link

commented Jan 22, 2018

Thanks!!

@Pickachu

This comment has been minimized.

Copy link

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

commented Apr 17, 2018

THANK YOU!

@JoshuaCaputo

This comment has been minimized.

Copy link

commented Apr 24, 2018

Been here 100 times :P

@ghost

This comment has been minimized.

Copy link

commented May 31, 2018

very useful

@signalwerk

This comment has been minimized.

Copy link

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

commented Sep 18, 2018

Thank you!

@iamusbah

This comment has been minimized.

Copy link

commented Oct 16, 2018

thank you very much, simple and efficient

@dpleshkov

This comment has been minimized.

Copy link

commented Nov 21, 2018

Big thanks

@audinue

This comment has been minimized.

Copy link

commented Jan 23, 2019

Thanks you for this useful snippet.

@cooperbrown9

This comment has been minimized.

Copy link

commented Jan 28, 2019

Thank u!

@ashokadichill

This comment has been minimized.

Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.