feat: Vector.angle

This commit is contained in:
cha0s 2019-03-22 11:23:55 -05:00
parent c6e14e5eb8
commit 3566640678

View File

@ -255,6 +255,10 @@ export function toDirection4(vector) {
}
}
export function angle(v) {
return Math.atan2(v[1], v[0]);
}
// Convert a vector to an 8-direction. An 8-direction is:
//
// * 0: Up
@ -274,7 +278,7 @@ export function toDirection4(vector) {
export function toDirection8(v) {
v = hypotenuse(v);
// Orient radians.
let rad = (TWO_PI + Math.atan2(v[1], v[0])) % TWO_PI;
let rad = (TWO_PI + angle(v)) % TWO_PI;
rad = (rad + HALF_PI) % TWO_PI;
// Truncate.
rad = Math.floor(rad * 100000) / 100000;