generated from karl/cpp-template
Implement Vector dot and cross product
A new Vector constructor was added for ease of use.
This commit is contained in:
parent
a1bae6dd5b
commit
6cb085dcd6
16
geometry.h
16
geometry.h
@ -23,32 +23,30 @@ struct Triangle {
|
||||
|
||||
struct Vector {
|
||||
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]
|
||||
float operator[](int i) const { return c[i]; }
|
||||
float &operator[](int i) { return c[i]; }
|
||||
|
||||
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 {
|
||||
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 {
|
||||
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) {
|
||||
// TODO
|
||||
return other;
|
||||
return Vector(c[2] * other[3] - c[3] - other[2], c[3] * other[1] - c[1] * other[3],
|
||||
c[1] * other[2] - c[2] * other[1]);
|
||||
}
|
||||
|
||||
float dot(const Vector &other) {
|
||||
// TODO
|
||||
return 0.0;
|
||||
}
|
||||
float dot(const Vector &other) { return c[1] * other[1] + c[2] * other[2] + c[3] * other[3]; }
|
||||
|
||||
float *c;
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user