Implement Vector dot and cross product

A new Vector constructor was added for ease of use.
This commit is contained in:
karl 2020-12-28 17:15:47 +01:00
parent a1bae6dd5b
commit 6cb085dcd6

View File

@ -23,32 +23,30 @@ struct Triangle {
struct Vector { struct Vector {
Vector(float coordinates[3]) : c(coordinates) {} Vector(float coordinates[3]) : c(coordinates) {}
Vector(float x, float y, float z) : c(new float[3]{x, y, z}) {}
// Avoid having to write vector.c[index], instead allow vector[index] // Avoid having to write vector.c[index], instead allow vector[index]
float operator[](int i) const { return c[i]; } float operator[](int i) const { return c[i]; }
float &operator[](int i) { return c[i]; } float &operator[](int i) { return c[i]; }
Vector operator+(const Vector &other) const { Vector operator+(const Vector &other) const {
return Vector(new float[3]{c[0] + other.c[0], c[1] + other.c[1], c[2] + other.c[2]}); return Vector(c[0] + other.c[0], c[1] + other.c[1], c[2] + other.c[2]);
} }
Vector operator-(const Vector &other) const { Vector operator-(const Vector &other) const {
return Vector(new float[3]{c[0] - other.c[0], c[1] - other.c[1], c[2] - other.c[2]}); return Vector(c[0] - other.c[0], c[1] - other.c[1], c[2] - other.c[2]);
} }
Vector operator*(float scalar) const { Vector operator*(float scalar) const {
return Vector(new float[3]{c[0] * scalar, c[1] * scalar, c[2] * scalar}); return Vector(c[0] * scalar, c[1] * scalar, c[2] * scalar);
} }
Vector cross(const Vector &other) { Vector cross(const Vector &other) {
// TODO return Vector(c[2] * other[3] - c[3] - other[2], c[3] * other[1] - c[1] * other[3],
return other; c[1] * other[2] - c[2] * other[1]);
} }
float dot(const Vector &other) { float dot(const Vector &other) { return c[1] * other[1] + c[2] * other[2] + c[3] * other[3]; }
// TODO
return 0.0;
}
float *c; float *c;
}; };