From f9dc00712da03bd31943bc3e09c0a8483a29a414 Mon Sep 17 00:00:00 2001 From: trotFunky Date: Tue, 22 Oct 2019 01:11:01 +0200 Subject: [PATCH] Added cross and dot products Fixed T initializers Constified functions that could be marked const. --- src/Vectors.h | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/src/Vectors.h b/src/Vectors.h index 2991d92..29bdf82 100644 --- a/src/Vectors.h +++ b/src/Vectors.h @@ -18,7 +18,7 @@ struct CoordinatesVector /// Computes the euclidean norm of the vector. /// \return The magnitude. - double magnitude() + double magnitude() const { // TODO : Save magnitude if the vector has not been modified. double sum_of_squares = 0; @@ -44,6 +44,19 @@ struct CoordinatesVector } } + + template + typename std::enable_if::value,T>::type + dot_product(const CoordinatesVector& operand) const + { + T result = 0; + for (unsigned int i = 0;i typename std::enable_if::value,CoordinatesVector>::type @@ -106,6 +119,12 @@ struct Vec2 : public CoordinatesVector Vec2() = default; + Vec2(T x_axis, T y_axis) + { + x = x_axis; + y = y_axis; + } + Vec2(const Vec2& original): x(CoordinatesVector::coordinates[0]), y(CoordinatesVector::coordinates[1]) { std::copy(std::begin(original.coordinates), std::end(original.coordinates), std::begin(CoordinatesVector::coordinates)); @@ -140,6 +159,13 @@ struct Vec3 : CoordinatesVector Vec3() = default; + Vec3(T x_axis, T y_axis, T z_axis) + { + x = x_axis; + y = y_axis; + z = z_axis; + } + Vec3(const Vec3& original): x(CoordinatesVector::coordinates[0]), y(CoordinatesVector::coordinates[1]), z(CoordinatesVector::coordinates[2]) { std::copy(std::begin(original.coordinates), std::end(original.coordinates), std::begin(CoordinatesVector::coordinates)); @@ -154,6 +180,18 @@ struct Vec3 : CoordinatesVector } } + template + typename std::enable_if::value,Vec3>::type + cross_product(const Vec3& operand) const + { + Vec3 normal_vector{0,0,0}; + normal_vector.x = y*operand.z - z*operand.y; + normal_vector.y = z*operand.x - x*operand.z; + normal_vector.z = x*operand.y - y*operand.x; + + return normal_vector; + } + Vec3& operator=(const Vec3& original) { if (this != &original)