#ifndef MATHS_H #define MATHS_H #include #include #include // Type definition typedef unsigned char uchar; typedef uchar byte; typedef short int16; typedef unsigned short uint16; typedef unsigned int uint; // Define M_PI #ifndef M_PI #define M_PI 3.14159265358979323846 // pi #endif // !M_PI #pragma warning( disable : 4244 ) struct Point2 { int x; int y; }; struct Vec2 { float x; float y; Vec2() : x(0.0f), y(0.0f) {} Vec2(float _val) : x(_val), y(_val) {} Vec2(float _x, float _y) : x(_x), y(_y) {} // Vec2 with Vec2 operations inline Vec2 operator+(const Vec2& rhs) const { return Vec2(x + rhs.x, y + rhs.y); } inline Vec2 operator-(const Vec2& rhs) const { return Vec2(x - rhs.x, y - rhs.y); } inline Vec2 operator*(const Vec2& rhs) const { return Vec2(x * rhs.x, y * rhs.y); } inline Vec2 operator/(const Vec2& rhs) const { return Vec2(x / rhs.x, y / rhs.y); } inline Vec2& operator+=(const Vec2& rhs) { *this = *this + rhs; return *this; } inline Vec2& operator*=(const Vec2& rhs) { *this = *this * rhs; return *this; } inline Vec2& operator-=(const Vec2& rhs) { *this = *this - rhs; return *this; } inline Vec2& operator/=(const Vec2& rhs) { *this = *this / rhs; return *this; } // Vec2 with Scalar operations inline Vec2 operator+(const float s) const { return Vec2(x + s, y + s); } inline Vec2 operator-(const float s) const { return Vec2(x - s, y - s); } inline Vec2 operator*(const float s) const { return Vec2(x * s, y * s); } inline Vec2 operator/(const float s) const { return Vec2(x / s, y / s); } inline Vec2& operator+=(const float s) { *this = *this + s; return *this; } inline Vec2& operator*=(const float s) { *this = *this * s; return *this; } inline Vec2& operator-=(const float s) { *this = *this - s; return *this; } inline Vec2& operator/=(const float s) { *this = *this / s; return *this; } }; struct Vec3 { float x; float y; float z; Vec3() : x(0.0f), y(0.0f), z(0.0f) {} Vec3(float _val) : x(_val), y(_val), z(_val) {} Vec3(float _x, float _y, float _z) : x(_x), y(_y), z(_z) {} // Vec3 with Vec2 operations inline Vec3 operator+(const Vec2& rhs) const { return Vec3(x + rhs.x, y + rhs.y, z); } inline Vec3 operator-(const Vec2& rhs) const { return Vec3(x - rhs.x, y - rhs.y, z); } inline Vec3 operator*(const Vec2& rhs) const { return Vec3(x * rhs.x, y * rhs.y, z); } inline Vec3 operator/(const Vec2& rhs) const { return Vec3(x / rhs.x, y / rhs.y, z); } inline Vec3& operator+=(const Vec2& rhs) { *this = *this + rhs; return *this; } inline Vec3& operator*=(const Vec2& rhs) { *this = *this * rhs; return *this; } inline Vec3& operator-=(const Vec2& rhs) { *this = *this - rhs; return *this; } inline Vec3& operator/=(const Vec2& rhs) { *this = *this / rhs; return *this; } // Vec3 with Vec3 operations inline Vec3 operator+(const Vec3& rhs) const { return Vec3(x + rhs.x, y + rhs.y, z + rhs.z); } inline Vec3 operator-(const Vec3& rhs) const { return Vec3(x - rhs.x, y - rhs.y, z - rhs.z); } inline Vec3 operator*(const Vec3& rhs) const { return Vec3(x * rhs.x, y * rhs.y, z * rhs.z); } inline Vec3 operator/(const Vec3& rhs) const { return Vec3(x / rhs.x, y / rhs.y, z / rhs.z); } inline Vec3& operator+=(const Vec3& rhs) { *this = *this + rhs; return *this; } inline Vec3& operator*=(const Vec3& rhs) { *this = *this * rhs; return *this; } inline Vec3& operator-=(const Vec3& rhs) { *this = *this - rhs; return *this; } inline Vec3& operator/=(const Vec3& rhs) { *this = *this / rhs; return *this; } // Vec3 with Scalar operations inline Vec3 operator+(const float s) const { return Vec3(x + s, y + s, z + s); } inline Vec3 operator-(const float s) const { return Vec3(x - s, y - s, z - s); } inline Vec3 operator*(const float s) const { return Vec3(x * s, y * s, z * s); } inline Vec3 operator/(const float s) const { return Vec3(x / s, y / s, z / s); } inline Vec3& operator+=(const float s) { *this = *this + s; return *this; } inline Vec3& operator*=(const float s) { *this = *this * s; return *this; } inline Vec3& operator-=(const float s) { *this = *this - s; return *this; } inline Vec3& operator/=(const float s) { *this = *this / s; return *this; } }; struct Vec4 { float x; float y; float z; float w; Vec4() : x(0.0f), y(0.0f), z(0.0f), w(0.0f) {} Vec4(float _val) : x(_val), y(_val), z(_val), w(_val) {} Vec4(float _x, float _y, float _z, float _w) : x(_x), y(_y), z(_z), w(_w) {} // Vec4 with Vec2 operations inline Vec4 operator+(const Vec2& rhs) const { return Vec4(x + rhs.x, y + rhs.y, z, w); } inline Vec4 operator-(const Vec2& rhs) const { return Vec4(x - rhs.x, y - rhs.y, z, w); } inline Vec4 operator*(const Vec2& rhs) const { return Vec4(x * rhs.x, y * rhs.y, z, w); } inline Vec4 operator/(const Vec2& rhs) const { return Vec4(x / rhs.x, y / rhs.y, z, w); } inline Vec4& operator+=(const Vec2& rhs) { *this = *this + rhs; return *this; } inline Vec4& operator*=(const Vec2& rhs) { *this = *this * rhs; return *this; } inline Vec4& operator-=(const Vec2& rhs) { *this = *this - rhs; return *this; } inline Vec4& operator/=(const Vec2& rhs) { *this = *this / rhs; return *this; } // Vec4 with Vec3 operations inline Vec4 operator+(const Vec3& rhs) const { return Vec4(x + rhs.x, y + rhs.y, z + rhs.z, w); } inline Vec4 operator-(const Vec3& rhs) const { return Vec4(x - rhs.x, y - rhs.y, z - rhs.z, w); } inline Vec4 operator*(const Vec3& rhs) const { return Vec4(x * rhs.x, y * rhs.y, z * rhs.z, w); } inline Vec4 operator/(const Vec3& rhs) const { return Vec4(x / rhs.x, y / rhs.y, z / rhs.z, w); } inline Vec4& operator+=(const Vec3& rhs) { *this = *this + rhs; return *this; } inline Vec4& operator*=(const Vec3& rhs) { *this = *this * rhs; return *this; } inline Vec4& operator-=(const Vec3& rhs) { *this = *this - rhs; return *this; } inline Vec4& operator/=(const Vec3& rhs) { *this = *this / rhs; return *this; } // Vec4 with Vec4 operations inline Vec4 operator+(const Vec4& rhs) const { return Vec4(x + rhs.x, y + rhs.y, z + rhs.z, w + rhs.w); } inline Vec4 operator-(const Vec4& rhs) const { return Vec4(x - rhs.x, y - rhs.y, z - rhs.z, w - rhs.w); } inline Vec4 operator*(const Vec4& rhs) const { return Vec4(x * rhs.x, y * rhs.y, z * rhs.z, w * rhs.w); } inline Vec4 operator/(const Vec4& rhs) const { return Vec4(x / rhs.x, y / rhs.y, z / rhs.z, w / rhs.w); } inline Vec4& operator+=(const Vec4& rhs) { *this = *this + rhs; return *this; } inline Vec4& operator*=(const Vec4& rhs) { *this = *this * rhs; return *this; } inline Vec4& operator-=(const Vec4& rhs) { *this = *this - rhs; return *this; } inline Vec4& operator/=(const Vec4& rhs) { *this = *this / rhs; return *this; } // Vec4 with Scalar operations inline Vec4 operator+(const float s) const { return Vec4(x + s, y + s, z + s, w + s); } inline Vec4 operator-(const float s) const { return Vec4(x - s, y - s, z - s, w - s); } inline Vec4 operator*(const float s) const { return Vec4(x * s, y * s, z * s, w * s); } inline Vec4 operator/(const float s) const { return Vec4(x / s, y / s, z / s, w / s); } inline Vec4& operator+=(const float s) { *this = *this + s; return *this; } inline Vec4& operator*=(const float s) { *this = *this * s; return *this; } inline Vec4& operator-=(const float s) { *this = *this - s; return *this; } inline Vec4& operator/=(const float s) { *this = *this / s; return *this; } }; inline Vec3 operator+(const float s, const Vec3& v) { return Vec3(s + v.x, s + v.y, s + v.z); } inline Vec3 operator-(const float s, const Vec3& v) { return Vec3(s - v.x, s - v.y, s - v.z); } inline Vec3 operator*(const float s, const Vec3& v) { return Vec3(s * v.x, s * v.y, s * v.z); } inline Vec3 operator/(const float s, const Vec3& v) { return Vec3(s / v.x, s / v.y, s / v.z); } #endif // !MATHS_H