UNPKG

agentscape

Version:

Agentscape is a library for creating agent-based simulations. It provides a simple API for defining agents and their behavior, and for defining the environment in which the agents interact. Agentscape is designed to be flexible and extensible, allowing

85 lines 2.41 kB
import Angle from './Angle'; export default class Vector2 { static random(rng, min, max) { return new Vector2([rng.uniformFloat(min, max), rng.uniformFloat(min, max)]); } static randomInt(rng, min, max) { return new Vector2([rng.uniformInt(min, max), rng.uniformInt(min, max)]); } constructor(components) { this.components = components; } get x() { return this.components[0]; } get y() { return this.components[1]; } set x(value) { this.components[0] = value; } set y(value) { this.components[1] = value; } get magnitude() { return Math.sqrt(this.x ** 2 + this.y ** 2); } get normal() { const mag = this.magnitude; return new Vector2([this.x / mag, this.y / mag]); } get angle() { return new Angle(Math.atan2(this.y, this.x), 'rad'); } dot(other) { return this.x * other.x + this.y * other.y; } add(other) { return new Vector2([this.x + other.x, this.y + other.y]); } subtract(other) { return new Vector2([this.x - other.x, this.y - other.y]); } subtractScalar(scalar) { return new Vector2([this.x - scalar, this.y - scalar]); } divideScalar(scalar) { return new Vector2([this.x / scalar, this.y / scalar]); } divide(other) { return new Vector2([this.x / other.x, this.y / other.y]); } scale(scalar) { return new Vector2([this.x * scalar, this.y * scalar]); } lerp(other, alpha) { return this.add(other.subtract(this).scale(alpha)); } rotate(angle) { const cos = Math.cos(angle); const sin = Math.sin(angle); return new Vector2([ this.x * cos - this.y * sin, this.x * sin + this.y * cos ]); } clamp(min, max) { return new Vector2([ Math.min(Math.max(this.x, min), max), Math.min(Math.max(this.y, min), max) ]); } /** * Computes the Euclidean distance to an other vector */ euclideanDistance(other) { return Math.sqrt(this.euclideanDistanceSqr(other)); } /** * Computes the Euclidean distance to an other vector */ euclideanDistanceSqr(other) { return (this.x - other.x) ** 2 + (this.y - other.y) ** 2; } } //# sourceMappingURL=Vector2.js.map