boids/vector.mjs

86 lines
1.4 KiB
JavaScript

class Vector
{
constructor(x = 0, y = 0)
{
this.x = x;
this.y = y;
}
add(vector)
{
this.x += vector.x;
this.y += vector.y;
return this;
}
addMul(vector, scalar)
{
this.x += vector.x * scalar;
this.y += vector.y * scalar;
return this;
}
sub(vector)
{
this.x -= vector.x;
this.y -= vector.y;
return this;
}
mul(scalar)
{
this.x *= scalar;
this.y *= scalar;
return this;
}
div(scalar)
{
return this.mul(1 / scalar);
}
rotate(angle)
{
const cos = Math.cos(angle);
const sin = Math.sin(angle);
const nextX = cos * this.x - sin * this.y;
const nextY = cos * this.y + sin * this.x;
this.x = nextX;
this.y = nextY;
return this;
}
angle()
{
return Math.PI / 2 - Math.atan2(this.x, this.y);
}
normSquared()
{
return this.x * this.x + this.y * this.y;
}
static distSquared(lhs, rhs)
{
return (
(lhs.x - rhs.x) * (lhs.x - rhs.x)
+ (lhs.y - rhs.y) * (lhs.y - rhs.y)
);
}
reset()
{
this.x = 0;
this.y = 0;
return this;
}
clone()
{
return new Vector(this.x, this.y);
}
}
export default Vector;