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

71 lines (60 loc) 1.67 kB
import type RandomGenerator from './RandomGenerator' /** * A symbolic representation of an angle. It may be * manipulated in either degrees or radians. */ export default class Angle { // the angle in radians private _angle: number /** * Generates a random angle in the range [0, 2π). */ static random(rng: RandomGenerator) { return new Angle(rng.uniformFloat(0, 2 * Math.PI), 'rad') } constructor(value: number, unit: 'deg'|'rad') { this.set(value, unit) } /** * Sets the angle to a given value. The value will be wrapped to the range [0, 2π). */ set(value: number, unit: 'deg'|'rad') { switch (unit) { case 'deg': this._angle = (value * Math.PI / 180) % (2 * Math.PI) break case 'rad': this._angle = value % (2 * Math.PI) break default: throw new Error('Invalid unit') } } /** * Returns the angle in radians */ asRadians() { return this._angle } /** * Returns the angle in degrees */ asDegrees() { return this._angle * 180 / Math.PI } /** * Increments the angle by a given value. The angle will be wrapped to the range [0, 2π). */ increment(value: number, unit: 'deg'|'rad') { switch (unit) { case 'deg': this._angle = (this._angle + value * Math.PI / 180) % (2 * Math.PI) break case 'rad': this._angle = (this._angle + value) % (2 * Math.PI) break default: throw new Error('Invalid unit') } } }