physics/vector.js

48 lines
661 B
JavaScript
Raw Permalink Normal View History

2024-06-24 15:49:18 -05:00
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];
}
}