modularization / extract vector to the math module

This commit is contained in:
Val Erastov 2018-01-03 19:39:47 -08:00
parent b876818ce5
commit bbbdf2f9db
3 changed files with 154 additions and 146 deletions

147
modules/math/vector.js Normal file
View file

@ -0,0 +1,147 @@
export default class Vector {
constructor(x, y, z) {
this.x = x || 0;
this.y = y || 0;
this.z = z || 0;
}
set(x, y, z) {
this.x = x || 0;
this.y = y || 0;
this.z = z || 0;
return this;
}
set3(data) {
this.x = data[0] || 0;
this.y = data[1] || 0;
this.z = data[2] || 0;
return this;
}
setV(data) {
this.x = data.x;
this.y = data.y;
this.z = data.z;
return this;
}
multiply(scalar) {
return new Vector(this.x * scalar, this.y * scalar, this.z * scalar);
}
_multiply(scalar) {
return this.set(this.x * scalar, this.y * scalar, this.z * scalar);
}
divide(scalar) {
return new Vector(this.x / scalar, this.y / scalar, this.z / scalar);
}
_divide(scalar) {
return this.set(this.x / scalar, this.y / scalar, this.z / scalar);
}
dot(vector) {
return this.x * vector.x + this.y * vector.y + this.z * vector.z;
}
copy() {
return new Vector(this.x, this.y, this.z);
}
length() {
return Math.sqrt(this.x*this.x + this.y*this.y + this.z*this.z);
};
lengthSquared() {
return this.dot(this);
}
distanceToSquared(a) {
return this.minus(a).lengthSquared();
}
minus(vector) {
return new Vector(this.x - vector.x, this.y - vector.y, this.z - vector.z);
}
_minus(vector) {
this.x -= vector.x;
this.y -= vector.y;
this.z -= vector.z;
return this;
}
_minusXYZ(x, y, z) {
this.x -= x;
this.y -= y;
this.z -= z;
return this;
}
plusXYZ(x, y, z) {
return new Vector(this.x + x, this.y + y, this.z + z);
}
plus(vector) {
return new Vector(this.x + vector.x, this.y + vector.y, this.z + vector.z);
}
_plus(vector) {
this.x += vector.x;
this.y += vector.y;
this.z += vector.z;
return this;
}
normalize() {
let mag = this.length();
if (mag === 0.0) {
return new Vector(0.0, 0.0, 0.0);
}
return new Vector(this.x / mag, this.y / mag, this.z / mag);
}
_normalize() {
let mag = this.length();
if (mag === 0.0) {
return this.set(0, 0, 0)
}
return this.set(this.x / mag, this.y / mag, this.z / mag)
};
cross(a) {
return new Vector(
this.y * a.z - this.z * a.y,
this.z * a.x - this.x * a.z,
this.x * a.y - this.y * a.x
);
};
negate() {
return this.multiply(-1);
}
_negate() {
return this._multiply(-1);
}
toArray() {
return [this.x, this.y, this.z];
}
static fromData(arr) {
return new Vector().set3(arr);
}
}
Vector.prototype.data = Vector.prototype.toArray;
Vector.prototype.unit = Vector.prototype.normalize;
Vector.prototype._unit = Vector.prototype._normalize;
Vector.prototype.scale = Vector.prototype.multiply;
Vector.prototype._scale = Vector.prototype._multiply;

View file

