#include "MathUtils.h"

auto Utils::Math::getDistance(glm::vec3 from, glm::vec3 to) -> float {
	return std::sqrt(
		(from.x - to.x) * (from.x - to.x) +
		(from.y - to.y) * (from.y - to.y) +
		(from.z - to.z) * (from.z - to.z)
	);
}

auto Utils::Math::getAngle(glm::vec3 from, glm::vec3 to) -> glm::vec2 {
	glm::vec3 diff = glm::vec3(
		to.x - from.x,
		to.y - from.y,
		to.z - from.z
	);

	diff.y = diff.y / sqrtf(diff.x * diff.x + diff.y * diff.y);
	return glm::vec2(asinf(diff.y) * -(180.f / 3.1415927f), (float)-atan2f(diff.x, diff.z) * (180.f / 3.1415927f));
}

auto Utils::Math::animate(float end, float current, float speed) -> float {
	if (speed < 0.0) speed = 0.0;
	else if (speed > 1.0) speed = 1.0;

	float dif = std::max(end, current) - std::min(end, current);
	float factor = dif * speed;
	return current + (end > current ? factor : -factor);
}

auto Utils::Math::wrap(float value, float min, float max) -> float {
	return fmod(fmod(value - min, max - min) + (max - min), max - min) + min;
}

auto Utils::Math::lerp(float a, float b, float c) -> float {
	return a + c * (b - a);
}

auto Utils::Math::getrandom(float min, float max) -> float {
	static std::random_device rd;
	static std::mt19937 gen(rd());

	std::uniform_real_distribution<float> dis(min, max);
	return dis(gen);
}
