From 0d3bff5e006cfaa358b51e3a6d11984e3782a90e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beat=20K=C3=BCng?= Date: Mon, 1 Oct 2018 09:16:45 +0200 Subject: [PATCH] Vector2: add explicit constructor for Vector3 Initialize from the first 2 elements. --- matrix/Vector2.hpp | 8 ++++++++ test/vector2.cpp | 5 +++++ 2 files changed, 13 insertions(+) diff --git a/matrix/Vector2.hpp b/matrix/Vector2.hpp index f6577e255e..dc42c5d5e9 100644 --- a/matrix/Vector2.hpp +++ b/matrix/Vector2.hpp @@ -22,6 +22,7 @@ class Vector2 : public Vector public: typedef Matrix Matrix21; + typedef Vector Vector3; Vector2() = default; @@ -42,6 +43,13 @@ public: v(1) = y; } + explicit Vector2(const Vector3 & other) + { + Vector2 &v(*this); + v(0) = other(0); + v(1) = other(1); + } + Type cross(const Matrix21 & b) const { const Vector2 &a(*this); return a(0)*b(1, 0) - a(1)*b(0, 0); diff --git a/test/vector2.cpp b/test/vector2.cpp index a8838d9b4e..f46f59fe29 100644 --- a/test/vector2.cpp +++ b/test/vector2.cpp @@ -29,6 +29,11 @@ int main() TEST(fabs(f(0) - 4) < 1e-5); TEST(fabs(f(1) - 5) < 1e-5); + Vector3f g(1.23f, 423.4f, 3221.f); + Vector2f h(g); + TEST(fabs(h(0) - 1.23f) < 1e-5); + TEST(fabs(h(1) - 423.4f) < 1e-5); + return 0; }