Instantly share code, notes, and snippets.

# conorbuck/angle-between-points.js

Created May 5, 2012 22:51
JavaScript: Find the angle between two points
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 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;

### mdermksian commented Nov 15, 2017

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

### lomewolf1776 commented Nov 16, 2017

@mdermksian why aren't you working on resin systems

### OfirShukrun commented Dec 29, 2017

Great one! ty very much!

+1 thanks! bro!

Thanks!!

### Pickachu commented Feb 7, 2018 • edited

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.

THANK YOU!

### JoshuaCaputo commented Apr 24, 2018

Been here 100 times :P

very useful

### signalwerk commented Aug 19, 2018

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

Thank you!

### iamusbah commented Oct 16, 2018

thank you very much, simple and efficient

Big thanks

### audinue commented Jan 23, 2019

Thanks you for this useful snippet.

Thank u!

### Adichilla commented Mar 13, 2019 • edited

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.

+∞

Thanks!

Thanks!

### reyn-nova commented Sep 15, 2020

Massive thanks!!!!!

Thanks

Thank you!

### 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 😕

very useful!

THANK YOU!!!

### modellking commented Nov 16, 2021

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

@Extex101

1. think of both as being at origin;
2. force them into the plane along 2 axis (set the last dimension to 0, because length is irrelevant) (which one depends on whether you want pitch yaw or roll and your coordinate system);
3. solve using 2D trigonometry (just the angle between these)

alternatively you could use a framework that uses these values to store rotation anyway, but beware of gimbal lock

### arteuspw commented Dec 28, 2022

thank you so much <3

Omg thank you!!!

### drewbaker commented Mar 18, 2023

Would love a version of this that has 0 degrees at due north if anyone knows how to do that...