DOSBox-X
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
include/util_math.h
00001 
00003 template <typename T> static inline int sgn(const T val) {
00004         // http://stackoverflow.com/questions/1903954/is-there-a-standard-sign-function-signum-sgn-in-c-c
00005         return (T(0) < val) - (val < T(0));
00006 }
00007 
00009 struct DOSBox_Vector2
00010 {
00011         float X, Y;
00012 
00013         DOSBox_Vector2(const float x, const float y) : X(x), Y(y) { }
00014         DOSBox_Vector2() : X(0.0f), Y(0.0f) { }
00015 
00016         DOSBox_Vector2 clamp(const DOSBox_Vector2 min, const DOSBox_Vector2 max) const {
00017                 float x = this->X;
00018                 float y = this->Y;
00019                 float xmin = min.X;
00020                 float xmax = max.X;
00021                 float ymin = min.Y;
00022                 float ymax = max.Y;
00023                 x = x < xmin ? xmin : x > xmax ? xmax : x;
00024                 y = y < ymin ? ymin : y > ymax ? ymax : y;
00025                 DOSBox_Vector2 clamp = DOSBox_Vector2(x, y);
00026                 return clamp;
00027         }
00028 
00029         inline float magnitude(void) const {
00030                 return (float)sqrt(sqrMagnitude());
00031         }
00032 
00033         inline float sqrMagnitude(void) const {
00034                 return X * X + Y * Y;
00035         }
00036 
00037         DOSBox_Vector2 normalized(void) const {
00038                 float m = this->magnitude();
00039                 return m > 0.0f ? DOSBox_Vector2(this->X / m, this->Y / m) : DOSBox_Vector2();
00040         }
00041 
00042         DOSBox_Vector2 operator*(const float f) const {
00043                 return DOSBox_Vector2(this->X * f, this->Y * f);
00044         }
00045 };
00046