242 lines
7.4 KiB
JavaScript
242 lines
7.4 KiB
JavaScript
// bluemath.js
|
|
// This library adds advanced math objects
|
|
|
|
function deg2rad (deg) {
|
|
return ((Math.PI*2)/360)*deg;
|
|
}
|
|
|
|
function rad2deg (rad) {
|
|
return (1/deg2rad(1))*rad;
|
|
}
|
|
|
|
class vector {
|
|
constructor(dimentions=[0]) {
|
|
var me = this;
|
|
this.d = Array.isArray(dimentions) ? dimentions.map(d => !isNaN(d)) : [0];
|
|
|
|
if (me.d.length == 2) {
|
|
this.getRadAngle = function (vec) {
|
|
vec = vec instanceof vector ? vec : new vector([0,0]);
|
|
var xydims = vec.d.length == 2 ? vec.d : [...me.d.map((d,i) => vec[i] ? vec[i] : 0)];
|
|
return Math.atan2(xydims[1] - me.d[1], xydims[0] - me.d[0]);
|
|
}
|
|
|
|
this.getDegAngle = function (vec) {
|
|
return rad2deg(me.getRadAngle(vec));
|
|
}
|
|
|
|
this.resize = function (newLength) {
|
|
var angle = me.getDegAngle();
|
|
var soh = Math.sin(angle) * newLength;
|
|
var cah = Math.cos(angle) * newLength;
|
|
me.d = [cah,soh];
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
add(vec=new vector([...this.d.map(d => 0)])) {
|
|
vec = vec instanceof vector ? vec : new vector([0]);
|
|
var longest = vec.d.length > this.d.length ? vec.d : this.d;
|
|
var shortest = vec.d.length < this.d.length ? vec.d : this.d;
|
|
return new vector([...longest.map(function (value,index) {
|
|
var small = shortest[index];
|
|
if (!isNaN(small)) { return value + small; } else { return value; }
|
|
})]);
|
|
}
|
|
|
|
mult(vec=new vector([...this.d.map(d => 1)])) {
|
|
vec = vec instanceof vector ? vec : new vector([0]);
|
|
var longest = vec.d.length > this.d.length ? vec.d : this.d;
|
|
var shortest = vec.d.length < this.d.length ? vec.d : this.d;
|
|
return new vector([...longest.map(function (value,index) {
|
|
var small = shortest[index];
|
|
if (!isNaN(small)) { return value * small; } else { value; }
|
|
})]);
|
|
}
|
|
|
|
mag(mult=1) {
|
|
mult = !isNaN(mult) ? mult : 1;
|
|
return this.mult(new vector([...this.d.map(d => mult)]));
|
|
}
|
|
|
|
square() {
|
|
return this.mult(this);
|
|
}
|
|
|
|
getDistance(origin = new vector([...this.d.map(d => 0)])) {
|
|
origin = origin instanceof vector ? origin : new vector([...this.d.map(d => 0)]);
|
|
return Math.sqrt(this.d.map(function (value, index) {
|
|
var ori = !isNaN(origin.d[index]) ? origin.d[index] : 0;
|
|
return Math.pow(ori - value, 2);
|
|
}).reduce((a,b) => a + b));
|
|
}
|
|
}
|
|
|
|
function rad2vec (rad) {
|
|
var x = Math.cos(rad);
|
|
var y = Math.sin(rad);
|
|
return new vector([x,y]);
|
|
}
|
|
|
|
function deg2vec (deg) { //abstraction, really just for sanity
|
|
return rad2vec(deg2rad(deg));
|
|
}
|
|
|
|
function getAngle(ax,ay,bx,by) {
|
|
var deltaX = bx - ax;
|
|
var deltaY = by - ay;
|
|
return rad2deg(Math.atan2(deltaY,deltaX));
|
|
}
|
|
|
|
function reflect(inAngle,surfaceAngle) {
|
|
var normal = surfaceAngle+90;
|
|
var theta1 = normal - inAngle;
|
|
var theta2 = normal + theta1;
|
|
return theta2;
|
|
}
|
|
|
|
class bound {
|
|
constructor (min=[-Infinity],max=[Infinity]) {
|
|
// Insure both inputs are arrays with valid numbers
|
|
min = Array.isArray(min) ? min.map(value => !isNaN(value) ? value : -Infinity) : [-Infinity];
|
|
max = Array.isArray(max) ? max.map(value => !isNaN(value) ? value : -Infinity) : [Infinity];
|
|
// Insure both arrays are equal in size
|
|
while( min.length != max.length ) {
|
|
if (min.length > max.length) { max.push(Infinity);
|
|
} else { min.push(-Infinity); }
|
|
}
|
|
// Accept filtered bounds
|
|
this.min = min;
|
|
this.max = max;
|
|
}
|
|
|
|
inbounds(valueArray=[...this.min.map(v => 0)]) {
|
|
if (valueArray.length != this.min.length) { return false }
|
|
return valueArray.map((value,index) => (value >= this.min[index]) && (value <= this.max[index])).reduce((a,b) => a && b);
|
|
}
|
|
|
|
normalize(valueArray=[...this.min.map(v => 0)]) {
|
|
if (valueArray.length != this.min.length) { return [...this.min.map(v => 0)]; }
|
|
return valueArray.map((value,index) => (value - this.min[index])/(this.max[index] - this.min[index]));
|
|
}
|
|
|
|
clamp(valueArray=[...this.min.map(v => 0)]) {
|
|
if (valueArray.length != this.min.length) { return [...this.min.map((v,i) => Math.min(Math.max(0, v), this.max[i]))]; }
|
|
return valueArray.map((value,index) => Math.min(Math.max(value,this.min[index]),this.max[index]));
|
|
}
|
|
}
|
|
|
|
class boundValue {
|
|
constructor (bounds=new bound(),value=0) {
|
|
this.bounds = bounds instanceof bound ? bounds : new bound();
|
|
this.value = !isNaN(value) ? value : 0;
|
|
}
|
|
|
|
setValue(value=0) {
|
|
value = !isNaN(value) ? value : 0;
|
|
return this.value = this.bounds.clamp([value])[0];
|
|
}
|
|
|
|
getValue() {
|
|
return this.bounds.clamp([this.value])[0];
|
|
}
|
|
|
|
getValuePercent() {
|
|
return this.bounds.normalize([this.value])[0];
|
|
}
|
|
|
|
modValue(mod=0) {
|
|
mod = !isNaN(mod) ? mod : 0;
|
|
return this.setValue(this.value+mod);
|
|
}
|
|
}
|
|
|
|
function normalizeArray(array) {
|
|
array = Array.isArray(array) ? array.map(e => !isNaN(e) ? e : 0) : [1];
|
|
var sum = array.reduce((a,b) => a+b);
|
|
return array.map(v => v/sum);
|
|
}
|
|
|
|
class matrix {
|
|
constructor (data) {
|
|
var me = this;
|
|
this.data = [];
|
|
this.rows = 0;
|
|
this.cols = 0;
|
|
if (!Array.isArray(data)) {
|
|
me.data.push([typeof data == "number" ? data : 0]);
|
|
me.cols = me.data.length;
|
|
me.rows = me.data[0].length;
|
|
} else {
|
|
me.cols = data.length;
|
|
me.rows = data[0].length;
|
|
for (var i = 0; i < me.cols; i++) {
|
|
var thisCol = [];
|
|
for (var j = 0; j < me.rows; j++) {
|
|
thisCol.push(data[i][j]);
|
|
}
|
|
me.data.push(thisCol);
|
|
}
|
|
}
|
|
|
|
this.getRow = function (row) {
|
|
return me.data.map(function (col) { return col[row]; });
|
|
}
|
|
|
|
this.addRow = function (rowData) {
|
|
if (!Array.isArray(rowData)) { return console.error("Not provided an array: "+rowData); }
|
|
me.data.forEach(function (col,i) { if (!(rowData[i])) { rowData[i] = 0; } col.push(rowData[i]); });
|
|
}
|
|
|
|
this.getCol = function (col) {
|
|
return Array.from(me.data[col]);
|
|
}
|
|
|
|
this.addCol = function (colData) {
|
|
if (!Array.isArray(colData)) { return console.error("Not provided an array: "+colData); }
|
|
colData = me.data[0].map(function (a,x) { if (colData[x]) { return colData[x]; } else { return 0;} });
|
|
me.data.push(colData);
|
|
}
|
|
|
|
this.transpose = function () {
|
|
var resultCols = me.rows;
|
|
var newData = [];
|
|
for (var c = 0; c < resultCols; c++) {
|
|
newData.push(me.getRow(c));
|
|
}
|
|
return new matrix(newData);
|
|
}
|
|
|
|
this.multMat = function (matB) {
|
|
if (!(matB instanceof matrix)) { return console.error("Must provide matrix for this method"); }
|
|
if (me.cols != matB.rows) { return console.error("The number of columns in the first matrix should be equal to the number of rows in the second\n"+JSON.stringify(me.data)+" x "+JSON.stringify(matB.data)) }
|
|
var resultRows = me.rows;
|
|
var resultCols = matB.cols;
|
|
var newData = [];
|
|
for (var c = 0; c < resultCols; c++) {
|
|
var bcol = matB.getCol(c);
|
|
var thisCol = [];
|
|
for (var r = 0; r < resultRows; r++) {
|
|
thisCol.push(me.getRow(r).map(function (a,i) { var res = a * bcol[i]; return isNaN(res) ? 0 : res; }).reduce(function (a,b) { return a + b; }) );
|
|
}
|
|
newData.push(thisCol);
|
|
}
|
|
return new matrix(newData);
|
|
}
|
|
|
|
this.multNum = function (num) {
|
|
if (typeof num != "number") { return console.error("Must provide a number, provided "+(typeof num)); }
|
|
return new matrix(me.data.map(function (a) { return a.map(function (b) { var res = b * num; return isNaN(res) ? 0 : res; }); }));
|
|
}
|
|
|
|
this.addMat = function (matB) {
|
|
if (!(matB instanceof matrix)) { return console.error("Must provide matrix for this method"); }
|
|
if (me.cols != matB.cols) {return console.error("Matrices must be of the same width"); }
|
|
if (me.rows != matB.rows) {return console.error("Matrices must be of the same height"); }
|
|
return new matrix(me.data.map(function (col,x) { return col.map(function (a,y) { var res = a + matB.data[x][y]; return isNaN(res) ? 0 : res; }); }));
|
|
}
|
|
}
|
|
}
|
|
|