Skip to content

Commit

Permalink
feat(vector): xy xz xz getter for 3d to 2d conv
Browse files Browse the repository at this point in the history
  • Loading branch information
MrTelanie committed May 19, 2019
1 parent 3354467 commit c596d18
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 20 deletions.
18 changes: 14 additions & 4 deletions src/point.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import formatNumber from './formatter';

/* eslint class-methods-use-this: 0 */
/* eslint-disable no-unused-vars */

const X = 0;
const Y = 1;
Expand Down Expand Up @@ -142,9 +143,18 @@ class APoint {
}

/**
*
* @param {(point: APointType) => number} alg
* @returns {this}
* @throws NotImplementedError
*/
calc(alg) {
throw new Error('calc() not implemented');
}

/**
* @throws NotImplementedError
* @returns {APoint}
*/
clone() {
throw new Error('clone() not implemented');
}
Expand Down Expand Up @@ -232,7 +242,7 @@ class APoint {

/**
*
* @throws GetNotImplementedError
* @returns {number}
*/
get z() {
throw new Error('get z() not implemented');
Expand Down Expand Up @@ -294,15 +304,15 @@ export class Point extends APoint {
}

/**
* @param {(point: PointType) => number} alg
* @param {(point: APointType) => number} alg
* @returns {this}
*/
calc(alg) {
return operatorCalc(alg, this);
}

/**
* @returns {Point}
* @returns {APoint}
*/
clone() {
return new Point(this.x, this.y);
Expand Down
83 changes: 67 additions & 16 deletions src/vector.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import {
cachedFactory
} from './operator';
import formatNumber from './formatter';
import { ipoint } from './point';

/* eslint class-methods-use-this: 0 */
/* eslint-disable no-unused-vars */

const X = 0;
const Y = 1;
Expand All @@ -30,7 +32,6 @@ class AVector {
* @param {number | (Alg)} x
* @param {number} [y]
* @param {number} [z]
* @returns {AVectorType}
*/
constructor(x, y, z) {
if (typeof x === 'function') {
Expand Down Expand Up @@ -69,16 +70,15 @@ class AVector {

/**
*
* @param {AVector} v
* @param {AVectorType} v
* @returns {number}
*/
dot(v) {
return this.x * v.x + this.y * v.y + this.z * v.z;
}

/**
*
* @param {AVector} v
* @param {AVectorType} v
* @returns {AVectorType}
*/
cross(v) {
Expand All @@ -90,8 +90,7 @@ class AVector {
}

/**
*
* @param {AVector} v
* @param {AVectorType} v
* @returns {AVectorType}
*/
crossNormalize(v) {
Expand All @@ -104,8 +103,7 @@ class AVector {
}

/**
*
* @param {AVector} v
* @param {AVectorType} v
* @returns {AVectorType}
*/
cn(v) {
Expand All @@ -125,7 +123,7 @@ class AVector {

/**
*
* @param {AVector} v
* @param {AVectorType} v
* @returns {number}
*/
angleTo(v) {
Expand All @@ -136,7 +134,7 @@ class AVector {
/**
*
* @param {{ x: number, y: number, z: number, w: number }} quat
* @returns {AVector}
* @returns {AVectorType}
*/
rotate(quat) {
const { x, y, z } = this;
Expand All @@ -159,7 +157,7 @@ class AVector {

/**
*
* @param {AVector} v
* @param {AVectorType} v
* @returns {number}
*/
distance(v) {
Expand All @@ -168,7 +166,7 @@ class AVector {

/**
*
* @param {AVector} v
* @param {AVectorType} v
* @returns {number}
*/
dist(v) {
Expand All @@ -188,17 +186,27 @@ class AVector {
return new this.constructor(data[0], data[1], data[2]);
}

/**
* @param {(vector: AVectorType) => number} arg
* @returns {this}
* @throws NotImplementedError
*/
calc(arg) {
throw new Error('calc() not implemented');
}

/**
*
* @throws NotImplementedError
* @return {AVectorType}
*/
clone() {
throw new Error('clone() not implemented');
}

/**
*
* @param {AVector} v
* @param {AVectorType} v
* @returns {boolean}
*/
equals(v) {
Expand Down Expand Up @@ -292,6 +300,49 @@ class AVector {
set z(_) {
throw new Error('set z() not implemented');
}

/**
* @typedef {((import("./point").IPoint) & number)} IPointType
* @returns {IPointType}
*/
get xy() {
return ipoint(this[AXES][X], this[AXES][Y]);
}

/**
* @throws SetNotImplementedError
*/
set xy(_) {
throw new Error('set xz() not implemented');
}

/**
* @returns {IPointType}
*/
get xz() {
return ipoint(this[AXES][X], this[AXES][Z]);
}

/**
* @throws SetNotImplementedError
*/
set xz(_) {
throw new Error('set xz() not implemented');
}

/**
* @returns {IPointType}
*/
get yz() {
return ipoint(this[AXES][Y], this[AXES][Z]);
}

/**
* @throws SetNotImplementedError
*/
set yz(_) {
throw new Error('set yz() not implemented');
}
}

cachedValueOf(AVector);
Expand Down Expand Up @@ -360,15 +411,15 @@ export class Vector extends AVector {
}

/**
* @param {(vector: VectorType) => number} alg
* @param {(vector: AVectorType) => number} alg
* @returns {this}
*/
calc(alg) {
return operatorCalc(alg, this);
}

/**
* @returns {VectorType}
* @returns {AVectorType}
*/
clone() {
return new Vector(this.x, this.y, this.z);
Expand All @@ -387,7 +438,7 @@ export class Victor extends AVector {
}

/**
* @returns {this}
* @returns {AVectorType}
*/
clone() {
return this;
Expand Down

0 comments on commit c596d18

Please sign in to comment.