Skip to content

Commit

Permalink
Add methods, fix .equals (#46)
Browse files Browse the repository at this point in the history
* Add isZero, xy, xzy, fix equals, improve set

Signed-off-by: szdytom <szdytom@qq.com>

* Remove nullable .set

Signed-off-by: szdytom <szdytom@qq.com>

* make .equals error default to 0

Signed-off-by: szdytom <szdytom@qq.com>

---------

Signed-off-by: szdytom <szdytom@qq.com>
  • Loading branch information
szdytom committed Dec 31, 2023
1 parent 8675f8e commit c6b94c4
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 51 deletions.
100 changes: 53 additions & 47 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,55 +29,61 @@ More available functions are listed below in Test Coverage.
## Test Coverage

```
v()
no args
x, y, z
array
object
string coords
deserialize
invalid deserialize
v()
no args
x, y, z
array
object
string coords
deserialize
invalid deserialize
vec3
✓ rounded
✓ round
✓ floored
✓ floor
✓ offset
✓ translate
✓ plus
✓ minus
✓ scaled
✓ abs
✓ distanceTo
✓ equals
✓ toString
✓ clone
✓ add
✓ subtract
✓ multiply
✓ divide
✓ set
✓ modulus
✓ volume
✓ min
✓ max
✓ update
✓ norm
✓ dot
✓ cross
✓ unit
✓ normalize
✓ scale
✓ xyDistanceTo
✓ xzDistanceTo
✓ yzDistanceTo
✓ innerProduct
✓ manhattanDistanceTo
✓ toArray
39 passing
✔ isZero
✔ at
✔ xz
✔ xy
✔ yz
✔ xzy
✔ rounded
✔ round
✔ floored
✔ floor
✔ offset
✔ translate
✔ plus
✔ minus
✔ scaled
✔ abs
✔ distanceTo
✔ distanceSquared
✔ equals
✔ toString
✔ clone
✔ add
✔ subtract
✔ multiply
✔ divide
✔ set
✔ modulus
✔ volume
✔ min
✔ max
✔ update
✔ norm
✔ dot
✔ cross
✔ unit
✔ normalize
✔ scale
✔ xyDistanceTo
✔ xzDistanceTo
✔ yzDistanceTo
✔ innerProduct
✔ manhattanDistanceTo
✔ toArray
50 passing (14ms)
```

More functions welcome in the form of pull requests.
Expand Down
36 changes: 34 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,39 @@ export class Vec3 {
y: number;
z: number;

/**
* Returns true when it is a zero vector.
*/
isZero(): boolean;

/**
* Access component by index
*/
at(id: number): number;

/**
* Returns an array component x, z
*/
xz(): [number, number];

/**
* Returns an array component x, y
*/
xy(): [number, number];

/**
* Returns an array component y, z
*/
yz(): [number, number];

/**
* Returns a vector with swapped y and z
*/
xzy(): Vec3;

/**
* Set own values to given x y z
* If some components is given null, then those components won't change
*/
set(x: number, y: number, z: number): this;

Expand Down Expand Up @@ -106,9 +137,10 @@ export class Vec3 {
distanceSquared(other: Vec3): number;

/**
* Returns true when all values match with the values off the other vector
* Check whether two vectors are equal
* Returns true if each components have at most `error` difference
*/
equals(other: Vec3): boolean;
equals(other: Vec3, error?: number): boolean;

/**
* Converts own values to a string representation in the format `(x, y, z)`
Expand Down
30 changes: 28 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,30 @@ class Vec3 {
this.z = z
}

isZero () {
return this.x === 0 && this.y === 0 && this.z === 0
}

at (id) {
return this.toArray()[id]
}

xz () {
return [this.x, this.z]
}

xy () {
return [this.x, this.y]
}

yz () {
return [this.y, this.z]
}

xzy () {
return new Vec3(this.x, this.z, this.y)
}

set (x, y, z) {
this.x = x
this.y = y
Expand Down Expand Up @@ -123,8 +147,10 @@ class Vec3 {
return dx * dx + dy * dy + dz * dz
}

equals (other) {
return this.x === other.x && this.y === other.y && this.z === other.z
equals (other, error = 0) {
return Math.abs(this.x - other.x) <= error &&
Math.abs(this.y - other.y) <= error &&
Math.abs(this.z - other.z) <= error
}

toString () {
Expand Down
6 changes: 6 additions & 0 deletions test/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ expectType<number>(vec.x);
expectType<number>(vec.y);
expectType<number>(vec.z);

expectType<boolean>(vec.isZero());
expectType<number>(vec.at(1));
expectType<[number, number]>(vec.xz());
expectType<[number, number]>(vec.xy());
expectType<[number, number]>(vec.yz());
expectType<Vec3>(vec.xzy());
expectType<Vec3>(vec.set(4, 5, 6));
expectType<Vec3>(vec.update(vec));
expectType<Vec3>(vec.floored());
Expand Down
41 changes: 41 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,43 @@ describe('v()', function () {
})
})
describe('vec3', function () {
it('isZero', function () {
const v1 = new Vec3(0, 1, 2)
const v2 = new Vec3(0, 0, 0)
assert.ok(!v1.isZero())
assert.ok(v2.isZero())
})
it('at', function () {
const v1 = new Vec3(0, 1, 2)
assert.strictEqual(v1.at(0), 0)
assert.strictEqual(v1.at(1), 1)
assert.strictEqual(v1.at(2), 2)
})
it('xz', function () {
const v1 = new Vec3(0, 1, 2)
const a = v1.xz()
assert.strictEqual(a[0], 0)
assert.strictEqual(a[1], 2)
})
it('xy', function () {
const v1 = new Vec3(0, 1, 2)
const a = v1.xy()
assert.strictEqual(a[0], 0)
assert.strictEqual(a[1], 1)
})
it('yz', function () {
const v1 = new Vec3(0, 1, 2)
const a = v1.yz()
assert.strictEqual(a[0], 1)
assert.strictEqual(a[1], 2)
})
it('xzy', function () {
const v1 = new Vec3(0, 1, 2)
const v2 = v1.xzy()
assert.strictEqual(v2.x, 0)
assert.strictEqual(v2.y, 2)
assert.strictEqual(v2.z, 1)
})
it('rounded', function () {
const v1 = new Vec3(1.1, -1.5, 1.9)
const v2 = v1.rounded()
Expand Down Expand Up @@ -168,6 +205,10 @@ describe('vec3', function () {
const v2 = v1.scaled(0.23424)
const v3 = v1.scaled(0.23424)
assert.ok(v2.equals(v3))
const v4 = new Vec3(0.1, 0, 0)
const v5 = new Vec3(0.2, 0, 0)
const v6 = new Vec3(0.3, 0, 0)
assert.ok(v4.plus(v5).equals(v6, Number.EPSILON))
})
it('toString', function () {
const v1 = new Vec3(1, -1, 3.14)
Expand Down

0 comments on commit c6b94c4

Please sign in to comment.