export default class Vector {
constructor(x, y) {
this._x = x;
this._y = y;
}
// Public
get x() { return this._x; }
get y() { return this._y; }
add(vector) {
this._x += vector.x;
this._y += vector.y;
return this;
}
subtract(vector) {
this._x -= vector.x;
this._y -= vector.y;
return this;
}
multiply(scalar) {
this._x *= scalar;
this._y *= scalar;
return this;
}
divide(scalar) {
this._x /= scalar;
this._y /= scalar;
return this;
}
shift(scalar) {
this._x += scalar;
this._y += scalar;
return this;
}
normalize(scalar) {
let magnitude = this.magnitude();
if (magnitude)
this.divide(magnitude);
else
this.multiply(0);
return this;
}
limit(scalar) {
if (this.magnitude() > scalar) {
this.normalize();
this.multiply(scalar);
}
return this;
}
distance(vector) {
return Math.sqrt(Math.pow(this._x - vector.x, 2) + Math.pow(this._y - vector.y, 2));
}
magnitude(scalar) {
return Math.sqrt(Math.pow(this._x, 2) + Math.pow(this._y, 2));
}
}