Removed some redundant classes from bluepixle.js and moved matrix class to bluemath.js
This commit is contained in:
parent
d29d4e9366
commit
1611f58512
@ -157,3 +157,85 @@ function normalizeArray(array) {
|
||||
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; }); }));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,13 +1,6 @@
|
||||
// bluepixle.js
|
||||
// This library is to help make using canvas tags easier
|
||||
|
||||
function deg2rad (deg) {
|
||||
return ((Math.PI*2)/360)*deg;
|
||||
}
|
||||
|
||||
function rad2deg (rad) {
|
||||
return (1/deg2rad(1))*rad;
|
||||
}
|
||||
// requires bluemath.js
|
||||
|
||||
function drift(object) {
|
||||
if (object instanceof container) {
|
||||
@ -20,19 +13,6 @@ function drift(object) {
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
function bounce(object, surface, impactTime) {
|
||||
object.direction = reflect(object.direction,surface.direction-90);
|
||||
object.speed = object.speed;
|
||||
@ -180,183 +160,6 @@ function drawPixel (ctx, x, y, r, g, b, a) {
|
||||
ctx.putImageData(new ImageData(new Uint8ClampedArray([r,g,b,a]),1,1),x,y);
|
||||
}
|
||||
|
||||
class matrix {
|
||||
constructor(m,n,matrixArray) {
|
||||
if (matrixArray.length != m*n) {
|
||||
return new RangeError("the size of the array does not match the size of the matrix!");
|
||||
} else {
|
||||
this.array = [];
|
||||
for (var row = 0; row < m; row++) {
|
||||
this.array[row] = [];
|
||||
for (var col = 0; col < n; col++) {
|
||||
this.array[row][col] = matrixArray[n*row+col];
|
||||
}
|
||||
}
|
||||
this.m = m;
|
||||
this.n = n;
|
||||
}
|
||||
}
|
||||
|
||||
log() {
|
||||
var string = "";
|
||||
for (var row = 0; row < this.m; row++) {
|
||||
string += "[";
|
||||
for( var col = 0; col < this.n; col++) {
|
||||
string += this.array[row][col];
|
||||
string += ",";
|
||||
}
|
||||
string += "]";
|
||||
console.log(string);
|
||||
string = "";
|
||||
}
|
||||
}
|
||||
|
||||
add(matrixb) {
|
||||
if (
|
||||
matrixb.m == this.m &&
|
||||
matrixb.n == this.n
|
||||
) {
|
||||
matrixb.array.forEach( function(row,rowIndex) {
|
||||
row.forEach( function(col,colIndex) {
|
||||
this.array[rowIndex][colIndex] += col;
|
||||
});
|
||||
});
|
||||
} else {
|
||||
throw new RangeError("cannot add matricies of different dimentions.");
|
||||
}
|
||||
}
|
||||
|
||||
scalarMult(scalar) {
|
||||
var data = this.array;
|
||||
data.forEach( function(row,rI) {
|
||||
row.forEach( function(col,cI) {
|
||||
data[rI][cI] *= scalar;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
transpose() {
|
||||
var tArray = [];
|
||||
for (var row = 0; row < this.n; row++) { tArray[row] = []; }
|
||||
for (var row = 0; row < this.m; row++) {
|
||||
for (var col = 0; col < this.n; col++) {
|
||||
tArray[col][row] = this.array[row][col];
|
||||
}
|
||||
}
|
||||
var rawArray = [];
|
||||
tArray.forEach(function (row,rI) {
|
||||
row.forEach( function(col,cI) {
|
||||
rawArray.push(tArray[rI][cI]);
|
||||
});
|
||||
});
|
||||
return new matrix(this.n,this.m,rawArray);
|
||||
}
|
||||
|
||||
multiply(matrixb) {
|
||||
if (matrixb.m == this.n) {
|
||||
var m = this.m;
|
||||
var n = this.n;
|
||||
var p = matrixb.n;
|
||||
var A = this;
|
||||
var B = matrixb;
|
||||
var newMatrix = [];
|
||||
for (var i = 0; i < m; i++) {
|
||||
newMatrix[i] = [];
|
||||
for (var j = 0; j < p; j++) {
|
||||
var sum = 0;
|
||||
for (var k = 0; k < n; k++) {
|
||||
sum += A.array[i][k] * B.array[k][j];
|
||||
}
|
||||
newMatrix[i][j] = sum;
|
||||
}
|
||||
}
|
||||
var rawArray = [];
|
||||
newMatrix.forEach(function (row,rI) {
|
||||
row.forEach( function (col,cI) {
|
||||
rawArray.push(newMatrix[rI][cI]);
|
||||
});
|
||||
});
|
||||
return new matrix(m,p,rawArray);
|
||||
} else {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class point {
|
||||
constructor(x,y) {
|
||||
this.x = "number" == typeof x ? x : 0;
|
||||
this.y = "number" == typeof y ? y : 0;
|
||||
}
|
||||
|
||||
getCoords() {
|
||||
return [this.x,this.y];
|
||||
}
|
||||
|
||||
setCoords(x,y) {
|
||||
this.x = "number" == typeof x ? x : 0;
|
||||
this.y = "number" == typeof y ? y : 0;
|
||||
}
|
||||
|
||||
setX(x) {
|
||||
this.x = "number" == typeof x ? x : 0;
|
||||
}
|
||||
|
||||
setY(y) {
|
||||
this.y = "number" == typeof y ? y : 0;
|
||||
}
|
||||
|
||||
}
|
||||
class vector {
|
||||
constructor(direction,magnitude) {
|
||||
this.direction = "number" == typeof direction ?
|
||||
0 <= direction ?
|
||||
360 >= direction ? direction : 360 : 0 : 0;
|
||||
this.magnitude = "number" == typeof magnitude ? magnitude : 0;
|
||||
}
|
||||
|
||||
getXYcomponents() {
|
||||
var theta = deg2rad(this.direction);
|
||||
var deltaX = Math.cos(theta)*this.magnitude;
|
||||
var deltaY = Math.sin(theta)*this.magnitude;
|
||||
return {"dx":deltaX,"dy":deltaY};
|
||||
}
|
||||
|
||||
adjustMag(value) {
|
||||
this.magnitude += "number" == typeof value ? value : 0;
|
||||
}
|
||||
|
||||
setMag(value) {
|
||||
this.magnitude = "number" == typeof value ? value : this.magnitude;
|
||||
}
|
||||
|
||||
adjustDir(value) {
|
||||
value = 0 <= value ? 360 >= value ? value: 360 : 0;
|
||||
this.direction += value;
|
||||
}
|
||||
|
||||
setDir(value) {
|
||||
this.direction = 0 <= value ? 360 >= value ? value: 360 : 0;
|
||||
}
|
||||
|
||||
executeVector(x,y) {
|
||||
var components = this.getXYcomponents();
|
||||
return new point(x+components.dx,y+components.dy);
|
||||
}
|
||||
|
||||
crossProduct(vectorB) {
|
||||
if (vectorB instanceof vector) {
|
||||
var V1 = this.executeVector(0,0);
|
||||
var V2 = vectorB.executeVector(0,0);
|
||||
return (Math.sin(V1.x) * Math.cos(V2.y))
|
||||
- (Math.cos(V1.y) * Math.sin(V2.x));
|
||||
} else {
|
||||
throw new TypeError("object is not a vector");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class lineSeg {
|
||||
constructor(x1,y1,x2,y2) {
|
||||
this.pointA = new point(x1,y1);
|
||||
|
Loading…
x
Reference in New Issue
Block a user