refactor: magnitude, dot

This commit is contained in:
cha0s 2019-03-27 18:06:55 -05:00
parent b042e1b1c0
commit c556c90602

View File

@ -61,10 +61,8 @@ export function mod(l, r) {
//
// avocado> Vector.magnitude [0, 0], [1, 1]
// 1.4142135623730951
export function magnitude (l, r) {
const xd = l[0] - r[0];
const yd = l[1] - r[1];
return Math.sqrt(xd * xd + yd * yd);
export function magnitude(l, r) {
return Math.sqrt(dot(sub(l, r)));
}
// Get the minimum values from two vectors.
@ -138,11 +136,14 @@ export function round(v) {
return [Math.round(v[0]), Math.round(v[1])];
}
// Get the dot product of two vectors.
// Get the dot product of two vectors. If only one gievn, dupe it.
//
// avocado> Vector.dot [2, 3], [4, 5]
// 23
export function dot(l, r) {
if (!r) {
r = l;
}
return l[0] * r[0] + l[1] * r[1];
}
@ -151,7 +152,7 @@ export function dot(l, r) {
// avocado> Vector.normalize [.5, .7]
// [0.5812381937190965, 0.813733471206735]
export function normalize(v) {
const dp = dot(v, v);
const dp = dot(v);
if (0 === dp) {
return [0, 0];
}