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
58 lines • 1.59 kB
JavaScript
/**
* A symbolic representation of an angle. It may be
* manipulated in either degrees or radians.
*/
export default class Angle {
/**
* Generates a random angle in the range [0, 2π).
*/
static random(rng) {
return new Angle(rng.uniformFloat(0, 2 * Math.PI), 'rad');
}
constructor(value, unit) {
this.set(value, unit);
}
/**
* Sets the angle to a given value. The value will be wrapped to the range [0, 2π).
*/
set(value, unit) {
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, unit) {
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');
}
}
}
//# sourceMappingURL=Angle.js.map