@thi.ng/vectors
Version:
Optimized 2d/3d/4d and arbitrary length vector operations, support for memory mapping/layouts
39 lines (38 loc) • 877 B
JavaScript
import { cossin } from "@thi.ng/math/angle";
import { add2 } from "./add.js";
import {
ZERO2,
ZERO3
} from "./api.js";
import { setC3 } from "./setc.js";
import { vop } from "./vop.js";
const cos = Math.cos;
const sin = Math.sin;
const cartesian = vop(1);
const cartesian2 = cartesian.add(
2,
(out, v, offset = ZERO2) => add2(out || v, cossin(v[1], v[0]), offset)
);
const cartesian3 = cartesian.add(3, (out, v, offset = ZERO3) => {
const r = v[0];
const theta = v[1];
const phi = v[2];
const ct = cos(theta);
return setC3(
out || v,
r * ct * cos(phi) + offset[0],
r * ct * sin(phi) + offset[1],
r * sin(theta) + offset[2]
);
});
const cartesian2FromAngles = (angles, n) => {
const polar = [];
for (let x of angles) polar.push(cossin(x, n));
return polar;
};
export {
cartesian,
cartesian2,
cartesian2FromAngles,
cartesian3
};