Added some operators to the vector classes
This commit is contained in:
parent
38cc784ec8
commit
bb674cdbb6
1 changed files with 122 additions and 0 deletions
122
src/types.h
122
src/types.h
|
@ -15,6 +15,48 @@ struct CoordinatesVector
|
||||||
{
|
{
|
||||||
T coordinates[N];
|
T coordinates[N];
|
||||||
|
|
||||||
|
// Use SFINAE to declare the function if and only if the type is arithmetic
|
||||||
|
template <typename Scalar>
|
||||||
|
typename std::enable_if<std::is_arithmetic<Scalar>::value,CoordinatesVector<T,N>>::type
|
||||||
|
operator*(const Scalar& scalar)
|
||||||
|
{
|
||||||
|
CoordinatesVector result;
|
||||||
|
for (unsigned int i = 0;i<N;i++)
|
||||||
|
{
|
||||||
|
result.coordinates[i] = coordinates[i] * scalar;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
CoordinatesVector operator+(const CoordinatesVector<T,N>& op)
|
||||||
|
{
|
||||||
|
CoordinatesVector result;
|
||||||
|
for (unsigned int i = 0;i<N;i++)
|
||||||
|
{
|
||||||
|
result.coordinates[i] = coordinates[i] + op.coordinates[i];
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
CoordinatesVector operator-(const CoordinatesVector<T,N>& op)
|
||||||
|
{
|
||||||
|
CoordinatesVector result;
|
||||||
|
for (unsigned int i = 0;i<N;i++)
|
||||||
|
{
|
||||||
|
result.coordinates[i] = coordinates[i] - op.coordinates[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
CoordinatesVector& operator=(const CoordinatesVector<T,N>& 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<T,N>& vector)
|
friend std::fstream& operator>>(std::fstream& stream, CoordinatesVector<T,N>& vector)
|
||||||
{
|
{
|
||||||
for (unsigned int i = 0;i<N;i++)
|
for (unsigned int i = 0;i<N;i++)
|
||||||
|
@ -33,6 +75,45 @@ struct CoordinatesVector<T,2>
|
||||||
T& x = coordinates[0];
|
T& x = coordinates[0];
|
||||||
T& y = coordinates[1];
|
T& y = coordinates[1];
|
||||||
|
|
||||||
|
// Use SFINAE to declare the function if and only if the type is a scalar
|
||||||
|
template <typename Scalar>
|
||||||
|
typename std::enable_if<std::is_arithmetic<Scalar>::value,CoordinatesVector<T,2>>::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<T,2>& op)
|
||||||
|
{
|
||||||
|
CoordinatesVector result;
|
||||||
|
result.x = x + op.x;
|
||||||
|
result.y = y + op.y;
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
CoordinatesVector operator-(const CoordinatesVector<T,2>& op)
|
||||||
|
{
|
||||||
|
CoordinatesVector<T,2> result;
|
||||||
|
result.x = x - op.x;
|
||||||
|
result.y = y - op.y;
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
CoordinatesVector& operator=(const CoordinatesVector<T,2>& 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<T,2>& vec2)
|
friend std::fstream& operator>>(std::fstream& stream, CoordinatesVector<T,2>& vec2)
|
||||||
{
|
{
|
||||||
stream >> vec2.x >> vec2.y;
|
stream >> vec2.x >> vec2.y;
|
||||||
|
@ -49,6 +130,47 @@ struct CoordinatesVector<T,3>
|
||||||
T& y = coordinates[1];
|
T& y = coordinates[1];
|
||||||
T& z = coordinates[2];
|
T& z = coordinates[2];
|
||||||
|
|
||||||
|
// Use SFINAE to declare the function if and only if the type is a scalar
|
||||||
|
template <typename Scalar>
|
||||||
|
typename std::enable_if<std::is_arithmetic<Scalar>::value,CoordinatesVector<T,3>>::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<T,3>& op)
|
||||||
|
{
|
||||||
|
CoordinatesVector result;
|
||||||
|
result.x = x + op.x;
|
||||||
|
result.y = y + op.y;
|
||||||
|
result.z = z + op.z;
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
CoordinatesVector operator-(const CoordinatesVector<T,3>& op)
|
||||||
|
{
|
||||||
|
CoordinatesVector<T,3> result;
|
||||||
|
result.x = x - op.x;
|
||||||
|
result.y = y - op.y;
|
||||||
|
result.z = z - op.z;
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
CoordinatesVector& operator=(const CoordinatesVector<T,3>& 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<T,3>& vec3)
|
friend std::fstream& operator>>(std::fstream& stream, CoordinatesVector<T,3>& vec3)
|
||||||
{
|
{
|
||||||
stream >> vec3.x >> vec3.y >> vec3.z;
|
stream >> vec3.x >> vec3.y >> vec3.z;
|
||||||
|
|
Loading…
Add table
Reference in a new issue