Huge News!Announcing our $40M Series B led by Abstract Ventures.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 0.1.3 to 0.1.4

HISTORY.md

41

index.js

@@ -122,2 +122,43 @@ module.exports = v;

};
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];
};

@@ -124,0 +165,0 @@ function euclideanMod(numerator, denominator) {

3

package.json
{
"name": "vec3",
"version": "0.1.3",
"version": "0.1.4",
"description": "3d vector math with good unit tests",
"main": "index.js",
"types": "index.d.ts",
"scripts": {

@@ -7,0 +8,0 @@ "test": "mocha --reporter spec"

@@ -30,33 +30,47 @@ # vec3

```
v()
✓ no args
✓ x, y, z
✓ array
✓ object
v()
√ no args
√ x, y, z
√ array
√ object
vec3
✓ floored
✓ floor
✓ offset
✓ translate
✓ plus
✓ minus
✓ scaled
✓ abs
✓ distanceTo
✓ equals
✓ toString
✓ clone
✓ add
✓ subtract
✓ set
✓ modulus
✓ volume
✓ min
✓ max
√ 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
21 tests complete
36 tests complete
```
More functions welcome in the form of pull requests.
## History
See [History](History.md)

@@ -232,2 +232,75 @@ var v = require('../')

});
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);
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);
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);
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);
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);
var array = v1.toArray()
assert.strictEqual(v1.x, array[0]);
assert.strictEqual(v1.y, array[1]);
assert.strictEqual(v1.z, array[2]);
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc