@openhps/core
Version:
Open Hybrid Positioning System - Core component
86 lines • 2.58 kB
JavaScript
import { __decorate, __metadata } from "tslib";
import { AbsolutePosition } from './AbsolutePosition';
import { NumberType, SerializableMember, SerializableObject } from '../decorators';
import { LengthUnit } from '../../utils';
import { Vector3 } from '../../utils/math';
/**
* Absolute cartesian 2D position. This class uses a {@link Vector2}. This location can be used both as
* an absolute location or relative location.
* @category Position
*/
let Absolute2DPosition = class Absolute2DPosition extends AbsolutePosition {
constructor(x, y, unit = LengthUnit.METER) {
super();
this.vector = new Vector3();
this.vector.x = x ? x : 0;
this.vector.y = y ? y : 0;
this.unit = unit;
}
get x() {
if (!this.vector) {
return undefined;
}
return this.vector.x;
}
set x(value) {
if (!this.vector) {
return;
}
this.vector.x = value;
}
get y() {
if (!this.vector) {
return undefined;
}
return this.vector.y;
}
set y(value) {
if (!this.vector) {
return;
}
this.vector.y = value;
}
/**
* Get the angle in radians from this position to a destination
* @param {Absolute2DPosition} destination Destination position
* @returns {number} Bearing in radians from this position to destination
*/
angleTo(destination) {
return this.vector.angleTo(destination.vector);
}
fromVector(vector, unit) {
if (unit) {
this.x = unit.convert(vector.x, this.unit);
this.y = unit.convert(vector.y, this.unit);
} else {
this.x = vector.x;
this.y = vector.y;
}
return this;
}
toVector3(unit) {
if (unit) {
return new Vector3(this.unit.convert(this.x, unit), this.unit.convert(this.y, unit));
} else {
return new Vector3(this.x, this.y);
}
}
/**
* Clone the position
* @returns {Absolute2DPosition} Cloned position
*/
clone() {
const position = super.clone();
position.x = this.x;
position.y = this.y;
return position;
}
};
__decorate([SerializableMember({
numberType: NumberType.DECIMAL
}), __metadata("design:type", Number), __metadata("design:paramtypes", [Number])], Absolute2DPosition.prototype, "x", null);
__decorate([SerializableMember({
numberType: NumberType.DECIMAL
}), __metadata("design:type", Number), __metadata("design:paramtypes", [Number])], Absolute2DPosition.prototype, "y", null);
Absolute2DPosition = __decorate([SerializableObject(), __metadata("design:paramtypes", [Number, Number, LengthUnit])], Absolute2DPosition);
export { Absolute2DPosition };