@tubular/math
Version:
Miscellaneous math functions
51 lines • 1.85 kB
JavaScript
import { SphericalPosition } from './spherical-position';
import { Angle } from './angle';
export class SphericalPosition3D extends SphericalPosition {
constructor(longitude, latitude, _radius = 0, longUnit, latUnit) {
super(longitude, latitude, longUnit, latUnit);
this._radius = _radius;
}
static convertRectangular(xOrPoint, y, z) {
let x;
if (typeof xOrPoint === 'number') {
x = xOrPoint;
if (y === undefined || z === undefined)
throw new Error('Invalid arguments');
}
else {
x = xOrPoint.x;
y = xOrPoint.y;
z = xOrPoint.z;
}
return new SphericalPosition3D(Angle.atan2_nonneg(y, x), Angle.atan2(z, Math.sqrt(x * x + y * y)), Math.sqrt(x * x + y * y + z * z));
}
static from2D(pos, radius) {
return new SphericalPosition3D(pos.longitude, pos.latitude, radius);
}
get radius() {
return this._radius;
}
get xyz() {
return {
x: this._radius * this._latitude.cos * this._longitude.cos,
y: this._radius * this._latitude.cos * this._longitude.sin,
z: this._radius * this._latitude.sin
};
}
translate(newOrigin) {
const L0 = newOrigin.longitude;
const B0 = newOrigin.latitude;
const R0 = newOrigin.radius;
const L = this.longitude;
const B = this.latitude;
const R = this.radius;
const x = R * B.cos * L.cos - R0 * B0.cos * L0.cos;
const y = R * B.cos * L.sin - R0 * B0.cos * L0.sin;
const z = R * B.sin - R0 * B0.sin;
return SphericalPosition3D.convertRectangular(x, y, z);
}
toString() {
return super.toString() + ', rad: ' + this.radius.toFixed(5);
}
}
//# sourceMappingURL=spherical-position-3d.js.map