🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Sign inDemoInstall
Socket

vec3

Package Overview
Dependencies
Maintainers
2
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

vec3 - npm Package Compare versions

Comparing version

to
0.1.5

7

HISTORY.md

@@ -0,1 +1,8 @@

## 0.1.5
* normalize() : like unit() but do not create a new vector (in-place)
* dot(other) : return the dot product of the vector with other
* cross(other) : return the cross product of the vector with other
## 0.1.4

@@ -2,0 +9,0 @@

@@ -48,2 +48,6 @@ declare class Vec3 {

dot(other: Vec3): number;
cross(other: Vec3): Vec3;
norm(): number;

@@ -53,2 +57,4 @@

normalize(): Vec3;
scale(scalar): this;

@@ -55,0 +61,0 @@

344

index.js

@@ -1,167 +0,211 @@

module.exports = v;
v.Vec3 = Vec3;
const re = /\((-?[.\d]+), (-?[.\d]+), (-?[.\d]+)\)/
var re = /\((-?[.\d]+), (-?[.\d]+), (-?[.\d]+)\)/;
class Vec3 {
constructor (x, y, z) {
this.x = x
this.y = y
this.z = z
}
function Vec3(x, y, z) {
this.x = x;
this.y = y;
this.z = z;
set (x, y, z) {
this.x = x
this.y = y
this.z = z
return this
}
update (other) {
this.x = other.x
this.y = other.y
this.z = other.z
return this
}
floored () {
return new Vec3(Math.floor(this.x), Math.floor(this.y), Math.floor(this.z))
}
floor () {
this.x = Math.floor(this.x)
this.y = Math.floor(this.y)
this.z = Math.floor(this.z)
return this
}
offset (dx, dy, dz) {
return new Vec3(this.x + dx, this.y + dy, this.z + dz)
}
translate (dx, dy, dz) {
this.x += dx
this.y += dy
this.z += dz
return this
}
add (other) {
this.x += other.x
this.y += other.y
this.z += other.z
return this
}
subtract (other) {
this.x -= other.x
this.y -= other.y
this.z -= other.z
return this
}
plus (other) {
return this.offset(other.x, other.y, other.z)
}
minus (other) {
return this.offset(-other.x, -other.y, -other.z)
}
scaled (scalar) {
return new Vec3(this.x * scalar, this.y * scalar, this.z * scalar)
}
abs () {
return new Vec3(Math.abs(this.x), Math.abs(this.y), Math.abs(this.z))
}
volume () {
return this.x * this.y * this.z
}
modulus (other) {
return new Vec3(
euclideanMod(this.x, other.x),
euclideanMod(this.y, other.y),
euclideanMod(this.z, other.z))
}
distanceTo (other) {
var dx = other.x - this.x
var dy = other.y - this.y
var dz = other.z - this.z
return Math.sqrt(dx * dx + dy * dy + dz * dz)
}
equals (other) {
return this.x === other.x && this.y === other.y && this.z === other.z
}
toString () {
return '(' + this.x + ', ' + this.y + ', ' + this.z + ')'
}
clone () {
return this.offset(0, 0, 0)
}
min (other) {
return new Vec3(Math.min(this.x, other.x), Math.min(this.y, other.y), Math.min(this.z, other.z))
}
max (other) {
return new Vec3(Math.max(this.x, other.x), Math.max(this.y, other.y), Math.max(this.z, other.z))
}
norm () {
return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z)
}
dot (other) {
return this.x * other.x + this.y * other.y + this.z * other.z
}
cross (other) {
return new Vec3(this.y * other.z - this.z * other.y, this.z * other.x - this.x * other.z, this.x * other.y - this.y * other.x)
}
unit () {
const norm = this.norm()
if (norm === 0) {
return this.clone()
} else {
return this.scaled(1 / norm)
}
}
normalize () {
const norm = this.norm()
if (norm !== 0) {
this.x /= norm
this.y /= norm
this.z /= norm
}
return this
}
scale (scalar) {
this.x *= scalar
this.y *= scalar
this.z *= scalar
return this
}
xyDistanceTo (other) {
var dx = other.x - this.x
var dy = other.y - this.y
return Math.sqrt(dx * dx + dy * dy)
}
xzDistanceTo (other) {
var dx = other.x - this.x
var dz = other.z - this.z
return Math.sqrt(dx * dx + dz * dz)
}
yzDistanceTo (other) {
var dy = other.y - this.y
var dz = other.z - this.z
return Math.sqrt(dy * dy + dz * dz)
}
innerProduct (other) {
return this.x * other.x + this.y * other.y + this.z * other.z
}
manhattanDistanceTo (other) {
return Math.abs(other.x - this.x) + Math.abs(other.y - this.y) + Math.abs(other.z - this.z)
}
toArray () {
return [this.x, this.y, this.z]
}
}
function v(x, y, z) {
function v (x, y, z) {
if (x == null) {
return new Vec3(0, 0, 0);
return new Vec3(0, 0, 0)
} else if (Array.isArray(x)) {
return new Vec3(parseFloat(x[0], 10), parseFloat(x[1], 10), parseFloat(x[2], 10));
return new Vec3(parseFloat(x[0], 10), parseFloat(x[1], 10), parseFloat(x[2], 10))
} else if (typeof x === 'object') {
return new Vec3(parseFloat(x.x, 10), parseFloat(x.y, 10), parseFloat(x.z, 10));
return new Vec3(parseFloat(x.x, 10), parseFloat(x.y, 10), parseFloat(x.z, 10))
} else if (typeof x === 'string' && y == null) {
var match = x.match(re);
var match = x.match(re)
if (match) {
return new Vec3(
parseFloat(match[1], 10),
parseFloat(match[2], 10),
parseFloat(match[3], 10));
parseFloat(match[1], 10),
parseFloat(match[2], 10),
parseFloat(match[3], 10))
} else {
throw new Error("vec3: cannot parse: " + x);
throw new Error('vec3: cannot parse: ' + x)
}
} else {
return new Vec3(parseFloat(x, 10), parseFloat(y, 10), parseFloat(z, 10));
return new Vec3(parseFloat(x, 10), parseFloat(y, 10), parseFloat(z, 10))
}
}
Vec3.prototype.set = function(x, y, z) {
this.x = x;
this.y = y;
this.z = z;
return this;
};
function euclideanMod (numerator, denominator) {
var result = numerator % denominator
return result < 0 ? result + denominator : result
}
Vec3.prototype.update = function(other) {
this.x = other.x;
this.y = other.y;
this.z = other.z;
return this;
};
Vec3.prototype.floored = function() {
return new Vec3(Math.floor(this.x), Math.floor(this.y), Math.floor(this.z));
};
Vec3.prototype.floor = function() {
this.x = Math.floor(this.x);
this.y = Math.floor(this.y);
this.z = Math.floor(this.z);
return this;
};
Vec3.prototype.offset = function(dx, dy, dz) {
return new Vec3(this.x + dx, this.y + dy, this.z + dz);
};
Vec3.prototype.translate = function(dx, dy, dz) {
this.x += dx;
this.y += dy;
this.z += dz;
return this;
};
Vec3.prototype.add = function(other) {
this.x += other.x;
this.y += other.y;
this.z += other.z;
return this;
};
Vec3.prototype.subtract = function(other) {
this.x -= other.x;
this.y -= other.y;
this.z -= other.z;
return this;
};
Vec3.prototype.plus = function(other) {
return this.offset(other.x, other.y, other.z);
};
Vec3.prototype.minus = function(other) {
return this.offset(-other.x, -other.y, -other.z);
};
Vec3.prototype.scaled = function(scalar) {
return new Vec3(this.x * scalar, this.y * scalar, this.z * scalar);
};
Vec3.prototype.abs = function() {
return new Vec3(Math.abs(this.x), Math.abs(this.y), Math.abs(this.z));
};
Vec3.prototype.volume = function() {
return this.x * this.y * this.z;
};
Vec3.prototype.modulus = function(other) {
return new Vec3(
euclideanMod(this.x, other.x),
euclideanMod(this.y, other.y),
euclideanMod(this.z, other.z));
};
Vec3.prototype.distanceTo = function(other) {
var dx = other.x - this.x;
var dy = other.y - this.y;
var dz = other.z - this.z;
return Math.sqrt(dx * dx + dy * dy + dz * dz);
};
Vec3.prototype.equals = function(other) {
return this.x === other.x && this.y === other.y && this.z === other.z;
};
Vec3.prototype.toString = function() {
return "(" + this.x + ", " + this.y + ", " + this.z + ")";
};
Vec3.prototype.clone = function() {
return this.offset(0, 0, 0);
};
Vec3.prototype.min = function(other) {
return new Vec3(Math.min(this.x, other.x), Math.min(this.y, other.y), Math.min(this.z, other.z));
};
Vec3.prototype.max = function(other) {
return new Vec3(Math.max(this.x, other.x), Math.max(this.y, other.y), Math.max(this.z, other.z));
};
Vec3.prototype.norm = function() {
return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
};
Vec3.prototype.unit = function() {
var norm = this.norm();
if(norm === 0) {
return this.clone();
} else {
return this.scaled(1 / norm);
}
};
Vec3.prototype.scale = function(scalar) {
this.x *= scalar;
this.y *= scalar;
this.z *= scalar;
return this;
};
Vec3.prototype.xyDistanceTo = function(other) {
var dx = other.x - this.x;
var dy = other.y - this.y;
return Math.sqrt(dx * dx + dy * dy);
};
Vec3.prototype.xzDistanceTo = function(other) {
var dx = other.x - this.x;
var dz = other.z - this.z;
return Math.sqrt(dx * dx + dz * dz);
};
Vec3.prototype.yzDistanceTo = function(other) {
var dy = other.y - this.y;
var dz = other.z - this.z;
return Math.sqrt(dy * dy + dz * dz);
};
Vec3.prototype.innerProduct = function(other) {
return this.x * other.x + this.y * other.y + this.z * other.z;
};
Vec3.prototype.manhattanDistanceTo = function(other) {
return Math.abs(other.x - this.x) + Math.abs(other.y - this.y) + Math.abs(other.z - this.z);
};
Vec3.prototype.toArray = function() {
return [this.x, this.y, this.z];
};
function euclideanMod(numerator, denominator) {
var result = numerator % denominator;
return result < 0 ? result + denominator : result;
}
module.exports = v
v.Vec3 = Vec3
{
"name": "vec3",
"version": "0.1.4",
"version": "0.1.5",
"description": "3d vector math with good unit tests",

@@ -8,3 +8,6 @@ "main": "index.js",

"scripts": {
"test": "mocha --reporter spec"
"test": "mocha --reporter spec",
"pretest": "npm run lint",
"lint": "standard",
"fix": "standard --fix"
},

@@ -17,4 +20,7 @@ "keywords": [

"devDependencies": {
"mocha": "~1.7.4"
"mocha": "^7.2.0"
},
"dependencies": {
"standard": "^14.3.4"
}
}

@@ -31,40 +31,46 @@ # vec3

v()
√ no args
√ x, y, z
√ array
√ object
✓ no args
✓ x, y, z
✓ array
✓ object
✓ string coords
✓ deserialize
✓ invalid deserialize
vec3
√ floored
√ floor
√ offset
√ translate
√ plus
√ minus
√ scaled
√ abs
√ distanceTo
√ equals
√ toString
√ clone
√ add
√ subtract
√ set
√ modulus
√ volume
√ min
√ max
√ update
√ norm
√ unit
√ scale
√ xyDistanceTo
√ xzDistanceTo
√ yzDistanceTo
√ innerProduct
√ manhattanDistanceTo
√ toArray
✓ floored
✓ floor
✓ offset
✓ translate
✓ plus
✓ minus
✓ scaled
✓ abs
✓ distanceTo
✓ equals
✓ toString
✓ clone
✓ add
✓ subtract
✓ set
✓ modulus
✓ volume
✓ min
✓ max
✓ update
✓ norm
✓ dot
✓ cross
✓ unit
✓ normalize
✓ scale
✓ xyDistanceTo
✓ xzDistanceTo
✓ yzDistanceTo
✓ innerProduct
✓ manhattanDistanceTo
✓ toArray
36 tests complete
39 passing
```

@@ -71,0 +77,0 @@

@@ -0,305 +1,330 @@

/* eslint-env mocha */
var v = require('../')
, Vec3 = v.Vec3
, assert = require('assert')
var Vec3 = v.Vec3
var assert = require('assert')
describe("v()", function() {
it("no args", function() {
var v1 = v();
assert.strictEqual(v1.x, 0);
assert.strictEqual(v1.y, 0);
assert.strictEqual(v1.z, 0);
});
it("x, y, z", function() {
var v1 = v(-1, 5, 10.10);
assert.strictEqual(v1.x, -1);
assert.strictEqual(v1.y, 5);
assert.strictEqual(v1.z, 10.10);
});
it("array", function() {
var v1 = v([4, 5, 6]);
assert.strictEqual(v1.x, 4);
assert.strictEqual(v1.y, 5);
assert.strictEqual(v1.z, 6);
});
it("object", function() {
var v1 = v({x: 9, y: 8, z: 7});
assert.strictEqual(v1.x, 9);
assert.strictEqual(v1.y, 8);
assert.strictEqual(v1.z, 7);
});
it("string coords", function() {
var v1 = v("1", "1.5", "-30.2");
assert.strictEqual(v1.x, 1);
assert.strictEqual(v1.y, 1.5);
assert.strictEqual(v1.z, -30.2);
});
it("deserialize", function() {
var v1 = v(v(1, -3.5, 0).toString());
assert.strictEqual(v1.x, 1);
assert.strictEqual(v1.y, -3.5);
assert.strictEqual(v1.z, 0);
var v2 = v(v(-111, 222, 9876543210.123456789).toString());
assert.strictEqual(v2.x, -111);
assert.strictEqual(v2.y, 222);
assert.strictEqual(v2.z, 9876543210.123456789);
});
it("invalid deserialize", function() {
assert.throws(function() {
return v("lol hax");
}, /cannot parse/);
});
});
describe("vec3", function() {
it("floored", function() {
var v1 = new Vec3(1.1, -1.5, 1.9);
var v2 = v1.floored();
v1.x = 10;
assert.strictEqual(v2.x, 1);
assert.strictEqual(v2.y, -2);
assert.strictEqual(v2.z, 1);
});
it("floor", function() {
var v1 = new Vec3(1.1, -1.5, 1.9);
var v2 = v1.floor();
assert.strictEqual(v2, v1);
assert.strictEqual(v1.x, 1);
assert.strictEqual(v1.y, -2);
assert.strictEqual(v1.z, 1);
});
it("offset", function() {
var v1 = new Vec3(1, 2, 3);
var v2 = v1.offset(10, -10, 20);
v1.x = -100;
assert.strictEqual(v2.x, 11);
assert.strictEqual(v2.y, -8);
assert.strictEqual(v2.z, 23);
});
it("translate", function() {
var v1 = new Vec3(1, 2, 3);
v1.translate(10, -10, 20);
assert.strictEqual(v1.x, 11);
assert.strictEqual(v1.y, -8);
assert.strictEqual(v1.z, 23);
});
it("plus", function() {
var v1 = new Vec3(1, 2, 3);
var v2 = new Vec3(-1, 0, 1);
var v3 = v1.plus(v2);
assert.strictEqual(v1.x, 1);
assert.strictEqual(v1.y, 2);
assert.strictEqual(v1.z, 3);
assert.strictEqual(v2.x, -1);
assert.strictEqual(v2.y, 0);
assert.strictEqual(v2.z, 1);
assert.strictEqual(v3.x, 0);
assert.strictEqual(v3.y, 2);
assert.strictEqual(v3.z, 4);
});
it("minus", function() {
var v1 = new Vec3(1, 2, 3);
var v2 = new Vec3(-1, 0, 1);
var v3 = v1.minus(v2);
assert.strictEqual(v1.x, 1);
assert.strictEqual(v1.y, 2);
assert.strictEqual(v1.z, 3);
assert.strictEqual(v2.x, -1);
assert.strictEqual(v2.y, 0);
assert.strictEqual(v2.z, 1);
assert.strictEqual(v3.x, 2);
assert.strictEqual(v3.y, 2);
assert.strictEqual(v3.z, 2);
});
it("scaled", function() {
var v1 = new Vec3(1, 2, 3);
var v2 = v1.scaled(2);
assert.strictEqual(v1.x, 1);
assert.strictEqual(v1.y, 2);
assert.strictEqual(v1.z, 3);
assert.strictEqual(v2.x, 2);
assert.strictEqual(v2.y, 4);
assert.strictEqual(v2.z, 6);
});
it("abs", function() {
var v1 = new Vec3(1.1, -1.5, 1.9);
var v2 = v1.abs();
v1.x = 10;
assert.strictEqual(v2.x, 1.1);
assert.strictEqual(v2.y, 1.5);
assert.strictEqual(v2.z, 1.9);
});
it("distanceTo", function() {
var v1 = new Vec3(1, 1, 1);
var v2 = new Vec3(2, 2, 2);
var dist1 = v1.distanceTo(v2);
var dist2 = v2.distanceTo(v1);
var expected = 1.7320508075688772;
assert.strictEqual(dist1, dist2);
assert.strictEqual(Math.round(dist1 * 100000), Math.round(expected * 100000));
});
it("equals", function() {
var v1 = new Vec3(1, 2, 3);
var v2 = v1.scaled(0.23424);
var v3 = v1.scaled(0.23424);
assert.ok(v2.equals(v3));
});
it("toString", function() {
var v1 = new Vec3(1, -1, 3.14);
assert.strictEqual(v1.toString(), "(1, -1, 3.14)");
});
it("clone", function() {
var v1 = new Vec3(1, 2, 3);
var v2 = v1.clone();
v2.x = 10;
assert.strictEqual(v1.x, 1);
assert.strictEqual(v1.y, 2);
assert.strictEqual(v1.z, 3);
assert.strictEqual(v2.x, 10);
assert.strictEqual(v2.y, 2);
assert.strictEqual(v2.z, 3);
});
it("add", function() {
var v1 = new Vec3(1, 2, 3);
var v2 = new Vec3(-1, -2, -3);
var v3 = v1.add(v2);
assert.strictEqual(v3, v1);
assert.strictEqual(v1.x, 0);
assert.strictEqual(v1.y, 0);
assert.strictEqual(v1.z, 0);
});
it("subtract", function() {
var v1 = new Vec3(1, 2, 3);
var v2 = new Vec3(-1, -2, -3);
var v3 = v1.subtract(v2);
assert.strictEqual(v3, v1);
assert.strictEqual(v1.x, 2);
assert.strictEqual(v1.y, 4);
assert.strictEqual(v1.z, 6);
});
it("set", function() {
var v1 = new Vec3(12, 32, 46);
var v2 = v1.set(0, 10, 100);
assert.strictEqual(v1, v2);
assert.strictEqual(v1.x, 0);
assert.strictEqual(v1.y, 10);
assert.strictEqual(v1.z, 100);
});
it("modulus", function() {
var v1 = new Vec3(12, 32, -1);
var v2 = new Vec3(14, 32, 16);
var v3 = v1.modulus(v2);
assert.strictEqual(v1.x, 12);
assert.strictEqual(v1.y, 32);
assert.strictEqual(v1.z, -1);
assert.strictEqual(v2.x, 14);
assert.strictEqual(v2.y, 32);
assert.strictEqual(v2.z, 16);
assert.strictEqual(v3.x, 12);
assert.strictEqual(v3.y, 0);
assert.strictEqual(v3.z, 15);
});
it("volume", function() {
var v1 = new Vec3(3, 4, 5);
assert.strictEqual(v1.volume(), 60);
});
it("min", function() {
var v1 = new Vec3(-1, 0, 1);
var v2 = new Vec3(10, -10, 1.1);
var v3 = v1.min(v2);
assert.strictEqual(v3.x, -1);
assert.strictEqual(v3.y, -10);
assert.strictEqual(v3.z, 1);
});
it("max", function() {
var v1 = new Vec3(-1, 0, 1);
var v2 = new Vec3(10, -10, 1.1);
var v3 = v1.max(v2);
assert.strictEqual(v3.x, 10);
assert.strictEqual(v3.y, 0);
assert.strictEqual(v3.z, 1.1);
});
it("update", function() {
var v1 = new Vec3(-1, 0, 1);
var v2 = new Vec3(10, -10, 1.1);
var v3 = v1.update(v2);
assert.strictEqual(v3, v1);
assert.strictEqual(v1.x, 10);
assert.strictEqual(v1.y, -10);
assert.strictEqual(v1.z, 1.1);
assert.strictEqual(v2.x, 10);
assert.strictEqual(v2.y, -10);
assert.strictEqual(v2.z, 1.1);
});
it("norm", function() {
var v1 = new Vec3(-10, 0, 10);
assert.strictEqual(Math.round(v1.norm() * 100000), Math.round(14.142135623730950 * 100000));
});
it("unit", function() {
var v1 = new Vec3(10, -10, 1.1);
describe('v()', function () {
it('no args', function () {
var v1 = v()
assert.strictEqual(v1.x, 0)
assert.strictEqual(v1.y, 0)
assert.strictEqual(v1.z, 0)
})
it('x, y, z', function () {
var v1 = v(-1, 5, 10.10)
assert.strictEqual(v1.x, -1)
assert.strictEqual(v1.y, 5)
assert.strictEqual(v1.z, 10.10)
})
it('array', function () {
var v1 = v([4, 5, 6])
assert.strictEqual(v1.x, 4)
assert.strictEqual(v1.y, 5)
assert.strictEqual(v1.z, 6)
})
it('object', function () {
var v1 = v({ x: 9, y: 8, z: 7 })
assert.strictEqual(v1.x, 9)
assert.strictEqual(v1.y, 8)
assert.strictEqual(v1.z, 7)
})
it('string coords', function () {
var v1 = v('1', '1.5', '-30.2')
assert.strictEqual(v1.x, 1)
assert.strictEqual(v1.y, 1.5)
assert.strictEqual(v1.z, -30.2)
})
it('deserialize', function () {
var v1 = v(v(1, -3.5, 0).toString())
assert.strictEqual(v1.x, 1)
assert.strictEqual(v1.y, -3.5)
assert.strictEqual(v1.z, 0)
var v2 = v(v(-111, 222, 9876543210.123456789).toString())
assert.strictEqual(v2.x, -111)
assert.strictEqual(v2.y, 222)
assert.strictEqual(v2.z, 9876543210.123456789)
})
it('invalid deserialize', function () {
assert.throws(function () {
return v('lol hax')
}, /cannot parse/)
})
})
describe('vec3', function () {
it('floored', function () {
var v1 = new Vec3(1.1, -1.5, 1.9)
var v2 = v1.floored()
v1.x = 10
assert.strictEqual(v2.x, 1)
assert.strictEqual(v2.y, -2)
assert.strictEqual(v2.z, 1)
})
it('floor', function () {
var v1 = new Vec3(1.1, -1.5, 1.9)
var v2 = v1.floor()
assert.strictEqual(v2, v1)
assert.strictEqual(v1.x, 1)
assert.strictEqual(v1.y, -2)
assert.strictEqual(v1.z, 1)
})
it('offset', function () {
var v1 = new Vec3(1, 2, 3)
var v2 = v1.offset(10, -10, 20)
v1.x = -100
assert.strictEqual(v2.x, 11)
assert.strictEqual(v2.y, -8)
assert.strictEqual(v2.z, 23)
})
it('translate', function () {
var v1 = new Vec3(1, 2, 3)
v1.translate(10, -10, 20)
assert.strictEqual(v1.x, 11)
assert.strictEqual(v1.y, -8)
assert.strictEqual(v1.z, 23)
})
it('plus', function () {
var v1 = new Vec3(1, 2, 3)
var v2 = new Vec3(-1, 0, 1)
var v3 = v1.plus(v2)
assert.strictEqual(v1.x, 1)
assert.strictEqual(v1.y, 2)
assert.strictEqual(v1.z, 3)
assert.strictEqual(v2.x, -1)
assert.strictEqual(v2.y, 0)
assert.strictEqual(v2.z, 1)
assert.strictEqual(v3.x, 0)
assert.strictEqual(v3.y, 2)
assert.strictEqual(v3.z, 4)
})
it('minus', function () {
var v1 = new Vec3(1, 2, 3)
var v2 = new Vec3(-1, 0, 1)
var v3 = v1.minus(v2)
assert.strictEqual(v1.x, 1)
assert.strictEqual(v1.y, 2)
assert.strictEqual(v1.z, 3)
assert.strictEqual(v2.x, -1)
assert.strictEqual(v2.y, 0)
assert.strictEqual(v2.z, 1)
assert.strictEqual(v3.x, 2)
assert.strictEqual(v3.y, 2)
assert.strictEqual(v3.z, 2)
})
it('scaled', function () {
var v1 = new Vec3(1, 2, 3)
var v2 = v1.scaled(2)
assert.strictEqual(v1.x, 1)
assert.strictEqual(v1.y, 2)
assert.strictEqual(v1.z, 3)
assert.strictEqual(v2.x, 2)
assert.strictEqual(v2.y, 4)
assert.strictEqual(v2.z, 6)
})
it('abs', function () {
var v1 = new Vec3(1.1, -1.5, 1.9)
var v2 = v1.abs()
v1.x = 10
assert.strictEqual(v2.x, 1.1)
assert.strictEqual(v2.y, 1.5)
assert.strictEqual(v2.z, 1.9)
})
it('distanceTo', function () {
var v1 = new Vec3(1, 1, 1)
var v2 = new Vec3(2, 2, 2)
var dist1 = v1.distanceTo(v2)
var dist2 = v2.distanceTo(v1)
var expected = 1.7320508075688772
assert.strictEqual(dist1, dist2)
assert.strictEqual(Math.round(dist1 * 100000), Math.round(expected * 100000))
})
it('equals', function () {
var v1 = new Vec3(1, 2, 3)
var v2 = v1.scaled(0.23424)
var v3 = v1.scaled(0.23424)
assert.ok(v2.equals(v3))
})
it('toString', function () {
var v1 = new Vec3(1, -1, 3.14)
assert.strictEqual(v1.toString(), '(1, -1, 3.14)')
})
it('clone', function () {
var v1 = new Vec3(1, 2, 3)
var v2 = v1.clone()
v2.x = 10
assert.strictEqual(v1.x, 1)
assert.strictEqual(v1.y, 2)
assert.strictEqual(v1.z, 3)
assert.strictEqual(v2.x, 10)
assert.strictEqual(v2.y, 2)
assert.strictEqual(v2.z, 3)
})
it('add', function () {
var v1 = new Vec3(1, 2, 3)
var v2 = new Vec3(-1, -2, -3)
var v3 = v1.add(v2)
assert.strictEqual(v3, v1)
assert.strictEqual(v1.x, 0)
assert.strictEqual(v1.y, 0)
assert.strictEqual(v1.z, 0)
})
it('subtract', function () {
var v1 = new Vec3(1, 2, 3)
var v2 = new Vec3(-1, -2, -3)
var v3 = v1.subtract(v2)
assert.strictEqual(v3, v1)
assert.strictEqual(v1.x, 2)
assert.strictEqual(v1.y, 4)
assert.strictEqual(v1.z, 6)
})
it('set', function () {
var v1 = new Vec3(12, 32, 46)
var v2 = v1.set(0, 10, 100)
assert.strictEqual(v1, v2)
assert.strictEqual(v1.x, 0)
assert.strictEqual(v1.y, 10)
assert.strictEqual(v1.z, 100)
})
it('modulus', function () {
var v1 = new Vec3(12, 32, -1)
var v2 = new Vec3(14, 32, 16)
var v3 = v1.modulus(v2)
assert.strictEqual(v1.x, 12)
assert.strictEqual(v1.y, 32)
assert.strictEqual(v1.z, -1)
assert.strictEqual(v2.x, 14)
assert.strictEqual(v2.y, 32)
assert.strictEqual(v2.z, 16)
assert.strictEqual(v3.x, 12)
assert.strictEqual(v3.y, 0)
assert.strictEqual(v3.z, 15)
})
it('volume', function () {
var v1 = new Vec3(3, 4, 5)
assert.strictEqual(v1.volume(), 60)
})
it('min', function () {
var v1 = new Vec3(-1, 0, 1)
var v2 = new Vec3(10, -10, 1.1)
var v3 = v1.min(v2)
assert.strictEqual(v3.x, -1)
assert.strictEqual(v3.y, -10)
assert.strictEqual(v3.z, 1)
})
it('max', function () {
var v1 = new Vec3(-1, 0, 1)
var v2 = new Vec3(10, -10, 1.1)
var v3 = v1.max(v2)
assert.strictEqual(v3.x, 10)
assert.strictEqual(v3.y, 0)
assert.strictEqual(v3.z, 1.1)
})
it('update', function () {
var v1 = new Vec3(-1, 0, 1)
var v2 = new Vec3(10, -10, 1.1)
var v3 = v1.update(v2)
assert.strictEqual(v3, v1)
assert.strictEqual(v1.x, 10)
assert.strictEqual(v1.y, -10)
assert.strictEqual(v1.z, 1.1)
assert.strictEqual(v2.x, 10)
assert.strictEqual(v2.y, -10)
assert.strictEqual(v2.z, 1.1)
})
it('norm', function () {
var v1 = new Vec3(-10, 0, 10)
assert.strictEqual(Math.round(v1.norm() * 100000), Math.round(14.142135623730950 * 100000))
})
it('dot', function () {
var v1 = new Vec3(-1, -1, -1)
var v2 = new Vec3(1, 1, 1)
assert.strictEqual(v1.dot(v2), -3)
})
it('cross', function () {
var v1 = new Vec3(1, 0, 0)
var v2 = new Vec3(0, 1, 0)
var v3 = new Vec3(0, 0, 1)
assert.ok(v1.cross(v2).equals(v3))
})
it('unit', function () {
var v1 = new Vec3(10, -10, 1.1)
var v2 = v1.unit()
assert.strictEqual(Math.round(v2.x * 100000), Math.round(0.7049774402016568 * 100000));
assert.strictEqual(Math.round(v2.y * 100000), Math.round(-0.704977440201656 * 100000));
assert.strictEqual(Math.round(v2.z * 100000), Math.round(0.07754751842218225 * 100000));
var v3 = new Vec3(0, 0, 0);
assert.strictEqual(Math.round(v2.x * 100000), Math.round(0.7049774402016568 * 100000))
assert.strictEqual(Math.round(v2.y * 100000), Math.round(-0.704977440201656 * 100000))
assert.strictEqual(Math.round(v2.z * 100000), Math.round(0.07754751842218225 * 100000))
var v3 = new Vec3(0, 0, 0)
var v4 = v3.unit()
assert.strictEqual(v4.x, 0);
assert.strictEqual(v4.y, 0);
assert.strictEqual(v4.z, 0);
});
it("scale", function() {
var v1 = new Vec3(10, -10, 1.1);
assert.strictEqual(v4.x, 0)
assert.strictEqual(v4.y, 0)
assert.strictEqual(v4.z, 0)
})
it('normalize', function () {
var v1 = new Vec3(10, -10, 1.1)
var v2 = v1.normalize()
assert.strictEqual(Math.round(v2.x * 100000), Math.round(0.7049774402016568 * 100000))
assert.strictEqual(Math.round(v2.y * 100000), Math.round(-0.704977440201656 * 100000))
assert.strictEqual(Math.round(v2.z * 100000), Math.round(0.07754751842218225 * 100000))
var v3 = new Vec3(0, 0, 0)
var v4 = v3.normalize()
assert.strictEqual(v4.x, 0)
assert.strictEqual(v4.y, 0)
assert.strictEqual(v4.z, 0)
})
it('scale', function () {
var v1 = new Vec3(10, -10, 1.1)
var v2 = v1.scale(1.5)
assert.strictEqual(v2.x, 15);
assert.strictEqual(v2.y, -15);
assert.strictEqual(Math.round(v2.z * 100000), Math.round(1.65 * 100000));
});
it("xyDistanceTo", function() {
var v1 = new Vec3(1, 1, 1);
var v2 = new Vec3(2, 2, 2);
var dist1 = v1.xyDistanceTo(v2);
var dist2 = v2.xyDistanceTo(v1);
var expected = 1.4142135623730950;
assert.strictEqual(dist1, dist2);
assert.strictEqual(Math.round(dist1 * 100000), Math.round(expected * 100000));
});
it("xzDistanceTo", function() {
var v1 = new Vec3(1, 1, 1);
var v2 = new Vec3(2, 2, 2);
var dist1 = v1.xzDistanceTo(v2);
var dist2 = v2.xzDistanceTo(v1);
var expected = 1.4142135623730950;
assert.strictEqual(dist1, dist2);
assert.strictEqual(Math.round(dist1 * 100000), Math.round(expected * 100000));
});
it("yzDistanceTo", function() {
var v1 = new Vec3(1, 1, 1);
var v2 = new Vec3(2, 2, 2);
var dist1 = v1.yzDistanceTo(v2);
var dist2 = v2.yzDistanceTo(v1);
var expected = 1.4142135623730950;
assert.strictEqual(dist1, dist2);
assert.strictEqual(Math.round(dist1 * 100000), Math.round(expected * 100000));
});
it("innerProduct", function() {
var v1 = new Vec3(-1, 0, 1);
var v2 = new Vec3(0, 1, 0);
assert.strictEqual(v2.x, 15)
assert.strictEqual(v2.y, -15)
assert.strictEqual(Math.round(v2.z * 100000), Math.round(1.65 * 100000))
})
it('xyDistanceTo', function () {
var v1 = new Vec3(1, 1, 1)
var v2 = new Vec3(2, 2, 2)
var dist1 = v1.xyDistanceTo(v2)
var dist2 = v2.xyDistanceTo(v1)
var expected = 1.4142135623730950
assert.strictEqual(dist1, dist2)
assert.strictEqual(Math.round(dist1 * 100000), Math.round(expected * 100000))
})
it('xzDistanceTo', function () {
var v1 = new Vec3(1, 1, 1)
var v2 = new Vec3(2, 2, 2)
var dist1 = v1.xzDistanceTo(v2)
var dist2 = v2.xzDistanceTo(v1)
var expected = 1.4142135623730950
assert.strictEqual(dist1, dist2)
assert.strictEqual(Math.round(dist1 * 100000), Math.round(expected * 100000))
})
it('yzDistanceTo', function () {
var v1 = new Vec3(1, 1, 1)
var v2 = new Vec3(2, 2, 2)
var dist1 = v1.yzDistanceTo(v2)
var dist2 = v2.yzDistanceTo(v1)
var expected = 1.4142135623730950
assert.strictEqual(dist1, dist2)
assert.strictEqual(Math.round(dist1 * 100000), Math.round(expected * 100000))
})
it('innerProduct', function () {
var v1 = new Vec3(-1, 0, 1)
var v2 = new Vec3(0, 1, 0)
var ip1 = v1.innerProduct(v2)
var ip2 = v2.innerProduct(v1)
assert.strictEqual(ip1, ip2);
assert.strictEqual(ip1, 0);
});
it("manhattanDistanceTo", function() {
var v1 = new Vec3(-1, 0, 1);
var v2 = new Vec3(10, -10, 1.1);
var dist1 = v1.manhattanDistanceTo(v2);
var dist2 = v2.manhattanDistanceTo(v1);
assert.strictEqual(dist1, dist2);
assert.strictEqual(dist1, 21.1);
});
it("toArray", function() {
var v1 = new Vec3(1, -1, 3.14);
assert.strictEqual(ip1, ip2)
assert.strictEqual(ip1, 0)
})
it('manhattanDistanceTo', function () {
var v1 = new Vec3(-1, 0, 1)
var v2 = new Vec3(10, -10, 1.1)
var dist1 = v1.manhattanDistanceTo(v2)
var dist2 = v2.manhattanDistanceTo(v1)
assert.strictEqual(dist1, dist2)
assert.strictEqual(dist1, 21.1)
})
it('toArray', function () {
var v1 = new Vec3(1, -1, 3.14)
var array = v1.toArray()
assert.strictEqual(v1.x, array[0]);
assert.strictEqual(v1.y, array[1]);
assert.strictEqual(v1.z, array[2]);
});
});
assert.strictEqual(v1.x, array[0])
assert.strictEqual(v1.y, array[1])
assert.strictEqual(v1.z, array[2])
})
})