#ifndef LLMR_UTIL_MATH #define LLMR_UTIL_MATH #include #include #include "vec.hpp" namespace llmr { namespace util { template inline T max(T a, T b) { return b > a ? b : a; } template inline T max(T a, T b, T c) { return max(max(a, b), c); } template inline T max(T a, T b, T c, T d) { return max(max(a, b), max(c, d)); } template inline T min(T a, T b) { return b < a ? b : a; } template inline T min(T a, T b, T c) { return min(min(a, b), c); } template inline T min(T a, T b, T c, T d) { return min(min(a, b), min(c, d)); } // Find the angle of the two vectors, solving the formula for the cross product // a x b = |a||b|sin(θ) for θ. template inline T angle_between(S ax, S ay, S bx, S by) { return atan2((ax * by - ay * bx), ax * bx + ay * by); } template inline T angle_between(const vec2& a, const vec2& b) { return angle_between(a.x, a.y, b.x, b.y); } template inline T angle_to(const vec2& a, const vec2& b) { return atan2(a.y - b.y, a.x - b.x); } template inline T interp(S1 a, S2 b, T t) { return (a * ((T)1 - t)) + (b * t); } // Reflect an angle around 0 degrees template inline std::array flip(const std::array& c) { return {{ static_cast(2 * M_PI - c[0]), static_cast(2 * M_PI - c[1]) }}; } template inline vec2 normal(const S1& a, const S2& b) { T dx = b.x - a.x; T dy = b.y - a.y; T c = sqrt(dx * dx + dy * dy); return { dx / c, dy / c }; } template inline T dist(const S1& a, const S2& b) { T dx = b.x - a.x; T dy = b.y - a.y; T c = sqrt(dx * dx + dy * dy); return c; } // Take the magnitude of vector a. template inline T mag(const S& a) { return sqrt(a.x * a.x + a.y * a.y); } } } #endif