Skip to content

Instantly share code, notes, and snippets.

@shape-of-myHeart
Last active June 20, 2017 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shape-of-myHeart/bd1f9450614e942e9fde06efde50c686 to your computer and use it in GitHub Desktop.
Save shape-of-myHeart/bd1f9450614e942e9fde06efde50c686 to your computer and use it in GitHub Desktop.
const { Vector2, isVector2 } = (
() => {
class _Vector2 {
constructor(x, y) {
let data = {
x: x || 0,
y: y || 0
};
const checkParmeterType = parameters => {
if (parameters[0] instanceof _Vector2) {
return parameters[0].get();
}
if (typeof parameters[0] === 'number' && typeof parameters[1] === 'number') {
return {
x: parameters[0],
y: parameters[1]
}
}
if (typeof parameters[0] === 'number') {
return {
x: parameters[0],
y: parameters[0]
};
}
return {
x: parameters[0],
y: parameters[1]
};
}
/* get */
this.get = () => data;
this.getX = () => data.x;
this.getY = () => data.y;
/* set */
this.set = (...parameters) => {
let pData = checkParmeterType(parameters);
data.x = pData.x === undefined ? data.x : pData.x;
data.y = pData.y === undefined ? data.y : pData.y;
return this;
}
this.setX = x => data.x = x;
this.setY = y => data.y = y;
/* add */
this.add = (...parameters) => {
let pData = checkParmeterType(parameters);
data.x += pData.x === undefined ? data.x : pData.x;
data.y += pData.y === undefined ? data.y : pData.y;
return this;
}
this.addX = x => data.x += x;
this.addY = y => data.y += y;
/* sub */
this.sub = (...parameters) => {
let pData = checkParmeterType(parameters);
data.x -= pData.x === undefined ? data.x : pData.x;
data.y -= pData.y === undefined ? data.y : pData.y;
return this;
}
this.subX = x => data.x -= x;
this.subY = y => data.y -= y;
/* multiply */
this.multiply = (...parameters) => {
let pData = checkParmeterType(parameters);
data.x *= pData.x === undefined ? 1 : pData.x;
data.y *= pData.y === undefined ? 1 : pData.y;
return this;
}
this.multiplyX = x => data.x *= x;
this.multiplyY = y => data.y *= y;
/* divide */
this.divide = (...parameters) => {
let pData = checkParmeterType(parameters);
data.x /= pData.x === undefined ? 1 : pData.x;
data.y /= pData.y === undefined ? 1 : pData.y;
return this;
}
this.divideX = x => data.x /= x;
this.devideY = y => data.y /= y;
/* distance */
this.distance = () => Math.sqrt(data.x * data.x + data.y * data.y);
/* normalize */
this.normalize = () => {
let distance = this.distance() || 1;
this.devide(distance.x, distance.y);
return this;
}
this.getNormalized = () => {
let vec2 = new _Vector2(data.x, data.y);
return vec2.normalize();
}
/* dot product */
this.dot = (...parameters) => {
let pData = checkParmeterType(parameters);
return data.x * pData.x + data.y * pData.y;
}
this.equal = (...parameters) => {
let pData = checkParmeterType(parameters);
return data.x === pData.x && data.y === pData.y;
}
this.clone = () => new _Vector2(data.x, data.y);
}
}
return {
isVector2: e => e instanceof _Vector2,
Vector2: (x, y) => new _Vector2(x, y)
};
}
)();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment