IceShard 1
A personal game engine project, with development focused on 2D/2.5D games.
Loading...
Searching...
No Matches
angles.hxx
Go to the documentation of this file.
1
3
4#pragma once
5#include <ice/base.hxx>
7
8namespace ice::math
9{
10
11 struct deg64;
12 struct deg32;
13 struct rad64;
14 struct rad32;
15
16 // TODO: Make a proper "scalar" base-type
17 template<typename T>
18 concept SimpleNumberType = std::is_arithmetic_v<T>;
19
20 template<typename T>
21 concept StrongScalarType = std::is_same_v<T, deg64> || std::is_same_v<T, deg32> || std::is_same_v<T, rad64> || std::is_same_v<T, rad32>;
22
23 struct deg64
24 {
25 constexpr auto operator*(this deg64 self, SimpleNumberType auto number) noexcept -> deg64 { return { self._value * ice::f64(number) }; }
26 constexpr auto operator/(this deg64 self, SimpleNumberType auto number) noexcept -> deg64 { return { self._value / ice::f64(number) }; }
27 constexpr auto operator+(this deg64 self, SimpleNumberType auto number) noexcept -> deg64 { return { self._value + ice::f64(number) }; }
28 constexpr auto operator-(this deg64 self, SimpleNumberType auto number) noexcept -> deg64 { return { self._value - ice::f64(number) }; }
29 constexpr auto operator+(this deg64 self, deg64 other) noexcept -> deg64 { return { self._value + other._value }; }
30 constexpr auto operator-(this deg64 self, deg64 other) noexcept -> deg64 { return { self._value - other._value }; }
31
32 constexpr auto operator<=>(this deg64 self, SimpleNumberType auto other) noexcept { return self._value <=> other; }
33 constexpr auto operator<=>(this deg64 self, deg64 other) noexcept { return self._value <=> other._value; }
34
35 constexpr auto raw_value(this deg64 self) noexcept -> f64 { return self._value; }
36 constexpr auto to_rad64(this deg64 self) noexcept -> rad64;
37 constexpr auto to_rad32(this deg64 self) noexcept -> rad32;
38
39 constexpr operator deg32() const noexcept;
40
41
43 };
44
45 struct deg32
46 {
47 constexpr auto operator*(this deg32 self, SimpleNumberType auto number) noexcept -> deg32 { return { self._value * ice::f32(number) }; }
48 constexpr auto operator/(this deg32 self, SimpleNumberType auto number) noexcept -> deg32 { return { self._value / ice::f32(number) }; }
49 constexpr auto operator+(this deg32 self, SimpleNumberType auto number) noexcept -> deg32 { return { self._value + ice::f32(number) }; }
50 constexpr auto operator-(this deg32 self, SimpleNumberType auto number) noexcept -> deg32 { return { self._value - ice::f32(number) }; }
51 constexpr auto operator+(this deg32 self, deg32 other) noexcept -> deg32 { return { self._value + other._value }; }
52 constexpr auto operator-(this deg32 self, deg32 other) noexcept -> deg32 { return { self._value - other._value }; }
53
54 constexpr auto operator<=>(this deg32 self, SimpleNumberType auto other) noexcept { return self._value <=> other; }
55 constexpr auto operator<=>(this deg32 self, deg32 other) noexcept { return self._value <=> other._value; }
56
57 constexpr auto raw_value(this deg32 self) noexcept -> f32 { return self._value; }
58 constexpr auto to_rad64(this deg32 self) noexcept -> rad64;
59 constexpr auto to_rad32(this deg32 self) noexcept -> rad32;
60
61 constexpr operator deg64() const noexcept;
62
64 };
65
66 struct rad64
67 {
68 constexpr auto raw_value(this rad64 self) noexcept -> f64 { return self._value; }
69 constexpr auto to_rad32(this rad64 self) noexcept -> rad32;
70 constexpr auto to_deg64(this rad64 self) noexcept -> deg64;
71 constexpr auto to_deg32(this rad64 self) noexcept -> deg32;
72
74 };
75
76 struct rad32
77 {
78 constexpr auto raw_value(this rad32 self) noexcept -> f32 { return self._value; }
79 constexpr auto to_rad64(this rad32 self) noexcept -> rad64;
80 constexpr auto to_deg64(this rad32 self) noexcept -> deg64;
81 constexpr auto to_deg32(this rad32 self) noexcept -> deg32;
82
84 };
85
86 // deg64
87 inline constexpr auto deg64::to_rad64(this deg64 self) noexcept -> rad64
88 {
89 return rad64{ self._value * (ice::math::f64_pi / 180.0) };
90 }
91
92 inline constexpr auto deg64::to_rad32(this deg64 self) noexcept -> rad32
93 {
94 return rad32{ ice::f32(self._value) * (ice::math::f32_pi / 180.0f) };
95 }
96
97 constexpr deg64::operator deg32() const noexcept
98 {
99 return { ice::f32(_value) };
100 }
101
102 // deg32
103 inline constexpr auto deg32::to_rad64(this deg32 self) noexcept -> rad64
104 {
105 return rad64{ ice::f64(self._value) * (ice::math::f64_pi / 180.0) };
106 }
107
108 inline constexpr auto deg32::to_rad32(this deg32 self) noexcept -> rad32
109 {
110 return rad32{ self._value * (ice::math::f32_pi / 180.0f) };
111 }
112
113 constexpr deg32::operator deg64() const noexcept
114 {
115 return { _value };
116 }
117
118 // rad64
119 inline constexpr auto rad64::to_rad32(this rad64 self) noexcept -> rad32
120 {
121 return rad32{ ice::f32(self._value) };
122 }
123
124 inline constexpr auto rad64::to_deg64(this rad64 self) noexcept -> deg64
125 {
126 return deg64{ (self._value * 180.0) / ice::math::f64_pi };
127 }
128
129 inline constexpr auto rad64::to_deg32(this rad64 self) noexcept -> deg32
130 {
131 return deg32{ (ice::f32(self._value) * 180.0f) / ice::math::f32_pi };
132 }
133
134 // rad32
135 inline constexpr auto rad32::to_rad64(this rad32 self) noexcept -> rad64
136 {
137 return rad64{ self._value };
138 }
139
140 inline constexpr auto rad32::to_deg64(this rad32 self) noexcept -> deg64
141 {
142 return deg64{ (ice::f64(self._value) * 180.0) / ice::math::f64_pi };
143 }
144
145 inline constexpr auto rad32::to_deg32(this rad32 self) noexcept -> deg32
146 {
147 return deg32{ (self._value * 180.0f) / ice::math::f32_pi };
148 }
149
150 inline constexpr auto operator""_deg(long double value) noexcept -> ice::math::deg64
151 {
152 return { static_cast<ice::f64>(value) };
153 }
154
155 // Operators
156
157 template<StrongScalarType ScalarType, SimpleNumberType NumberType>
158 inline constexpr auto operator*(ScalarType left, NumberType right) noexcept -> ScalarType
159 {
160 return { left.raw_value() * right };
161 }
162
163 template<StrongScalarType ScalarType, SimpleNumberType NumberType>
164 inline constexpr auto operator/(ScalarType left, NumberType right) noexcept -> ScalarType
165 {
166 return { left.raw_value() / right };
167 }
168
169 using deg = deg32;
170 using rad = rad32;
171
172} // namespace ice::math
Definition angles.hxx:18
Definition angles.hxx:21
Definition algorithm.hxx:8
rad32 rad
Definition angles.hxx:170
static constexpr ice::f32 f32_pi
Definition constants.hxx:11
deg32 deg
Definition angles.hxx:169
static constexpr ice::f64 f64_pi
Definition constants.hxx:15
double f64
Definition types.hxx:17
float f32
Definition types.hxx:16
Definition angles.hxx:46
constexpr auto to_rad64(this deg32 self) noexcept -> rad64
Definition angles.hxx:103
f32 _value
Definition angles.hxx:63
constexpr auto to_rad32(this deg32 self) noexcept -> rad32
Definition angles.hxx:108
constexpr auto operator/(this deg32 self, SimpleNumberType auto number) noexcept -> deg32
Definition angles.hxx:48
constexpr auto operator-(this deg32 self, SimpleNumberType auto number) noexcept -> deg32
Definition angles.hxx:50
constexpr auto operator<=>(this deg32 self, SimpleNumberType auto other) noexcept
Definition angles.hxx:54
constexpr auto raw_value(this deg32 self) noexcept -> f32
Definition angles.hxx:57
constexpr auto operator+(this deg32 self, deg32 other) noexcept -> deg32
Definition angles.hxx:51
constexpr auto operator-(this deg32 self, deg32 other) noexcept -> deg32
Definition angles.hxx:52
constexpr auto operator*(this deg32 self, SimpleNumberType auto number) noexcept -> deg32
Definition angles.hxx:47
constexpr auto operator<=>(this deg32 self, deg32 other) noexcept
Definition angles.hxx:55
constexpr auto operator+(this deg32 self, SimpleNumberType auto number) noexcept -> deg32
Definition angles.hxx:49
Definition angles.hxx:24
constexpr auto operator<=>(this deg64 self, SimpleNumberType auto other) noexcept
Definition angles.hxx:32
constexpr auto operator*(this deg64 self, SimpleNumberType auto number) noexcept -> deg64
Definition angles.hxx:25
constexpr auto operator<=>(this deg64 self, deg64 other) noexcept
Definition angles.hxx:33
f64 _value
Definition angles.hxx:42
constexpr auto operator-(this deg64 self, SimpleNumberType auto number) noexcept -> deg64
Definition angles.hxx:28
constexpr auto to_rad64(this deg64 self) noexcept -> rad64
Definition angles.hxx:87
constexpr auto to_rad32(this deg64 self) noexcept -> rad32
Definition angles.hxx:92
constexpr auto operator+(this deg64 self, SimpleNumberType auto number) noexcept -> deg64
Definition angles.hxx:27
constexpr auto operator/(this deg64 self, SimpleNumberType auto number) noexcept -> deg64
Definition angles.hxx:26
constexpr auto raw_value(this deg64 self) noexcept -> f64
Definition angles.hxx:35
constexpr auto operator-(this deg64 self, deg64 other) noexcept -> deg64
Definition angles.hxx:30
constexpr auto operator+(this deg64 self, deg64 other) noexcept -> deg64
Definition angles.hxx:29
Definition angles.hxx:77
constexpr auto to_deg64(this rad32 self) noexcept -> deg64
Definition angles.hxx:140
constexpr auto to_rad64(this rad32 self) noexcept -> rad64
Definition angles.hxx:135
constexpr auto to_deg32(this rad32 self) noexcept -> deg32
Definition angles.hxx:145
constexpr auto raw_value(this rad32 self) noexcept -> f32
Definition angles.hxx:78
f32 _value
Definition angles.hxx:83
Definition angles.hxx:67
constexpr auto to_deg32(this rad64 self) noexcept -> deg32
Definition angles.hxx:129
constexpr auto to_rad32(this rad64 self) noexcept -> rad32
Definition angles.hxx:119
f64 _value
Definition angles.hxx:73
constexpr auto to_deg64(this rad64 self) noexcept -> deg64
Definition angles.hxx:124
constexpr auto raw_value(this rad64 self) noexcept -> f64
Definition angles.hxx:68