Skip to content

Instantly share code, notes, and snippets.

@jeremyjackson89
Created August 26, 2017 03:16
Show Gist options
  • Save jeremyjackson89/9b977104aeaeb1463ea69e19f91602b2 to your computer and use it in GitHub Desktop.
Save jeremyjackson89/9b977104aeaeb1463ea69e19f91602b2 to your computer and use it in GitHub Desktop.
function getNormalized(point) {
var norm = Math.sqrt((point.x * point.x) + (point.y * point.y));
var normalized = { x: 0, y: 0 };
if (norm != 0) {
normalized.x = point.x / norm;
normalized.y = point.y / norm;
}
return normalized;
}
function subtractVectors(v1, v2) {
return {
x: (v1.x - v2.x),
y: (v1.y - v2.y)
};
}
function getDistance(v1, v2) {
var newV = subtractVectors(v1, v2);
return Math.abs(Math.sqrt(newV.x * newV.x + newV.y * newV.y));
}
function dotProduct(vectorA, vectorB) {
return (vectorA.x * vectorB.x) + (vectorA.y * vectorB.y);
}
function getDegrees(angle) {
return angle * (180 / Math.PI);
}
function test() {
var guardPos = { x: 1, y: 3 };
var guardFacing = { x: 1, y: 1 };
var heroPos = { x: 3, y: 2 };
var guardFacingNormalized = getNormalized(guardFacing);
console.log("guardFacingNormalized", guardFacingNormalized);
var heroMinusGuardPos = subtractVectors(heroPos, guardPos);
console.log("heroMinusGuardPos", heroMinusGuardPos);
var guardToHero = getNormalized(heroMinusGuardPos);
console.log("guardToHero", guardToHero);
var angle = Math.acos(dotProduct(guardFacingNormalized, guardToHero));
console.log("angle is ", getDegrees(angle));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment