frame.akima
Version:
A package for Akima interpolation
48 lines (47 loc) • 1.37 kB
JavaScript
export class AkimaPoint {
x;
y;
xOrigin;
yOrigin;
constructor(x, y, origin) {
this.xOrigin = origin ? origin.X : 0;
this.yOrigin = origin ? origin.Y : 0;
this.x = x;
this.y = y;
}
get X() {
return this.x;
}
set X(value) {
this.x = value;
}
get Y() {
return this.y;
}
set Y(value) {
this.y = value;
}
get XY() {
return { x: this.X, y: this.Y };
}
get Radius() {
return (Math.round(Math.sqrt(this.X * this.X + this.Y * this.Y) * 1000) / 1000);
}
/**
* Calculates the angle in radians between the line connecting this point to the given origin and the positive X-axis.
* The result is normalized to the range [0, 2π) and rounded to three decimal places.
*
* @param origin - The reference point from which the angle is measured.
* @returns The angle in radians, rounded to three decimal places, in the range [0, 2π).
*/
relAtan(origin) {
const cos = this.X - origin.X;
const sin = this.Y - origin.Y;
let atan2 = Math.atan2(sin, cos); // atan2 range is [-PI, PI]
if (atan2 < 0) {
// atan2 is negative, add 2 * Math.PI to make it positive
atan2 += Math.PI * 2;
}
return Math.round(atan2 * 1000) / 1000;
}
}