should add0ms ‣
const expected = [3, 5];
testV2WithAndWithoutDest((a, b, newDst) => {
return vec2.add(a, b, newDst);
}, expected, [1, 2], [2, 3]);
should compute angle0ms ‣
const tests = [
{ a: [1, 0], b: [ 0, 1], expected: Math.PI / 2, },
{ a: [1, 0], b: [-1, 0], expected: Math.PI, },
{ a: [1, 0], b: [ 1, 0], expected: 0, },
{ a: [1, 2], b: [ 4, 5], expected: 0.2110933, },
{ a: [1, 0], b: [ 0, Number.POSITIVE_INFINITY], expected: Math.PI / 2, },
];
for (const {a, b, expected} of tests) {
const av = vec2.create(...a);
const bv = vec2.create(...b);
assertEqualApproximately(vec2.angle(av, bv), expected);
vec2.mulScalar(av, 1000, av);
vec2.mulScalar(bv, 1000, bv);
assertEqualApproximately(vec2.angle(av, bv), expected);
}
should compute ceil0ms ‣
const expected = [2, -1];
testV2WithAndWithoutDest((a, newDst) => {
return vec2.ceil(a, newDst);
}, expected, [1.1, -1.1]);
should compute floor1ms ‣
const expected = [1, -2];
testV2WithAndWithoutDest((a, newDst) => {
return vec2.floor(a, newDst);
}, expected, [1.1, -1.1]);
should compute round0ms ‣
const expected = [1, -1];
testV2WithAndWithoutDest((a, newDst) => {
return vec2.round(a, newDst);
}, expected, [1.1, -1.1]);
should clamp0ms ‣
{
const expected = [1, 0];
testV2WithAndWithoutDest((a, newDst) => {
return vec2.clamp(a, 0, 1, newDst);
}, expected, [2, -1]);
}
{
const expected = [-10, 5];
testV2WithAndWithoutDest((a, newDst) => {
return vec2.clamp(a, -10, 5, newDst);
}, expected, [-22, 50]);
}
should equals approximately0ms ‣
assertTruthy(vec2.equalsApproximately(vec2.create(2, 3), vec2.create(2, 3)));
assertTruthy(vec2.equalsApproximately(vec2.create(2, 3), vec2.create(2 + utils.EPSILON * 0.5, 3)));
assertFalsy(vec2.equalsApproximately(vec2.create(2, 3), vec2.create(2.001, 3)));
should equals0ms ‣
assertTruthy(vec2.equals(vec2.create(2, 3), vec2.create(2, 3)));
assertFalsy(vec2.equals(vec2.create(2, 3), vec2.create(2 + utils.EPSILON * 0.5, 3)));
should subtract0ms ‣
const expected = [-2, -3];
testV2WithAndWithoutDest((a, b, newDst) => {
return vec2.subtract(a, b, newDst);
}, expected, [2, 3], [4, 6]);
should sub0ms ‣
const expected = [-2, -3];
testV2WithAndWithoutDest((a, b, newDst) => {
return vec2.sub(a, b, newDst);
}, expected, [2, 3], [4, 6]);
should lerp0ms ‣
const expected = [3, 4.5];
testV2WithAndWithoutDest((a, b, newDst) => {
return vec2.lerp(a, b, 0.5, newDst);
}, expected, [2, 3], [4, 6]);
should lerp under 00ms ‣
const expected = [0.5, 1.5];
testV2WithAndWithoutDest((a, b, newDst) => {
return vec2.lerp(a, b, -0.5, newDst);
}, expected, [1, 3], [2, 6]);
should lerp over 00ms ‣
const expected = [2.5, 7.5];
testV2WithAndWithoutDest((a, b, newDst) => {
return vec2.lerp(a, b, 1.5, newDst);
}, expected, [1, 3], [2, 6]);
should multiply by scalar0ms ‣
const expected = [4, 6];
testV2WithAndWithoutDest((a, newDst) => {
return vec2.mulScalar(a, 2, newDst);
}, expected, [2, 3]);
should scale0ms ‣
const expected = [4, 6];
testV2WithAndWithoutDest((a, newDst) => {
return vec2.scale(a, 2, newDst);
}, expected, [2, 3]);
should add scaled0ms ‣
const expected = [10, 15];
testV2WithAndWithoutDest((a, newDst) => {
return vec2.addScaled(a, [4, 6], 2, newDst);
}, expected, [2, 3]);
should divide by scalar0ms ‣
const expected = [0.5, 1.5];
testV2WithAndWithoutDest((a, newDst) => {
return vec2.divScalar(a, 2, newDst);
}, expected, [1, 3]);
should inverse0ms ‣
const expected = [1 / 3, 1 / -4];
testV2WithAndWithoutDest((a, newDst) => {
return vec2.inverse(a, newDst);
}, expected, [3, -4]);
should cross0ms ‣
const expected = [
0,
0,
2 * 5 - 3 * 4,
];
const c = vec2.cross(vec2.create(2, 3), vec2.create(4, 5));
assertArrayEqualApproximately(c, expected);
const d = vec2.cross(vec2.create(3, 2), vec2.create(4, 5), c);
assertStrictEqual(d, c);
const expected2 = [
0,
0,
3 * 5 - 2 * 4,
];
assertArrayEqualApproximately(c, expected2);
should compute dot product0ms ‣
const expected = 2 * 4 + 3 * 6;
const value = vec2.dot(vec2.create(2, 3), vec2.create(4, 6));
assertStrictEqual(value, expected);
should compute length0ms ‣
const expected = Math.sqrt(2 * 2 + 3 * 3);
const value = vec2.length(vec2.create(2, 3));
assertStrictEqual(value, expected);
should compute length squared0ms ‣
const expected = 2 * 2 + 3 * 3;
const value = vec2.lengthSq(vec2.create(2, 3));
assertStrictEqual(value, expected);
should compute len0ms ‣
const expected = Math.sqrt(2 * 2 + 3 * 3);
const value = vec2.len(vec2.create(2, 3));
assertStrictEqual(value, expected);
should compute lenSq0ms ‣
const expected = 2 * 2 + 3 * 3;
const value = vec2.lenSq(vec2.create(2, 3));
assertStrictEqual(value, expected);
should compute distance0ms ‣
const expected = Math.sqrt(3 * 3 + 4 * 4);
const value = vec2.distance(vec2.create(2, 3), [5, 7]);
assertStrictEqual(value, expected);
should compute distance squared0ms ‣
const expected = 3 * 3 + 4 * 4;
const value = vec2.distanceSq(vec2.create(2, 3), [5, 7]);
assertStrictEqual(value, expected);
should compute dist0ms ‣
const expected = Math.sqrt(3 * 3 + 4 * 4);
const value = vec2.dist(vec2.create(2, 3), [5, 7]);
assertStrictEqual(value, expected);
should compute dist squared0ms ‣
const expected = 3 * 3 + 4 * 4;
const value = vec2.distSq(vec2.create(2, 3), [5, 7]);
assertStrictEqual(value, expected);
should normalize0ms ‣
const length = Math.sqrt(2 * 2 + 3 * 3);
const expected = [
2 / length,
3 / length,
];
testV2WithAndWithoutDest((a, newDst) => {
return vec2.normalize(a, newDst);
}, expected, [2, 3]);
should negate0ms ‣
const expected = [-2, 3];
testV2WithAndWithoutDest((a, newDst) => {
return vec2.negate(a, newDst);
}, expected, [2, -3]);
should copy0ms ‣
const expected = [2, 3];
const v = vec2.create(2, 3);
testV2WithAndWithoutDest((a, newDst) => {
const result = vec2.copy(a, newDst);
assertStrictNotEqual(result, v);
return result;
}, expected, [2, 3]);
should clone0ms ‣
const expected = [2, 3];
const v = vec2.create(2, 3);
testV2WithAndWithoutDest((a, newDst) => {
const result = vec2.clone(a, newDst);
assertStrictNotEqual(result, v);
return result;
}, expected, [2, 3]);
should set0ms ‣
const expected = [2, 3];
testV2WithAndWithoutDest((a, b, newDst) => {
return vec2.set(a, b, newDst);
}, expected, 2, 3);
should multiply0ms ‣
const expected = [8, 18];
testV2WithAndWithoutDest((a, b, newDst) => {
return vec2.multiply(a, b, newDst);
}, expected, [2, 3], [4, 6]);
should mul0ms ‣
const expected = [8, 18];
testV2WithAndWithoutDest((a, b, newDst) => {
return vec2.mul(a, b, newDst);
}, expected, [2, 3], [4, 6]);
should divide0ms ‣
const expected = [2 / 3, 3 / 4];
testV2WithAndWithoutDest((a, b, newDst) => {
return vec2.divide(a, b, newDst);
}, expected, [2, 3], [3, 4]);
should div1ms ‣
const expected = [2 / 3, 3 / 4];
testV2WithAndWithoutDest((a, b, newDst) => {
return vec2.div(a, b, newDst);
}, expected, [2, 3], [3, 4]);
should fromValues0ms ‣
const expected = vec2.create(2, 3);
const v1 = vec2.fromValues(2, 3);
assertEqual(v1, expected);
should random1ms ‣
for (let i = 0; i < 100; ++i) {
const v1 = vec2.random();
assertEqualApproximately(vec2.length(v1), 1);
const v2 = vec2.random(2);
assertEqualApproximately(vec2.length(v2), 2);
const vp5 = vec2.random(0.5);
assertEqualApproximately(vec2.length(vp5), 0.5);
const vd = vec2.create();
const vn = vec2.random(3, vd);
assertStrictEqual(vd, vn);
assertEqualApproximately(vec2.length(3, vd), 3);
}
should transform by 3x30ms ‣
const expected = [16, 17];
testV2WithAndWithoutDest((a, newDst) => {
const m = [
4, 0, 0, 11,
0, 5, 0, 12,
8, 2, 0, 13,
];
return vec2.transformMat3(a, m, newDst);
}, expected, [2, 3]);
should transform by 4x40ms ‣
const expected = [6, 11];
testV2WithAndWithoutDest((a, newDst) => {
const m = [
1, 0, 0, 0,
0, 2, 0, 0,
0, 0, 3, 0,
4, 5, 6, 1,
];
return vec2.transformMat4(a, m, newDst);
}, expected, [2, 3]);
should zero0ms ‣
const v = vec2.zero();
assertEqual(v, [0, 0]);
const v2 = vec2.create(2, 3);
const vn = vec2.zero(v2);
assertStrictEqual(v2, vn);
assertEqual(v2, [0, 0]);
should return the rotated vector0ms ‣
const expected = [0, -1];
testV2WithAndWithoutDestApprox((a, b, angle, newDst) => {
return vec2.rotate(a, b, angle, newDst);
}, expected, [0, 1], [0, 0], Math.PI);
should return the rotated vector0ms ‣
const expected = [-6, -5];
testV2WithAndWithoutDestApprox((a, b, angle, newDst) => {
return vec2.rotate(a, b, angle, newDst);
}, expected, [6, -5], [0, -5], Math.PI);
should return the lengthened vector0ms ‣
const expected = [10.323759005323593, 10.323759005323593];
testV2WithAndWithoutDestApprox(
(a, len, newDst) => vec2.setLength(a, len, newDst),
expected,
[1, 1], 14.6);
should shorten the vector1ms ‣
const expected = [2.82842712474619, 2.82842712474619];
testV2WithAndWithoutDestApprox(
(a, len, newDst) => vec2.truncate(a, len, newDst),
expected,
[10.323759005323593, 10.323759005323593],
4.0);
should preserve the vector when shorter than maxLen0ms ‣
const expected = [11, 12];
testV2WithAndWithoutDestApprox(
(a, len, newDst) => vec2.truncate(a, len, newDst),
expected,
[11, 12],
14.6);
should return the midpoint0ms ‣
const expected = [5, 5];
testV2WithAndWithoutDest(
(a, b, newDst) => vec2.midpoint(a, b, newDst),
expected,
[0, 0], [10, 10]
);
should handle negatives1ms ‣
const expected = [10, 10];
testV2WithAndWithoutDest(
(a, b, newDst) => vec2.midpoint(a, b, newDst),
expected,
[-10, -20], [30, 40]);