Small cleaning.

This commit is contained in:
James Grogan 2023-01-28 20:48:04 +00:00
parent 9e1d951520
commit 9f036d6438
8 changed files with 94 additions and 34 deletions

View file

@ -1,17 +1,49 @@
#include "Vector.h"
#include <cmath>
#include <stdexcept>
Vector::Vector(Primitive p)
{
switch(p)
{
case Primitive::UNIT_X:
mX = 1.0;
break;
case Primitive::UNIT_Y:
mY = 1.0;
break;
case Primitive::UNIT_Z:
mZ = 1.0;
break;
case Primitive::NEGATIVE_X:
mX = -1.0;
break;
case Primitive::NEGATIVE_Y:
mY = -1.0;
break;
case Primitive::NEGATIVE_Z:
mZ = -1.0;
break;
}
}
Vector::Vector(double x, double y, double z)
: mX(x),
mY(y),
mZ(z)
{
updateLength();
}
Vector::~Vector()
{
};
void Vector::reverseDirection()
{
(*this) *= -1.0;
}
double Vector::getX() const
{
return mX;
@ -29,7 +61,7 @@ double Vector::getZ() const
double Vector::getLength() const
{
return mLength;
return std::sqrt(mX * mX + mY * mY + mZ * mZ);
}
double Vector::dotProduct(const Vector& v) const
@ -44,7 +76,12 @@ Vector Vector::crossProduct(const Vector& v) const
Vector Vector::getNormalized() const
{
return Vector(mX / mLength, mY / mLength, mZ / mLength);
const auto length = getLength();
if (length == 0.0)
{
throw std::logic_error("Divide by zero getting normalizing vector");
}
return Vector(mX / length, mY / length, mZ / length);
}
void Vector::scale(double x, double y, double z)
@ -52,10 +89,4 @@ void Vector::scale(double x, double y, double z)
mX = x * mX;
mY = y * mY;
mZ = z * mZ;
updateLength();
}
void Vector::updateLength()
{
mLength = std::sqrt(mX * mX + mY * mY + mZ * mZ);
}