Skip to content

Commit

Permalink
more vector operators
Browse files Browse the repository at this point in the history
* allowing to init vectors using any numeric types
  • Loading branch information
CodeMyst committed Jun 27, 2020
1 parent e414e2d commit 2c03bac
Showing 1 changed file with 59 additions and 10 deletions.
69 changes: 59 additions & 10 deletions source/cosmomyst/math/vector.d
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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
+/
Expand All @@ -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
+/
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 2c03bac

Please sign in to comment.