UNPKG

@tubular/math

Version:
47 lines 1.6 kB
import { Angle, Mode, Unit } from './angle'; import { abs, acos, limitNeg1to1, mod2, TWO_PI } from './math'; export class SphericalPosition { constructor(longitude = 0, latitude = 0, longUnit = Unit.RADIANS, latUnit = Unit.RADIANS) { if (typeof longitude === 'number') this._longitude = new Angle(longitude, longUnit, Mode.RANGE_LIMIT_NONNEGATIVE); else this._longitude = longitude; if (typeof latitude === 'number') this._latitude = new Angle(latitude, latUnit); else this._latitude = latitude; } get longitude() { return this._longitude; } get rightAscension() { return this._longitude; } get altitude() { return this._latitude; } get azimuth() { return this._longitude; } get latitude() { return this._latitude; } get declination() { return this._latitude; } distanceFrom(p) { // Tiny rounding errors which can make the argument of acos very slightly larger than 1.0 // or very slight smaller than -1.0 are enough to blow up the acos function and get you a // NaN for your trouble. // let d = acos(limitNeg1to1(this._latitude.sin * p._latitude.sin + this._latitude.cos * p._latitude.cos * this._longitude.subtract(p._longitude).cos)); d = abs(mod2(d, TWO_PI)); return new Angle(d); } toString() { return 'lon: ' + this.longitude + ', lat: ' + this.latitude; } } //# sourceMappingURL=spherical-position.js.map