From 8143b73ffbe84ee1fa0df99dd98c2b569bdd2f9c Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Sun, 24 Feb 2013 23:03:12 +0000 Subject: [PATCH] math: add Box2d + vec2 operations for easy box translations. --- src/lol/math/geometry.h | 48 +++++++++++++++++++++++++++++++++++++++++ test/unit/box.cpp | 15 +++++++++++++ 2 files changed, 63 insertions(+) diff --git a/src/lol/math/geometry.h b/src/lol/math/geometry.h index 0f6c7363..f00a8887 100644 --- a/src/lol/math/geometry.h +++ b/src/lol/math/geometry.h @@ -40,6 +40,30 @@ struct Box2D B(b) {} + Box2D operator +(vec2 v) + { + return Box2D(A + v, B + v); + } + + Box2D &operator +=(vec2 v) + { + A += v; + B += v; + return *this; + } + + Box2D operator -(vec2 v) + { + return Box2D(A - v, B - v); + } + + Box2D &operator -=(vec2 v) + { + A -= v; + B -= v; + return *this; + } + vec2 A, B; }; @@ -55,6 +79,30 @@ struct Box3D B(b) {} + Box3D operator +(vec3 v) + { + return Box3D(A + v, B + v); + } + + Box3D &operator +=(vec3 v) + { + A += v; + B += v; + return *this; + } + + Box3D operator -(vec3 v) + { + return Box3D(A - v, B - v); + } + + Box3D &operator -=(vec3 v) + { + A -= v; + B -= v; + return *this; + } + vec3 A, B; }; diff --git a/test/unit/box.cpp b/test/unit/box.cpp index 699a872b..e2758080 100644 --- a/test/unit/box.cpp +++ b/test/unit/box.cpp @@ -38,6 +38,21 @@ LOLUNIT_FIXTURE(BoxTest) LOLUNIT_ASSERT_EQUAL(false, BoxIsectBox(b4, b5)); } + + LOLUNIT_TEST(Box2DMove) + { + Box2D b1(vec2(0.f, 0.f), vec2(1.f, 1.f)); + Box2D b2(vec2(2.f, 2.f), vec2(3.f, 3.f)); + + b1 += vec2(0.6f, 0.6f); + LOLUNIT_ASSERT_EQUAL(false, BoxIsectBox(b1, b2)); + + b1 += vec2(0.6f, 0.6f); + LOLUNIT_ASSERT_EQUAL(true, BoxIsectBox(b1, b2)); + + b1 -= vec2(0.0f, 0.6f); + LOLUNIT_ASSERT_EQUAL(false, BoxIsectBox(b1, b2)); + } }; } /* namespace lol */