48 lines
661 B
JavaScript
48 lines
661 B
JavaScript
class Vector {
|
|
constructor([x, y]) {
|
|
this.x = x;
|
|
this.y = y;
|
|
}
|
|
|
|
get length() {
|
|
return Math.sqrt(this.x**2 + this.y**2);
|
|
}
|
|
|
|
mult(s) {
|
|
return new Vector([
|
|
this.x * s,
|
|
this.y * s,
|
|
]);
|
|
}
|
|
|
|
add(v) {
|
|
if (!(v instanceof Vector)) {
|
|
v = new Vector(v);
|
|
}
|
|
return new Vector([
|
|
this.x + v.x,
|
|
this.y + v.y,
|
|
]);
|
|
}
|
|
|
|
sub(v) {
|
|
if (!(v instanceof Vector)) {
|
|
v = new Vector(v);
|
|
}
|
|
return new Vector([
|
|
this.x - v.x,
|
|
this.y - v.y,
|
|
]);
|
|
}
|
|
|
|
rot90() {
|
|
return new Vector([
|
|
-this.y,
|
|
this.x
|
|
]);
|
|
}
|
|
|
|
get array() {
|
|
return [this.x, this.y];
|
|
}
|
|
} |