Added cross and dot products
Fixed T initializers Constified functions that could be marked const.
This commit is contained in:
parent
18bad8389a
commit
f9dc00712d
1 changed files with 39 additions and 1 deletions
|
@ -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 U = T>
|
||||
typename std::enable_if<std::is_arithmetic<U>::value,T>::type
|
||||
dot_product(const CoordinatesVector<T,N>& operand) const
|
||||
{
|
||||
T result = 0;
|
||||
for (unsigned int i = 0;i<N;i++)
|
||||
{
|
||||
result += coordinates[i]*operand.coordinates[i];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// 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
|
||||
|
@ -106,6 +119,12 @@ struct Vec2 : public CoordinatesVector<T,2>
|
|||
|
||||
Vec2() = default;
|
||||
|
||||
Vec2(T x_axis, T y_axis)
|
||||
{
|
||||
x = x_axis;
|
||||
y = y_axis;
|
||||
}
|
||||
|
||||
Vec2(const Vec2<T>& original): x(CoordinatesVector<T,2>::coordinates[0]), y(CoordinatesVector<T,2>::coordinates[1])
|
||||
{
|
||||
std::copy(std::begin(original.coordinates), std::end(original.coordinates), std::begin(CoordinatesVector<T,2>::coordinates));
|
||||
|
@ -140,6 +159,13 @@ struct Vec3 : CoordinatesVector<T,3>
|
|||
|
||||
Vec3() = default;
|
||||
|
||||
Vec3(T x_axis, T y_axis, T z_axis)
|
||||
{
|
||||
x = x_axis;
|
||||
y = y_axis;
|
||||
z = z_axis;
|
||||
}
|
||||
|
||||
Vec3(const Vec3<T>& original): x(CoordinatesVector<T,3>::coordinates[0]), y(CoordinatesVector<T,3>::coordinates[1]), z(CoordinatesVector<T,3>::coordinates[2])
|
||||
{
|
||||
std::copy(std::begin(original.coordinates), std::end(original.coordinates), std::begin(CoordinatesVector<T,3>::coordinates));
|
||||
|
@ -154,6 +180,18 @@ struct Vec3 : CoordinatesVector<T,3>
|
|||
}
|
||||
}
|
||||
|
||||
template <typename U = T>
|
||||
typename std::enable_if<std::is_arithmetic<U>::value,Vec3<T>>::type
|
||||
cross_product(const Vec3<T>& operand) const
|
||||
{
|
||||
Vec3<T> 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<T>& original)
|
||||
{
|
||||
if (this != &original)
|
||||
|
|
Loading…
Add table
Reference in a new issue