diff options
| author | a1xd <[email protected]> | 2021-09-03 18:09:00 -0400 |
|---|---|---|
| committer | a1xd <[email protected]> | 2021-09-23 22:28:44 -0400 |
| commit | 6a9272d3af202274dfbced245f0ba20b263fcd8b (patch) | |
| tree | b139e1d21aac0febc6105ac0d4c480f352d3064a /common/math-vec2.hpp | |
| parent | add per-device configuration (diff) | |
| download | rawaccel-6a9272d3af202274dfbced245f0ba20b263fcd8b.tar.xz rawaccel-6a9272d3af202274dfbced245f0ba20b263fcd8b.zip | |
refactor vec2/math
Diffstat (limited to 'common/math-vec2.hpp')
| -rw-r--r-- | common/math-vec2.hpp | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/common/math-vec2.hpp b/common/math-vec2.hpp new file mode 100644 index 0000000..2622926 --- /dev/null +++ b/common/math-vec2.hpp @@ -0,0 +1,36 @@ +#pragma once + +#define _USE_MATH_DEFINES +#include <math.h> + +template <typename T> +struct vec2 { + T x; + T y; +}; + +using vec2d = vec2<double>; + +inline vec2d direction(double degrees) +{ + double radians = degrees * M_PI / 180; + return { cos(radians), sin(radians) }; +} + +constexpr vec2d rotate(const vec2d& v, const vec2d& direction) +{ + return { + v.x * direction.x - v.y * direction.y, + v.x * direction.y + v.y * direction.x + }; +} + +inline double magnitude(const vec2d& v) +{ + return sqrt(v.x * v.x + v.y * v.y); +} + +inline double lp_distance(const vec2d& v, double p) +{ + return pow(pow(v.x, p) + pow(v.y, p), 1 / p); +} |