From bb674cdbb69f2dedecf52dfcd52f6de6e754e8b7 Mon Sep 17 00:00:00 2001 From: trotFunky Date: Mon, 14 Oct 2019 18:46:32 +0200 Subject: [PATCH] Added some operators to the vector classes --- src/types.h | 122 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) diff --git a/src/types.h b/src/types.h index 6c3349e..1e69e15 100644 --- a/src/types.h +++ b/src/types.h @@ -15,6 +15,48 @@ struct CoordinatesVector { T coordinates[N]; + // Use SFINAE to declare the function if and only if the type is arithmetic + template + typename std::enable_if::value,CoordinatesVector>::type + operator*(const Scalar& scalar) + { + CoordinatesVector result; + for (unsigned int i = 0;i& op) + { + CoordinatesVector result; + for (unsigned int i = 0;i& op) + { + CoordinatesVector result; + for (unsigned int i = 0;i& original) + { + if (this != &original) + { + std::copy(std::begin(original.coordinates), std::end(original.coordinates), std::begin(coordinates)); + } + return *this; + } + friend std::fstream& operator>>(std::fstream& stream, CoordinatesVector& vector) { for (unsigned int i = 0;i T& x = coordinates[0]; T& y = coordinates[1]; + // Use SFINAE to declare the function if and only if the type is a scalar + template + typename std::enable_if::value,CoordinatesVector>::type + operator*(const Scalar& scalar) + { + CoordinatesVector result; + for (unsigned int i = 0;i<2;i++) + { + result.coordinates[i] = coordinates[i] * scalar; + } + } + + CoordinatesVector operator+(const CoordinatesVector& op) + { + CoordinatesVector result; + result.x = x + op.x; + result.y = y + op.y; + + return result; + } + + CoordinatesVector operator-(const CoordinatesVector& op) + { + CoordinatesVector result; + result.x = x - op.x; + result.y = y - op.y; + + return result; + } + + CoordinatesVector& operator=(const CoordinatesVector& original) + { + if (this != &original) + { + std::copy(std::begin(original.coordinates), std::end(original.coordinates), std::begin(coordinates)); + } + return *this; + } + friend std::fstream& operator>>(std::fstream& stream, CoordinatesVector& vec2) { stream >> vec2.x >> vec2.y; @@ -49,6 +130,47 @@ struct CoordinatesVector T& y = coordinates[1]; T& z = coordinates[2]; + // Use SFINAE to declare the function if and only if the type is a scalar + template + typename std::enable_if::value,CoordinatesVector>::type + operator*(const Scalar& scalar) + { + CoordinatesVector result; + for (unsigned int i = 0;i<3;i++) + { + result.coordinates[i] = coordinates[i] * scalar; + } + } + + CoordinatesVector operator+(const CoordinatesVector& op) + { + CoordinatesVector result; + result.x = x + op.x; + result.y = y + op.y; + result.z = z + op.z; + + return result; + } + + CoordinatesVector operator-(const CoordinatesVector& op) + { + CoordinatesVector result; + result.x = x - op.x; + result.y = y - op.y; + result.z = z - op.z; + + return result; + } + + CoordinatesVector& operator=(const CoordinatesVector& original) + { + if (this != &original) + { + std::copy(std::begin(original.coordinates), std::end(original.coordinates), std::begin(coordinates)); + } + return *this; + } + friend std::fstream& operator>>(std::fstream& stream, CoordinatesVector& vec3) { stream >> vec3.x >> vec3.y >> vec3.z;