57inline std::string tostr(
const fx8& val) {
58 float float_value =
static_cast< float >( val );
59 return std::to_string( float_value );
62constexpr fx8 FX8_E =
fx8(696, 256);
63constexpr fx8 FX8_LOG2E =
fx8(369, 256);
64constexpr fx8 FX8_LOG10E =
fx8(111, 256);
65constexpr fx8 FX8_LN2 =
fx8(178, 256);
66constexpr fx8 FX8_LN10 =
fx8(590, 256);
67constexpr fx8 FX8_1_PI =
fx8(82, 256);
68constexpr fx8 FX8_2_PI =
fx8(163, 256);
69constexpr fx8 FX8_2_SQRTPI =
fx8(289, 256);
70constexpr fx8 FX8_SQRT2 =
fx8(362, 256);
71constexpr fx8 FX8_SQRT1_2 =
fx8(181, 256);
72constexpr fx8 FX8_2PI =
fx8(1608, 256);
73constexpr fx8 FX8_PI =
fx8(804, 256);
74constexpr fx8 FX8_PI_2 =
fx8(402, 256);
75constexpr fx8 FX8_PI_3 =
fx8(268, 256);
76constexpr fx8 FX8_PI_4 =
fx8(201, 256);
77constexpr fx8 FX8_PI_5 =
fx8(161, 256);
78constexpr fx8 FX8_PI_6 =
fx8(134, 256);
79constexpr fx8 FX8_PI_7 =
fx8(115, 256);
80constexpr fx8 FX8_PI_8 =
fx8(100, 256);
81constexpr fx8 FX8_PI_9 =
fx8(89, 256);
82constexpr fx8 FX8_PI_10 =
fx8(80, 256);
83constexpr fx8 FX8_PI_11 =
fx8(73, 256);
84constexpr fx8 FX8_PI_12 =
fx8(67, 256);
85constexpr fx8 FX8_PI_13 =
fx8(62, 256);
86constexpr fx8 FX8_PI_14 =
fx8(57, 256);
87constexpr fx8 FX8_PI_15 =
fx8(54, 256);
88constexpr fx8 FX8_PI_16 =
fx8(50, 256);
98constexpr fx12 FX12_E =
fx12(11135, 4096);
99constexpr fx12 FX12_LOG2E =
fx12(5912, 4096);
100constexpr fx12 FX12_LOG10E =
fx12(1785, 4096);
101constexpr fx12 FX12_LN2 =
fx12(2831, 4096);
102constexpr fx12 FX12_LN10 =
fx12(9438, 4096);
103constexpr fx12 FX12_1_PI =
fx12(1317, 4096);
104constexpr fx12 FX12_2_PI =
fx12(2634, 4096);
105constexpr fx12 FX12_2_SQRTPI =
fx12(4613, 4096);
106constexpr fx12 FX12_SQRT2 =
fx12(5793, 4096);
107constexpr fx12 FX12_SQRT1_2 =
fx12(2896, 4096);
108constexpr fx12 FX12_PI =
fx12(12868, 4096);
109constexpr fx12 FX12_PI_2 =
fx12(6434, 4096);
110constexpr fx12 FX12_PI_3 =
fx12(4290, 4096);
111constexpr fx12 FX12_PI_4 =
fx12(3217, 4096);
112constexpr fx12 FX12_PI_5 =
fx12(2574, 4096);
113constexpr fx12 FX12_PI_6 =
fx12(2145, 4096);
114constexpr fx12 FX12_PI_7 =
fx12(1838, 4096);
115constexpr fx12 FX12_PI_8 =
fx12(1609, 4096);
116constexpr fx12 FX12_PI_9 =
fx12(1432, 4096);
117constexpr fx12 FX12_PI_10 =
fx12(1287, 4096);
118constexpr fx12 FX12_PI_11 =
fx12(1171, 4096);
119constexpr fx12 FX12_PI_12 =
fx12(1072, 4096);
120constexpr fx12 FX12_PI_13 =
fx12(989, 4096);
121constexpr fx12 FX12_PI_14 =
fx12(919, 4096);
122constexpr fx12 FX12_PI_15 =
fx12(858, 4096);
123constexpr fx12 FX12_PI_16 =
fx12(804, 4096);
131 Vec(
fx8 x_ = 0,
fx8 y_ = 0) : x(x_), y(y_) {}
155 Vec& setWithAngle(
fx8 angle,
fx8 length);
158 Vec& addWithAngle(
fx8 angle,
fx8 length);
176 fx8 distanceTo(
const Vec& xy_)
const;
179 bool isInRect(
fx8 x_,
fx8 y_,
fx8 width,
fx8 height)
const;
180 bool isInRect(
const Rect& rc )
const;
183 bool equals(
const Vec& other)
const;
209 Vec operator+(
const Vec& other)
const {
210 return Vec(x + other.x, y + other.y);
214 Vec operator-(
const Vec& other)
const {
215 return Vec(x - other.x, y - other.y);
219 Vec& operator+=(
const Vec& other) {
226 Vec& operator-=(
const Vec& other) {
233 Vec operator*(
fx8 scalar)
const {
234 return Vec(x * scalar, y * scalar);
238 Vec& operator*=(
fx8 scalar) {
245 Vec operator*(
const Vec& other)
const {
246 return Vec(x * other.x, y * other.y);
250 Vec& operator*=(
const Vec& other) {
257 Vec operator/(
const Vec& other)
const {
258 fx8 rx = (other.x == 0) ?
fx8(0) : (x / other.x);
259 fx8 ry = (other.y == 0) ?
fx8(0) : (y / other.y);
264 Vec& operator/=(
const Vec& other) {
279 Vec operator/(
fx8 scalar)
const {
286 Vec& operator/=(
fx8 scalar) {
304 fx8 currentLen = length();
305 if (currentLen > maxLength && currentLen !=
fx8(0)) {
306 fx8 scale = maxLength / currentLen;
314inline std::string tostr(
const Vec& val) {
315 return '(' + tostr( val.x ) +
" , " + tostr( val.y ) +
')';
410inline std::string tostr(
const Rect& val) {
411 return '(' + tostr( val.x ) +
"," + tostr( val.y ) +
" , " +
412 tostr( val.w ) +
"," + tostr( val.h ) +
')';
424 return lhs_ < rhs_ ? lhs_ : rhs_;
435 return lhs_ > rhs_ ? lhs_ : rhs_;
447 return _min(
_max( lhs_, x_ ), rhs_ );
457 return x_ >
fx8(0) ? x_ : -x_;
473template <
typename T,
typename U,
typename V>
474inline T
lim(
const T& x_,
const U& lhs_,
const V& rhs_) {
475 return std::min(
static_cast<T
>(std::max(x_,
static_cast<T
>(lhs_))),
static_cast<T
>(rhs_));
484extern s16
sin_12( u32 th );
492extern s16
cos_12( u32 th );
539extern uint32_t
qmod(uint32_t x, uint32_t N);
559extern uint32_t
qdiv(uint32_t x, uint32_t N);
561#define M_E 2.71828182845904523536028747135266250
562#define M_LOG2E 1.44269504088896340735992468100189214
563#define M_LOG10E 0.434294481903251827651128918916605082
564#define M_LN2 0.693147180559945309417232121458176568
565#define M_LN10 2.30258509299404568401799145468436421
566#define M_PI 3.14159265358979323846264338327950288
567#define M_PI_2 1.57079632679489661923132169163975144
568#define M_PI_4 0.785398163397448309615660845819875721
569#define M_1_PI 0.318309886183790671537767526745028724
570#define M_2_PI 0.636619772367581343075535053490057448
571#define M_2_SQRTPI 1.12837916709551257389615890312154517
572#define M_SQRT2 1.41421356237309504880168872420969808
573#define M_SQRT1_2 0.707106781186547524400844362104849039
578 explicit Xorshift32(uint32_t seed = 0xA5A5A5A5 ) : state(seed) {
579 if (state == 0) state = 1;
583 state ^= state << 13;
584 state ^= state >> 17;
597 return static_cast< s32
>( next()>>1 );
610 if (min_ > max_) std::swap(min_, max_);
611 const uint64_t range =
static_cast<uint64_t
>(max_) -
static_cast<uint64_t
>(min_) + 1;
612 if (range >
static_cast<uint64_t
>(0x7fffffff)) {
616 }
while (
static_cast<uint64_t
>(random_val) >= range);
617 return min_ + random_val;
619 return min_ +
qmod( this->
next_int31() ,
static_cast<int32_t
>(range) );
624#define MAXFLOAT 0x1.fffffep+127f
Vec & clampLength(fx8 maxLength)
Clamps the length of the vector to the specified maximum value.
Definition submath.h:303
Represents a line segment in 2D space defined by two points.
Definition submath.h:327
Vec pos1
The ending point of the line.
Definition submath.h:329
Line(const Vec &p0, const Vec &p1)
Constructor that initializes the line with given starting and ending points.
Definition submath.h:342
Line()=default
Default constructor that initializes both points to (0, 0).
Vec pos0
The starting point of the line.
Definition submath.h:328
Represents a triangle in 2D space using three points.
Definition submath.h:355
Poly()=default
Default constructor.
Vec pos0
The first vertex of the triangle.
Definition submath.h:356
Vec pos2
The third vertex of the triangle.
Definition submath.h:358
Vec pos1
The second vertex of the triangle.
Definition submath.h:357
A structure for representing rectangles with fixed-point coordinates.
Definition submath.h:372
void SetXYWH(fx8 x_, fx8 y_, fx8 w_, fx8 h_)
Set the coordinates and dimensions of the rectangle.
Definition submath.h:386
fx8 MiddlePointX()
Calculate the middle point x-coordinate of the rectangle.
Definition submath.h:398
s32 next_range(int32_t min_, int32_t max_)
Generates a random number in the specified range [min_, max_].
Definition submath.h:609
s32 next_int31(void)
Generates a random number on [0, 0x7fffffff] interval.
Definition submath.h:596
Provides mathematical functions and utilities, primarily focused on fixed-point arithmetic.
T lim(const T &x_, const U &lhs_, const V &rhs_)
Limit a value within a specified range.
Definition submath.h:474
const fx8 & _max(const fx8 &lhs_, const fx8 &rhs_)
Get the maximum of two fixed-point values.
Definition submath.h:434
const fx8 & _lim(const fx8 &x_, const fx8 &lhs_, const fx8 &rhs_)
Limit a fixed-point value within a specified range.
Definition submath.h:446
s16 cos_12(u32 th)
Calculate the cosine of an angle using a 12-bit angle representation.
Definition submath.cpp:1040
fx8 rad_sin_12(fx8 rad, u32 th)
Calculate the sine of an angle and scale a fixed-point value by it.
Definition submath.cpp:1052
fx8 genrand_min_max_fx8(fx8 min_, fx8 max_)
Generate a random fixed-point number within a specified range.
Definition submath.cpp:1060
s16 sin_12(u32 th)
Calculate the sine of an angle using a 12-bit angle representation.
Definition submath.cpp:1033
fpm::fixed< std::int32_t, std::int64_t, 12 > fx12
Alias for fixed-point type with 12 fractional bits.
Definition submath.h:96
fpm::fixed< std::int32_t, std::int64_t, 8 > fx8
Alias for fixed-point type with 8 fractional bits.
Definition submath.h:56
uint32_t qdiv(uint32_t x, uint32_t N)
Computes the quotient of x divided by N using an optimized method.
Definition submath.cpp:1265
const fx8 _abs(const fx8 &x_)
Get the absolute value of a fixed-point number.
Definition submath.h:456
uint32_t qmod(uint32_t x, uint32_t N)
Computes the remainder of x divided by N using an optimized method.
Definition submath.cpp:1255
const fx8 & _min(const fx8 &lhs_, const fx8 &rhs_)
Get the minimum of two fixed-point values.
Definition submath.h:423
fx8 rad_cos_12(fx8 rad, u32 th)
Calculate the cosine of an angle and scale a fixed-point value by it.
Definition submath.cpp:1044