Created
May 28, 2019 22:16
-
-
Save m0wh/ee34d279e044a1d358427f898e9684ea to your computer and use it in GitHub Desktop.
WIP: a nice and simple vector 2D class
This file contains 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
class Vector { | |
constructor(x, y) { | |
this.x = x | |
this.y = y | |
} | |
get x () { return this.x } | |
get y () { return this.y } | |
set x (newX) { this.x = newX } | |
set x (newY) { this.y = newY } | |
// Math operations | |
addX (other) { | |
if (typeof other !== typeof this) throw Error('You can only add a Vector to a Vector.') | |
this.x += other.x | |
return this | |
} | |
addY (other) { | |
if (typeof other !== typeof this) throw Error('You can only add a Vector to a Vector.') | |
this.y += other.y | |
return this | |
} | |
add (other) { | |
if (typeof other !== typeof this) throw Error('You can only add a Vector to a Vector.') | |
this.x += other.x | |
this.y += other.y | |
return this | |
} | |
subX (other) { | |
if (typeof other !== typeof this) throw Error('You can only substract a Vector to a Vector.') | |
this.x -= other.x | |
return this | |
} | |
subY (other) { | |
if (typeof other !== typeof this) throw Error('You can only substract a Vector to a Vector.') | |
this.y -= other.y | |
return this | |
} | |
sub (other) { | |
if (typeof other !== typeof this) throw Error('You can only substract a Vector to a Vector.') | |
this.x -= other.x | |
this.y -= other.y | |
return this | |
} | |
multX (other) { | |
if (typeof other !== typeof this) throw Error('You can only multiply a Vector to a Vector.') | |
this.x *= other.x | |
return this | |
} | |
multY (other) { | |
if (typeof other !== typeof this) throw Error('You can only multiply a Vector to a Vector.') | |
this.y *= other.y | |
return this | |
} | |
mult (other) { | |
if (typeof other !== typeof this) throw Error('You can only multiply a Vector to a Vector.') | |
this.x *= other.x | |
this.y *= other.y | |
return this | |
} | |
divX (other) { | |
if (typeof other !== typeof this) throw Error('You can only divide a Vector to a Vector.') | |
this.x /= other.x | |
return this | |
} | |
divY (other) { | |
if (typeof other !== typeof this) throw Error('You can only divide a Vector to a Vector.') | |
this.y /= other.y | |
return this | |
} | |
div (other) { | |
if (typeof other !== typeof this) throw Error('You can only divide a Vector to a Vector.') | |
this.x /= other.x | |
this.y /= other.y | |
return this | |
} | |
mirrorX () { | |
this.x *= -1 | |
return this | |
} | |
mirrorY () { | |
this.y *= -1 | |
return this | |
} | |
mirror () { | |
this.y *= -1 | |
this.y *= -1 | |
return this | |
} | |
copyX (other) { | |
this.x = other.x | |
return this | |
} | |
copyY (other) { | |
this.y = other.y | |
return this | |
} | |
copy (other) { | |
this.x = other.x | |
this.y = other.y | |
return this | |
} | |
clone () { | |
return new Vector(this.x, this.y) | |
} | |
// Other operations | |
toString (decimals = 2) { return `x: ${this.x.toFixed(decimals)}, y: ${this.y.toFixed(decimals)}` } | |
toArray () { return [this.x, this.y] } | |
toObject () { return {x: this.x, y: this.y} } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment