Created deg struct for better usage of degree
This commit is contained in:
57
include/math.hpp
Normal file
57
include/math.hpp
Normal file
@@ -0,0 +1,57 @@
|
||||
#pragma once
|
||||
#include "stdint.h"
|
||||
|
||||
namespace math {
|
||||
template<typename T>
|
||||
struct raw_math {
|
||||
constexpr T operator-() const {
|
||||
return T{.raw = -(static_cast<const T&>(*this).raw)};
|
||||
}
|
||||
|
||||
constexpr T operator+(const T& obj) const {
|
||||
return T{.raw = static_cast<const T&>(*this).raw + obj.raw};
|
||||
}
|
||||
|
||||
constexpr T operator-(const T& b) const {}
|
||||
|
||||
constexpr T& operator+=(const T& obj) {
|
||||
static_cast<T&>(*this).raw += obj.raw;
|
||||
return static_cast<T&>(*this);
|
||||
}
|
||||
|
||||
constexpr T& operator-=(const T& obj) {
|
||||
static_cast<T&>(*this).raw -= obj.raw;
|
||||
return static_cast<T&>(*this);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
struct deg_t : public math::raw_math<deg_t> {
|
||||
static constexpr auto full_circle = 32768;
|
||||
static constexpr auto one_degree = full_circle/360;
|
||||
static constexpr auto one_tenth_degree = full_circle/3600;
|
||||
|
||||
int16_t raw;
|
||||
|
||||
static constexpr deg_t zero() {
|
||||
return deg_t{.raw = 0};
|
||||
}
|
||||
|
||||
static constexpr deg_t from_degree(int32_t deg) {
|
||||
return deg_t{.raw = static_cast<int16_t>(deg*one_degree)};
|
||||
}
|
||||
|
||||
static constexpr deg_t from_tenth_degree(int32_t deg10) {
|
||||
return deg_t{.raw = static_cast<int16_t>(deg10*one_tenth_degree)};
|
||||
}
|
||||
};
|
||||
|
||||
static constexpr deg_t operator""_deg(long double degree) {
|
||||
return deg_t::from_tenth_degree((degree*10.0));
|
||||
}
|
||||
|
||||
using sin_t = int32_t;
|
||||
using cos_t = int32_t;
|
||||
|
||||
sin_t sin(deg_t value);
|
||||
cos_t cos(deg_t value);
|
Reference in New Issue
Block a user