Skip to content

Commit

Permalink
feat(math): add multiplyVecMat4 function
Browse files Browse the repository at this point in the history
  • Loading branch information
MrTelanie committed Aug 26, 2020
1 parent 2ae0f66 commit a5430ba
Show file tree
Hide file tree
Showing 9 changed files with 13 additions and 793 deletions.
9 changes: 0 additions & 9 deletions src/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,6 @@ export class VectorArray extends Array {

cachedValueOf(VectorArray, src => src);

/**
* @typedef {Array<number> & number} ArrayType
* @typedef {(arr: Array<number>) => ArrayType} ArrArr
* @typedef {(...number) => ArrayType} NumberArr
* @typedef {ArrArr & NumberArr}
*
* @param {Array<number> | ...number} vals
* @returns {ArrayType}
*/
export function vectorArray(...vals) {
return new VectorArray(...vals);
}
155 changes: 4 additions & 151 deletions src/color.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,6 @@ const Z = 2;
const W = 3;
const AXES = Symbol('axes');

/**
* @typedef {IColor & number} IColorType
* @typedef {Color & number} ColorType
* @typedef {() => number} Alg
* @typedef {AColor & number} AColorType
*/
/**
* @abstract
*/
class AColor {
constructor(x, y, z, w) {
if (typeof x === 'function') {
Expand All @@ -35,133 +26,72 @@ class AColor {
}
}

/**
* @throws NotImplementedError
*/
dot(v) {
return this.x * v.x + this.y * v.y + this.z * v.z + this.w * v.w;
}

valueOf() {
throw new Error('valueOf() not implemented, looks like you try to calculate outside of calc');
}

/**
*
* @returns {[number, number]}
*/
toArray() {
return [this.x, this.y, this.z, this.w];
}

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

/**
* @throws NotImplementedError ⚠
* @returns {AColor}
*/
clone() {
throw new Error('clone() not implemented');
}

/**
*
* @param {AColor} v
* @returns {boolean}
*/
equals(v) {
return this.x === v.x && this.y === v.y && this.z === v.z && this.w === v.w;
}

/**
*
* @returns {object}
*/
toJSON() {
return {
x: this.x, y: this.y, z: this.z, w: this.w
};
}

/**
*
* @returns {string}
*/
toString() {
return JSON.stringify(this.toJSON());
}

/**
*
* @returns {object}
*/
toCSSVars(name, target) {
return convertToCSSVars(name, this.toJSON(), target);
}

/**
*
* @returns {number}
*/
get x() {
return this[AXES][X];
}

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

/**
*
* @returns {number}
*/
get y() {
return this[AXES][Y];
}

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

/**
*
* @returns {number}
*/
get z() {
return this[AXES][Z];
}

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

/**
*
* @returns {number}
*/
get w() {
return this[AXES][W];
}

/**
*
* @throws SetNotImplementedError
*/
set w(_) {
throw new Error('set w() not implemented');
}
Expand All @@ -176,138 +106,61 @@ defineVectorLength(AColor, 4);
cachedMethod(AColor, 'toArray');

export class Color extends AColor {
/**
*
* @param {number} x
*/
set x(x) {
this[AXES][X] = x;
}

/**
*
* @param {number} y
*/
set y(y) {
this[AXES][Y] = y;
}

/**
*
* @param {number} z
*/
set z(z) {
this[AXES][Z] = z;
}

/**
*
* @param {number} w
*/
set w(w) {
this[AXES][W] = w;
}

/**
*
* @returns {number}
*/
get x() {
return this[AXES][X];
}

/**
*
* @returns {number}
*/
get y() {
return this[AXES][Y];
}

/**
*
* @returns {number}
*/
get z() {
return this[AXES][Z];
}

/**
*
* @returns {number}
*/
get w() {
return this[AXES][W];
}

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

/**
* @returns {AColor}
*/
clone() {
return new Color(this.x, this.y);
}
}

export class IColor extends AColor {
/**
* @returns {ColorType}
*/
toColor() {
return new Color(this.x, this.y, this.z, this.w);
}
}

/**
* @param {Alg} alg
* @return {ColorType | IColorType}
*/
export function calc(alg) {
return operatorCalc(alg);
}

/**
* @template P
* @typedef {() => P} PZero
*/
/**
* @template P
* @typedef {(alg: Alg) => P} PAlg
*/
/**
* @template P
* @typedef {(x: number, y: number, z: number, w: number) => P} PCon
*/
/**
* @template P
* @typedef {(data: [number, number, number, number]) => P} PArr
*/
/**
* @template P
* @typedef {(vec: { x: number, y: number, z: number, w: number }) => P} PObj
*/
/**
* @template P
* @typedef {PZero<P> & PAlg<P> & PCon<P> & PArr<P> & PObj<P>} Po
*/

const colorFactory = cachedFunction((x, y, z, w) => new Color(x, y, z, w));

/**
* @type {Po<ColorType>}
*/
export const color = (x, y, z, w) => colorFactory(x, y, z, w);

const icolorFactory = cachedFunction((x, y, z, w) => new IColor(x, y, z, w));

/**
* @type {Po<IColorType>}
*/
export const icolor = (x, y, z, w) => icolorFactory(x, y, z, w);
36 changes: 0 additions & 36 deletions src/degree.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,7 @@ const ANGLE = Symbol('angle rad');
const DEG_TO_RAD = Math.PI / 180;
const RAD_TO_DEG = 180 / Math.PI;

/**
* @typedef {Degree & number} DegreeType
* @typedef {IDegree & number} IDegreeType
* @abstract
*/
class ADegree {
/**
*
* @param {number | ADegree} angle
*/
constructor(angle) {
if (angle instanceof ADegree) {
this[ANGLE] = angle[ANGLE];
Expand All @@ -24,40 +15,24 @@ class ADegree {
}
}

/**
* @returns {number}
*/
valueOf() {
return this[ANGLE];
}

/**
* @returns {object}
*/
toJSON() {
return { angle: this[ANGLE] };
}

/**
* @returns {string}
*/
toString() {
return JSON.stringify(this.toJSON());
}

/**
* @returns {object}
*/
toCSSVars(name, target) {
return convertToCSSVars(name, this.toJSON(), target);
}
}

export class Degree extends ADegree {
/**
*
* @param {number | Degree | IDegree} [angle]
*/
set(angle) {
if (angle instanceof ADegree) {
this[ANGLE] = angle[ANGLE];
Expand All @@ -68,28 +43,17 @@ export class Degree extends ADegree {
}

export class IDegree extends ADegree {
/**
* @returns {Degree}
*/
toDegree() {
return new Degree(this[ANGLE]);
}
}

const ZERO = new IDegree(0);

/**
* @param {number | Degree | IDegree} angle
* @returns {DegreeType}
*/
export function degree(angle) {
return new Degree(angle);
}

/**
* @param {number | Degree | IDegree} angle
* @returns {IDegreeType}
*/
export function idegree(angle) {
if (angle instanceof IDegree) {
return angle;
Expand Down
Loading

0 comments on commit a5430ba

Please sign in to comment.