From c10e0d92f46e5eaf25a69e1fafe5f4dbd8eaab9d Mon Sep 17 00:00:00 2001 From: chai Date: Wed, 3 Nov 2021 09:52:26 +0800 Subject: *misc --- Runtime/Math/Math.h | 4 +--- Runtime/Math/MathHelper.h | 11 +++++++++++ Runtime/Math/Vector2.h | 17 +++++++++++++++++ 3 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 Runtime/Math/MathHelper.h (limited to 'Runtime/Math') diff --git a/Runtime/Math/Math.h b/Runtime/Math/Math.h index aaeb264..5e08613 100644 --- a/Runtime/Math/Math.h +++ b/Runtime/Math/Math.h @@ -6,9 +6,7 @@ #include "Matrix44.h" #include "FloatConversion.h" #include "Rect.h" - -#define max(a, b)\ -(a)>(b)?(a):(b) +#include "MathHelper.h" typedef Internal::Vector2 Vector2; typedef Internal::Vector3 Vector3; diff --git a/Runtime/Math/MathHelper.h b/Runtime/Math/MathHelper.h new file mode 100644 index 0000000..3f8754a --- /dev/null +++ b/Runtime/Math/MathHelper.h @@ -0,0 +1,11 @@ +#pragma once + +#define max(a, b)\ +(a)>(b)?(a):(b) + +#define min(a, b)\ +(a)<(b)?(a):(b) + +#define clamp(v, lo, hi)\ +max((lo), min((v), (hi))) + diff --git a/Runtime/Math/Vector2.h b/Runtime/Math/Vector2.h index 27cf312..9d7e4e9 100644 --- a/Runtime/Math/Vector2.h +++ b/Runtime/Math/Vector2.h @@ -1,4 +1,6 @@ #pragma once +#include "MathHelper.h" +#include "Runtime/Utilities/Assert.h" namespace Internal { @@ -15,6 +17,21 @@ namespace Internal this->y = y; } + Vector2 Clamp(float xmin, float xmax, float ymin, float ymax) + { + Vector2 v; + v.x = clamp(x, xmin, xmax); + v.y = clamp(y, ymin, ymax); + return v; + } + + float operator[](int i) + { + if (i == 0) return x; + else if (i == 1) return y; + Assert(false); + } + bool operator == (const Vector2& v) const { return v.x == x && v.y == y; -- cgit v1.1-26-g67d0