From 2c03bac32619d39c80e4d7a33515868386f7c173 Mon Sep 17 00:00:00 2001 From: CodeMyst Date: Sat, 27 Jun 2020 18:54:21 +0200 Subject: [PATCH] more vector operators * allowing to init vectors using any numeric types --- source/cosmomyst/math/vector.d | 69 +++++++++++++++++++++++++++++----- 1 file changed, 59 insertions(+), 10 deletions(-) diff --git a/source/cosmomyst/math/vector.d b/source/cosmomyst/math/vector.d index 86a28bc..332a4e2 100644 --- a/source/cosmomyst/math/vector.d +++ b/source/cosmomyst/math/vector.d @@ -34,8 +34,10 @@ struct vec(ulong n) if (n >= 1) { } @nogc this(T...)(T args) pure nothrow { + import std.traits : isNumeric; + static foreach (arg; args) { - static assert(is(typeof(arg) == float) || is(typeof(arg) == const(float)), "all values must be of type float"); + static assert(isNumeric!(typeof(arg)), "all values must be numeric"); } static assert(args.length > 0, "no args provided"); @@ -115,6 +117,11 @@ struct vec(ulong n) if (n >= 1) { return res; } + @nogc void opOpAssign(string s) (const float scalar) pure notrhow if (s == "*") { + auto res = this * scalar; + this.v = res.v; + } + /++ + returns the sum of this + scalar +/ @@ -126,39 +133,75 @@ struct vec(ulong n) if (n >= 1) { return res; } + @nogc void opOpAssign(string s) (const float scalar) const if (s == "+") { + auto res = this + scalar; + this.v = res.v; + } + /++ - + returns the sum of 2 vectors + + returns the sub of this - scalar +/ - @nogc vec!n opBinary(string s) (const vec!n other) const if (s == "+") { + @nogc vec!n opBinary(string s) (const float scalar) const if (s == "-") { vec!n res; for (int i = 0; i < n; i++) { - res.v[i] = v[i] + other.v[i]; + res[i] = this[i] - scalar; } return res; } + @nogc void opOpAssign(string s) (const float scalar) const if (s == "-") { + auto res = this - scalar; + this.v = res.v; + } + /++ - + returns the sub of 2 vectors. + + returns the div of this / scalar. +/ - @nogc vec!n opBinary(string s) (const vec!n other) const if (s == "-") { + @nogc vec!n opBinary(string s) (in float scalar) const if (s == "/") { vec!n res; for (int i = 0; i < n; i++) { - res.v[i] = v[i] - other.v[i]; + res.v[i] = v[i] / scalar; } return res; } + @nogc void opOpAssign(string s) (in float scalar) const if (s == "/") { + auto res = this / scalar; + this.v = res.v; + } + /++ - + returns the div of this / scalar. + + returns the sum of 2 vectors +/ - @nogc vec!n opBinary(string s) (in float scalar) const if (s == "/") { + @nogc vec!n opBinary(string s) (const vec!n other) const if (s == "+") { vec!n res; for (int i = 0; i < n; i++) { - res.v[i] = v[i] / scalar; + res.v[i] = v[i] + other.v[i]; } return res; } + @nogc void opOpAssign(string s) (const vec!n other) const if (s == "+") { + auto res = this + other; + this.v = res.v; + } + + /++ + + returns the sub of 2 vectors. + +/ + @nogc vec!n opBinary(string s) (const vec!n other) const if (s == "-") { + vec!n res; + for (int i = 0; i < n; i++) { + res.v[i] = v[i] - other.v[i]; + } + return res; + } + + @nogc void opOpAssign(string s) (const vec!n other) const if (s == "-") { + auto res = this - other; + this.v = res.v; + } + /++ + get the nth component +/ @@ -208,6 +251,12 @@ unittest { assert(t1.toString() == "[2, 3]"); } +unittest { + auto t1 = vec2(2, 3); + + assert(t1 * 2 == vec2(4, 6)); +} + unittest { auto t1 = vec3(5f, 2f, 6f); assert(t1.length() == 8.06225774829855f);