export type Vector = [number, number];

export const distance = (v1: Vector, v2: Vector): number =>
	length(subtractV(copy(v1), v2));

export const angle = (v: Vector) => Math.atan2(v[1], v[0]);

export const equal = (v1: Vector, v2: Vector): boolean =>
	v1[0] === v2[0] && v1[1] === v2[1];

export const copy = (v: Vector): Vector => [v[0], v[1]];

export const normalize = (v: Vector): Vector => divide(v, length(v));

export const length = (v: Vector): number => Math.sqrt(dot(v, v));

export const dot = (v1: Vector, v2: Vector): number =>
	v1[0] * v2[0] + v1[1] * v1[1];

export const divide = (v: Vector, n: number): Vector => {
	if (n === 0) {
		v[0] = 0;
		v[1] = 0;
	} else {
		v[0] /= n;
		v[1] /= n;
	}
	return v;
};

export const multiply = (v: Vector, n: number): Vector => {
	v[0] *= n;
	v[1] *= n;
	return v;
};

export const addV = (v1: Vector, v2: Vector): Vector => {
	v1[0] += v2[0];
	v1[1] += v2[1];
	return v1;
};

export const subtractV = (v1: Vector, v2: Vector): Vector => {
	v1[0] -= v2[0];
	v1[1] -= v2[1];
	return v1;
};
