aboutsummaryrefslogtreecommitdiff
path: root/Client/Source/Phy2D/Common/Math.h
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2021-11-30 22:25:37 +0800
committerchai <chaifix@163.com>2021-11-30 22:25:37 +0800
commit9e0e01b7f4375063f06e494113187d48614628e0 (patch)
tree21a4901612ad92c121f4c887a33b1bbbe87c6b00 /Client/Source/Phy2D/Common/Math.h
+init
Diffstat (limited to 'Client/Source/Phy2D/Common/Math.h')
-rw-r--r--Client/Source/Phy2D/Common/Math.h191
1 files changed, 191 insertions, 0 deletions
diff --git a/Client/Source/Phy2D/Common/Math.h b/Client/Source/Phy2D/Common/Math.h
new file mode 100644
index 0000000..9c0ff8f
--- /dev/null
+++ b/Client/Source/Phy2D/Common/Math.h
@@ -0,0 +1,191 @@
+#pragma once
+
+#include <math.h>
+#include <assert.h>
+#include <stdlib.h>
+#include <string>
+
+#include "Settings.h"
+
+namespace Phy2D
+{
+
+ struct Vec2
+ {
+ Vec2() {}
+ Vec2(number x, number y) : x(x), y(y) {}
+
+ void Set(number x_, number y_) { x = x_; y = y_; }
+
+ Vec2 operator -() { return Vec2(-x, -y); }
+
+ void operator += (const Vec2& v)
+ {
+ x += v.x; y += v.y;
+ }
+
+ void operator -= (const Vec2& v)
+ {
+ x -= v.x; y -= v.y;
+ }
+
+ void operator *= (number a)
+ {
+ x *= a; y *= a;
+ }
+
+ number Length() const
+ {
+ return SQRT(x * x + y * y);
+ }
+
+ std::string ToString()
+ {
+ return std::to_string((float)x) + "," + std::to_string((float)y);
+ }
+
+ number x, y;
+ };
+
+ struct Mat22
+ {
+ Mat22() {}
+ Mat22(number angle)
+ {
+ number c = COS(angle), s = SIN(angle);
+ col1.x = c; col2.x = -s;
+ col1.y = s; col2.y = c;
+ }
+
+ Mat22(const Vec2& col1, const Vec2& col2) : col1(col1), col2(col2) {}
+
+ Mat22 Transpose() const
+ {
+ return Mat22(Vec2(col1.x, col2.x), Vec2(col1.y, col2.y));
+ }
+
+ Mat22 Invert() const
+ {
+ number a = col1.x, b = col2.x, c = col1.y, d = col2.y;
+ Mat22 B;
+ number det = a * d - b * c;
+ assert(det != 0.0f);
+ det = (number)1.0f / det;
+ B.col1.x = det * d; B.col2.x = -det * b;
+ B.col1.y = -det * c; B.col2.y = det * a;
+ return B;
+ }
+
+ Vec2 col1, col2;
+ };
+
+ inline number Dot(const Vec2& a, const Vec2& b)
+ {
+ return a.x * b.x + a.y * b.y;
+ }
+
+ inline number Cross(const Vec2& a, const Vec2& b)
+ {
+ return a.x * b.y - a.y * b.x;
+ }
+
+ inline Vec2 Cross(const Vec2& a, number s)
+ {
+ return Vec2(s * a.y, -s * a.x);
+ }
+
+ inline Vec2 Cross(number s, const Vec2& a)
+ {
+ return Vec2(-s * a.y, s * a.x);
+ }
+
+ inline Vec2 operator * (const Mat22& A, const Vec2& v)
+ {
+ return Vec2(A.col1.x * v.x + A.col2.x * v.y, A.col1.y * v.x + A.col2.y * v.y);
+ }
+
+ inline Vec2 operator + (const Vec2& a, const Vec2& b)
+ {
+ return Vec2(a.x + b.x, a.y + b.y);
+ }
+
+ inline Vec2 operator - (const Vec2& a, const Vec2& b)
+ {
+ return Vec2(a.x - b.x, a.y - b.y);
+ }
+
+ inline Vec2 operator * (number s, const Vec2& v)
+ {
+ return Vec2(s * v.x, s * v.y);
+ }
+
+ inline Mat22 operator + (const Mat22& A, const Mat22& B)
+ {
+ return Mat22(A.col1 + B.col1, A.col2 + B.col2);
+ }
+
+ inline Mat22 operator * (const Mat22& A, const Mat22& B)
+ {
+ return Mat22(A * B.col1, A * B.col2);
+ }
+
+ inline number Abs(number a)
+ {
+ return a > 0.0f ? a : -a;
+ }
+
+ inline Vec2 Abs(const Vec2& a)
+ {
+ return Vec2(fabsf(a.x), fabsf(a.y));
+ }
+
+ inline Mat22 Abs(const Mat22& A)
+ {
+ return Mat22(Abs(A.col1), Abs(A.col2));
+ }
+
+ inline number Sign(number x)
+ {
+ return (float) x < 0.0f ? -1.0f : 1.0f;
+ }
+
+ inline number Min(number a, number b)
+ {
+ return a < b ? a : b;
+ }
+
+ inline number Max(number a, number b)
+ {
+ return a > b ? a : b;
+ }
+
+ inline number Clamp(number a, number low, number high)
+ {
+ return Max(low, Min(a, high));
+ }
+
+ template<typename T> inline void Swap(T& a, T& b)
+ {
+ T tmp = a;
+ a = b;
+ b = tmp;
+ }
+
+ //// Random number in range [-1,1]
+ //inline number Random()
+ //{
+ // number r = (number)rand();
+ // r /= RAND_MAX;
+ // r = (number)2.0f * r - 1.0f;
+ // return r;
+ //}
+
+ //inline number Random(number lo, number hi)
+ //{
+ // number r = (number)rand();
+ // r /= RAND_MAX;
+ // r = (hi - lo) * r + lo;
+ // return r;
+ //}
+
+}