export function addVec(a, b) {
	return { x: a.x + b.x, y: a.y + b.y, z: a.z + b.z };
}

export function scaleVec(v, scalar) {
	return { x: v.x * scalar, y: v.y * scalar, z: v.z * scalar };
}

export function vecEquals(a, b) {
	return a.x === b.x && a.y === b.y && a.z === b.z;
}

export function normalizeVec(v, length) {
	let mag = Math.hypot(v.x, v.y, v.z);
	return {
		x: length * (v.x / mag),
		y: length * (v.y / mag),
		z: length * (v.z / mag)
	};
}

export function xAxis(v, scale) {
	let p = { x: v.z, y: 0, z: -v.x };
	return normalizeVec(p, scale);
}

export function yAxis(v, scale) {
	let m = Math.hypot(v.x, v.z);
	let p = {
		x: (v.x / m) * -v.y,
		y: m,
		z: (v.z / m) * -v.y
	};
	return normalizeVec(p, scale);
}

export function zAxis(v, scale) {
	return normalizeVec(v, scale);
}

export function relativePos(p, xs, ys, zs) {
	let d = p.getViewDirection();
	let l = p.location;
	let vx = xAxis(d, xs);
	let vy = yAxis(d, ys);
	let vz = zAxis(d, zs);
	return {
		x: l.x + vx.x + vy.x + vz.x,
		y: l.y + vx.y + vy.y + vz.y,
		z: l.z + vx.z + vy.z + vz.z
	};
}

export function approxEquals(a, b) {
	return Math.abs(a - b) < 1e-9
}

export function distanceSquared(a, b) {
	return (a.x - b.x) ** 2 + (a.y - b.y) ** 2 + (a.z - b.z) ** 2;
}