File: util_math.h

package info (click to toggle)
dosbox-x 2026.01.02%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 53,220 kB
  • sloc: cpp: 341,269; ansic: 165,494; sh: 1,463; makefile: 967; perl: 385; python: 106; asm: 57
file content (47 lines) | stat: -rw-r--r-- 1,283 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

#include <math.h> /* sqrtf */

//! \brief Get value sign, i.e. less than zero: -1, zero: 0, greater than zero: 1.
template <typename T> static inline int sgn(const T val) {
	// http://stackoverflow.com/questions/1903954/is-there-a-standard-sign-function-signum-sgn-in-c-c
	return (T(0) < val) - (val < T(0));
}

//! \brief Floating-point vector with 2 components.
struct DOSBox_Vector2
{
	float X, Y;

	DOSBox_Vector2(const float x, const float y) : X(x), Y(y) { }
	DOSBox_Vector2() : X(0.0f), Y(0.0f) { }

	DOSBox_Vector2 clamp(const DOSBox_Vector2 min, const DOSBox_Vector2 max) const {
		float x = this->X;
		float y = this->Y;
		float xmin = min.X;
		float xmax = max.X;
		float ymin = min.Y;
		float ymax = max.Y;
		x = x < xmin ? xmin : x > xmax ? xmax : x;
		y = y < ymin ? ymin : y > ymax ? ymax : y;
		DOSBox_Vector2 clamp = DOSBox_Vector2(x, y);
		return clamp;
	}

	inline float magnitude(void) const {
		return sqrtf(sqrMagnitude());
	}

	inline float sqrMagnitude(void) const {
		return X * X + Y * Y;
	}

	DOSBox_Vector2 normalized(void) const {
		float m = this->magnitude();
		return m > 0.0f ? DOSBox_Vector2(this->X / m, this->Y / m) : DOSBox_Vector2();
	}

	DOSBox_Vector2 operator*(const float f) const {
		return DOSBox_Vector2(this->X * f, this->Y * f);
	}
};