Skip to content

Commit

Permalink
feat(vector3): add accessors for x, y, z (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
fallenoak committed May 25, 2020
1 parent c2e029b commit fdd24be
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/lib/Vector3.mjs
Expand Up @@ -61,6 +61,39 @@ class Vector3 extends Float32Array {
}
}

/**
* @type {Number}
*/
get x() {
return this[0];
}

set x(x) {
this[0] = x;
}

/**
* @type {Number}
*/
get y() {
return this[1];
}

set y(y) {
this[1] = y;
}

/**
* @type {Number}
*/
get z() {
return this[2];
}

set z(z) {
this[2] = z;
}

/**
* Add given vector v to this vector.
*
Expand Down
48 changes: 48 additions & 0 deletions src/spec/Vector3.spec.js
Expand Up @@ -16,6 +16,54 @@ describe('Vector3', () => {
});
});

describe('prototype.x', () => {
test('gets x component', () => {
const vector = Vector3.of(1.0, 2.0, 3.0);

expect(vector.x).toEqual(1.0);
});

test('sets x component', () => {
const vector = Vector3.of(1.0, 2.0, 3.0);

vector.x = 4.0;

expect(vector.x).toEqual(4.0);
});
});

describe('prototype.y', () => {
test('gets y component', () => {
const vector = Vector3.of(1.0, 2.0, 3.0);

expect(vector.y).toEqual(2.0);
});

test('sets y component', () => {
const vector = Vector3.of(1.0, 2.0, 3.0);

vector.y = 4.0;

expect(vector.y).toEqual(4.0);
});
});

describe('prototype.z', () => {
test('gets z component', () => {
const vector = Vector3.of(1.0, 2.0, 3.0);

expect(vector.z).toEqual(3.0);
});

test('sets z component', () => {
const vector = Vector3.of(1.0, 2.0, 3.0);

vector.z = 4.0;

expect(vector.z).toEqual(4.0);
});
});

describe('prototype.add()', () => {
test('returns expected result when adding positive vector', () => {
const vector1 = Vector3.of(1.0, 2.0, 3.0);
Expand Down

0 comments on commit fdd24be

Please sign in to comment.