@ -1,151 +1,9 @@
import {vectorsEqual} from './math'
import Vector from 'math/vector'
/** @constructor */
function Vector(x, y, z) {
this.x = x || 0;
this.y = y || 0;
this.z = z || 0;
}
Vector.prototype.set = function(x, y, z) {
this.x = x || 0;
this.y = y || 0;
this.z = z || 0;
return this;
};
Vector.prototype.set3 = function(data) {
this.x = data[0] || 0;
this.y = data[1] || 0;
this.z = data[2] || 0;
return this;
};
Vector.prototype.setV = function(data) {
this.x = data.x;
this.y = data.y;
this.z = data.z;
return this;
};
Vector.prototype.multiply = function(scalar) {
return new Vector(this.x * scalar, this.y * scalar, this.z * scalar);
};
Vector.prototype._multiply = function(scalar) {
return this.set(this.x * scalar, this.y * scalar, this.z * scalar);
};
Vector.prototype.divide = function(scalar) {
return new Vector(this.x / scalar, this.y / scalar, this.z / scalar);
};
Vector.prototype._divide = function(scalar) {
return this.set(this.x / scalar, this.y / scalar, this.z / scalar);
};
Vector.prototype.dot = function(vector) {
return this.x * vector.x + this.y * vector.y + this.z * vector.z;
};
Vector.prototype.copy = function() {
return new Vector(this.x, this.y, this.z);
};
Vector.prototype.length = function() {
return Math.sqrt(this.x*this.x + this.y*this.y + this.z*this.z);
};
Vector.prototype.lengthSquared = function() {
return this.dot(this);
};
Vector.prototype.distanceToSquared = function(a) {
return this.minus(a).lengthSquared();
};
Vector.prototype.minus = function(vector) {
return new Vector(this.x - vector.x, this.y - vector.y, this.z - vector.z);
};
Vector.prototype._minus = function(vector) {
this.x -= vector.x;
this.y -= vector.y;
this.z -= vector.z;
return this;
};
Vector.prototype._minusXYZ = function(x, y, z) {
this.x -= x;
this.y -= y;
this.z -= z;
return this;
};
Vector.prototype.plusXYZ = function(x, y, z) {
return new Vector(this.x + x, this.y + y, this.z + z);
};
Vector.prototype.plus = function(vector) {
return new Vector(this.x + vector.x, this.y + vector.y, this.z + vector.z);
};
Vector.prototype._plus = function(vector) {
this.x += vector.x;
this.y += vector.y;
this.z += vector.z;
return this;
};
Vector.prototype.normalize = function() {
var mag = this.length();
if (mag == 0.0) {
return new Vector(0.0, 0.0, 0.0);
}
return new Vector(this.x / mag, this.y / mag, this.z / mag);
};
Vector.prototype._normalize = function() {
var mag = this.length();
if (mag == 0.0) {
return this.set(0, 0, 0)
}
return this.set(this.x / mag, this.y / mag, this.z / mag)
};
Vector.prototype.cross = function(a) {
return new Vector(
this.y * a.z - this.z * a.y,
this.z * a.x - this.x * a.z,
this.x * a.y - this.y * a.x
);
};
Vector.prototype.negate = function() {
return this.multiply(-1);
};
Vector.prototype._negate = function() {
return this._multiply(-1);
};
Vector.prototype.equals = function(vector) {
return vectorsEqual(this, vector);
};
Vector.prototype.toArray = function() {
return [this.x, this.y, this.z];
};
Vector.prototype.data = Vector.prototype.toArray;
console.log(new Vector().fromData)
Vector.prototype.three = function() {
return new THREE.Vector3(this.x, this.y, this.z);
};
Vector.fromData = function(arr) {
return new Vector().set3(arr);
}
export default Vector;

View file

@ -2,6 +2,8 @@ const path = require('path');
const webpack = require('webpack');
const WEB_APP = path.join(__dirname, 'web/app');
const MODULES = path.join(__dirname, 'modules');
const INTEGRATION_TESTS = path.join(__dirname, 'web/test');
module.exports = {
devtool: 'source-map',
@ -22,13 +24,14 @@ module.exports = {
new webpack.HotModuleReplacementPlugin(),
],
resolve: {
extensions: ['.js', '.jsx']
extensions: ['.js', '.jsx'],
modules: [MODULES, "node_modules"]
},
module: {
rules: [{
test: /\.(js|jsx)$/,
loader: 'babel-loader',
include: [WEB_APP, path.join(__dirname, 'web/test')],
include: [MODULES, WEB_APP, INTEGRATION_TESTS],
options: {
plugins: [
['local-styles-transformer', {include: WEB_APP}]