Last active
June 20, 2017 14:17
-
-
Save shape-of-myHeart/bd1f9450614e942e9fde06efde50c686 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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