Skip to content

Commit c6b94c4

Browse files
authored
Add methods, fix .equals (#46)
* Add isZero, xy, xzy, fix equals, improve set Signed-off-by: szdytom <[email protected]> * Remove nullable .set Signed-off-by: szdytom <[email protected]> * make .equals error default to 0 Signed-off-by: szdytom <[email protected]> --------- Signed-off-by: szdytom <[email protected]>
1 parent 8675f8e commit c6b94c4

File tree

5 files changed

+162
-51
lines changed

5 files changed

+162
-51
lines changed

Diff for: README.md

+53-47
Original file line numberDiff line numberDiff line change
@@ -29,55 +29,61 @@ More available functions are listed below in Test Coverage.
2929
## Test Coverage
3030

3131
```
32-
v()
33-
no args
34-
x, y, z
35-
array
36-
object
37-
string coords
38-
deserialize
39-
invalid deserialize
32+
v()
33+
no args
34+
x, y, z
35+
array
36+
object
37+
string coords
38+
deserialize
39+
invalid deserialize
4040
4141
vec3
42-
✓ rounded
43-
✓ round
44-
✓ floored
45-
✓ floor
46-
✓ offset
47-
✓ translate
48-
✓ plus
49-
✓ minus
50-
✓ scaled
51-
✓ abs
52-
✓ distanceTo
53-
✓ equals
54-
✓ toString
55-
✓ clone
56-
✓ add
57-
✓ subtract
58-
✓ multiply
59-
✓ divide
60-
✓ set
61-
✓ modulus
62-
✓ volume
63-
✓ min
64-
✓ max
65-
✓ update
66-
✓ norm
67-
✓ dot
68-
✓ cross
69-
✓ unit
70-
✓ normalize
71-
✓ scale
72-
✓ xyDistanceTo
73-
✓ xzDistanceTo
74-
✓ yzDistanceTo
75-
✓ innerProduct
76-
✓ manhattanDistanceTo
77-
✓ toArray
78-
79-
80-
39 passing
42+
✔ isZero
43+
✔ at
44+
✔ xz
45+
✔ xy
46+
✔ yz
47+
✔ xzy
48+
✔ rounded
49+
✔ round
50+
✔ floored
51+
✔ floor
52+
✔ offset
53+
✔ translate
54+
✔ plus
55+
✔ minus
56+
✔ scaled
57+
✔ abs
58+
✔ distanceTo
59+
✔ distanceSquared
60+
✔ equals
61+
✔ toString
62+
✔ clone
63+
✔ add
64+
✔ subtract
65+
✔ multiply
66+
✔ divide
67+
✔ set
68+
✔ modulus
69+
✔ volume
70+
✔ min
71+
✔ max
72+
✔ update
73+
✔ norm
74+
✔ dot
75+
✔ cross
76+
✔ unit
77+
✔ normalize
78+
✔ scale
79+
✔ xyDistanceTo
80+
✔ xzDistanceTo
81+
✔ yzDistanceTo
82+
✔ innerProduct
83+
✔ manhattanDistanceTo
84+
✔ toArray
85+
86+
50 passing (14ms)
8187
```
8288

8389
More functions welcome in the form of pull requests.

Diff for: index.d.ts

+34-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,39 @@ export class Vec3 {
55
y: number;
66
z: number;
77

8+
/**
9+
* Returns true when it is a zero vector.
10+
*/
11+
isZero(): boolean;
12+
13+
/**
14+
* Access component by index
15+
*/
16+
at(id: number): number;
17+
18+
/**
19+
* Returns an array component x, z
20+
*/
21+
xz(): [number, number];
22+
23+
/**
24+
* Returns an array component x, y
25+
*/
26+
xy(): [number, number];
27+
28+
/**
29+
* Returns an array component y, z
30+
*/
31+
yz(): [number, number];
32+
33+
/**
34+
* Returns a vector with swapped y and z
35+
*/
36+
xzy(): Vec3;
37+
838
/**
939
* Set own values to given x y z
40+
* If some components is given null, then those components won't change
1041
*/
1142
set(x: number, y: number, z: number): this;
1243

@@ -106,9 +137,10 @@ export class Vec3 {
106137
distanceSquared(other: Vec3): number;
107138

108139
/**
109-
* Returns true when all values match with the values off the other vector
140+
* Check whether two vectors are equal
141+
* Returns true if each components have at most `error` difference
110142
*/
111-
equals(other: Vec3): boolean;
143+
equals(other: Vec3, error?: number): boolean;
112144

113145
/**
114146
* Converts own values to a string representation in the format `(x, y, z)`

Diff for: index.js

+28-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,30 @@ class Vec3 {
77
this.z = z
88
}
99

10+
isZero () {
11+
return this.x === 0 && this.y === 0 && this.z === 0
12+
}
13+
14+
at (id) {
15+
return this.toArray()[id]
16+
}
17+
18+
xz () {
19+
return [this.x, this.z]
20+
}
21+
22+
xy () {
23+
return [this.x, this.y]
24+
}
25+
26+
yz () {
27+
return [this.y, this.z]
28+
}
29+
30+
xzy () {
31+
return new Vec3(this.x, this.z, this.y)
32+
}
33+
1034
set (x, y, z) {
1135
this.x = x
1236
this.y = y
@@ -123,8 +147,10 @@ class Vec3 {
123147
return dx * dx + dy * dy + dz * dz
124148
}
125149

126-
equals (other) {
127-
return this.x === other.x && this.y === other.y && this.z === other.z
150+
equals (other, error = 0) {
151+
return Math.abs(this.x - other.x) <= error &&
152+
Math.abs(this.y - other.y) <= error &&
153+
Math.abs(this.z - other.z) <= error
128154
}
129155

130156
toString () {

Diff for: test/index.test-d.ts

+6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ expectType<number>(vec.x);
1717
expectType<number>(vec.y);
1818
expectType<number>(vec.z);
1919

20+
expectType<boolean>(vec.isZero());
21+
expectType<number>(vec.at(1));
22+
expectType<[number, number]>(vec.xz());
23+
expectType<[number, number]>(vec.xy());
24+
expectType<[number, number]>(vec.yz());
25+
expectType<Vec3>(vec.xzy());
2026
expectType<Vec3>(vec.set(4, 5, 6));
2127
expectType<Vec3>(vec.update(vec));
2228
expectType<Vec3>(vec.floored());

Diff for: test/test.js

+41
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,43 @@ describe('v()', function () {
5252
})
5353
})
5454
describe('vec3', function () {
55+
it('isZero', function () {
56+
const v1 = new Vec3(0, 1, 2)
57+
const v2 = new Vec3(0, 0, 0)
58+
assert.ok(!v1.isZero())
59+
assert.ok(v2.isZero())
60+
})
61+
it('at', function () {
62+
const v1 = new Vec3(0, 1, 2)
63+
assert.strictEqual(v1.at(0), 0)
64+
assert.strictEqual(v1.at(1), 1)
65+
assert.strictEqual(v1.at(2), 2)
66+
})
67+
it('xz', function () {
68+
const v1 = new Vec3(0, 1, 2)
69+
const a = v1.xz()
70+
assert.strictEqual(a[0], 0)
71+
assert.strictEqual(a[1], 2)
72+
})
73+
it('xy', function () {
74+
const v1 = new Vec3(0, 1, 2)
75+
const a = v1.xy()
76+
assert.strictEqual(a[0], 0)
77+
assert.strictEqual(a[1], 1)
78+
})
79+
it('yz', function () {
80+
const v1 = new Vec3(0, 1, 2)
81+
const a = v1.yz()
82+
assert.strictEqual(a[0], 1)
83+
assert.strictEqual(a[1], 2)
84+
})
85+
it('xzy', function () {
86+
const v1 = new Vec3(0, 1, 2)
87+
const v2 = v1.xzy()
88+
assert.strictEqual(v2.x, 0)
89+
assert.strictEqual(v2.y, 2)
90+
assert.strictEqual(v2.z, 1)
91+
})
5592
it('rounded', function () {
5693
const v1 = new Vec3(1.1, -1.5, 1.9)
5794
const v2 = v1.rounded()
@@ -168,6 +205,10 @@ describe('vec3', function () {
168205
const v2 = v1.scaled(0.23424)
169206
const v3 = v1.scaled(0.23424)
170207
assert.ok(v2.equals(v3))
208+
const v4 = new Vec3(0.1, 0, 0)
209+
const v5 = new Vec3(0.2, 0, 0)
210+
const v6 = new Vec3(0.3, 0, 0)
211+
assert.ok(v4.plus(v5).equals(v6, Number.EPSILON))
171212
})
172213
it('toString', function () {
173214
const v1 = new Vec3(1, -1, 3.14)

0 commit comments

Comments
 (0)