-
-
Save darkworks/cc8d4949d7880cf1088899dbc2fed16e to your computer and use it in GitHub Desktop.
JavaScript: Find the angle between two points
This file contains hidden or 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
// FInd Angle between 2 point ... 0 to 90 | |
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; | |
// FInd Angle between two points 0 to 360 | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment