Last active
December 10, 2015 19:48
-
-
Save afreeland/4484102 to your computer and use it in GitHub Desktop.
JavaScript: Get Angle By Two Vectors On Coordinate Plan
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
/** | |
* Defines an x,y coordinate | |
* @param {float} x coordinate | |
* @param {float} y coordinate | |
*/ | |
function Point (x, y) { | |
this.x = x; | |
this.y = y; | |
} | |
/** | |
* Get angle by two vectors on a coordinate plan | |
* This works regardless of origin | |
* @param {Point} P2 x, y | |
* @param {Point} P1 x, y --This can be thought of as the new origin point | |
* @param {bool} UseRadians [The default is degrees, however to return radians pass in true] | |
*/ | |
function AngleByVectors (P1, P2, UseRadians) { | |
var DeltaY = P2.y - P1.y, | |
DeltaX = P2.x - P1.x; | |
return !UseRadians ? Math.atan2(DeltaY, DeltaX) * 180/Math.PI : Math.atan2(DeltaY, DeltaX); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment