166 lines
3.5 KiB
C++
Executable File
166 lines
3.5 KiB
C++
Executable File
#include "Vector3d.h"
|
|
#include <ostream>
|
|
|
|
const Vector3d Vector3d::Zero { 0.0, 0.0, 0.0 };
|
|
const Vector3d Vector3d::One { 1.0, 1.0, 1.0 };
|
|
const Vector3d Vector3d::Forward { 0.0, 0.0, 1.0 };
|
|
const Vector3d Vector3d::Backward { 0.0, 0.0, -1.0 };
|
|
const Vector3d Vector3d::Up { 0.0, 1.0, 0.0 };
|
|
const Vector3d Vector3d::Down { 0.0, -1.0, 0.0 };
|
|
const Vector3d Vector3d::Left { -1.0, 0.0, 0.0 };
|
|
const Vector3d Vector3d::Right { 1.0, 0.0, 0.0 };
|
|
const Vector3d Vector3d::X { 1.0, 0.0, 0.0 };
|
|
const Vector3d Vector3d::Y { 0.0, 1.0, 0.0 };
|
|
const Vector3d Vector3d::Z { 0.0, 0.0, 1.0 };
|
|
|
|
Vector3d::Vector3d() noexcept
|
|
: x(0.0), y(0.0), z(0.0)
|
|
{
|
|
}
|
|
|
|
Vector3d::Vector3d(double x, double y, double z) noexcept
|
|
: x(x), y(y), z(z)
|
|
{
|
|
}
|
|
|
|
Vector3d& Vector3d::operator=(const Vector3d& other) noexcept
|
|
{
|
|
if (this != &other)
|
|
{
|
|
x = other.x;
|
|
y = other.y;
|
|
z = other.z;
|
|
}
|
|
return *this;
|
|
}
|
|
|
|
Vector3d& Vector3d::operator=(Vector3d&& other) noexcept
|
|
{
|
|
if (this != &other)
|
|
{
|
|
x = other.x;
|
|
y = other.y;
|
|
z = other.z;
|
|
}
|
|
return *this;
|
|
}
|
|
|
|
Vector3d Vector3d::operator+(const Vector3d& rhs) const noexcept
|
|
{
|
|
return { x + rhs.x, y + rhs.y, z + rhs.z };
|
|
}
|
|
|
|
Vector3d Vector3d::operator-(const Vector3d& rhs) const noexcept
|
|
{
|
|
return { x - rhs.x, y - rhs.y, z - rhs.z };
|
|
}
|
|
|
|
Vector3d Vector3d::operator*(double scalar) const noexcept
|
|
{
|
|
return { x * scalar, y * scalar, z * scalar };
|
|
}
|
|
|
|
Vector3d Vector3d::operator/(double scalar) const noexcept
|
|
{
|
|
return { x / scalar, y / scalar, z / scalar };
|
|
}
|
|
|
|
Vector3d& Vector3d::operator+=(const Vector3d& rhs) noexcept
|
|
{
|
|
x += rhs.x;
|
|
y += rhs.y;
|
|
z += rhs.z;
|
|
return *this;
|
|
}
|
|
|
|
Vector3d& Vector3d::operator-=(const Vector3d& rhs) noexcept
|
|
{
|
|
x -= rhs.x;
|
|
y -= rhs.y;
|
|
z -= rhs.z;
|
|
return *this;
|
|
}
|
|
|
|
Vector3d& Vector3d::operator*=(double scalar) noexcept
|
|
{
|
|
x *= scalar;
|
|
y *= scalar;
|
|
z *= scalar;
|
|
return *this;
|
|
}
|
|
|
|
Vector3d& Vector3d::operator/=(double scalar) noexcept
|
|
{
|
|
x /= scalar;
|
|
y /= scalar;
|
|
z /= scalar;
|
|
return *this;
|
|
}
|
|
|
|
bool Vector3d::operator==(const Vector3d& rhs) const noexcept
|
|
{
|
|
return x == rhs.x && y == rhs.y && z == rhs.z;
|
|
}
|
|
|
|
bool Vector3d::operator!=(const Vector3d& rhs) const noexcept
|
|
{
|
|
return !(*this == rhs);
|
|
}
|
|
|
|
std::ostream& operator<<(std::ostream& os, const Vector3d& v)
|
|
{
|
|
os << "Vector3d(" << v.x << ", " << v.y << ", " << v.z << ")";
|
|
return os;
|
|
}
|
|
|
|
double Vector3d::dot(const Vector3d& rhs) const noexcept
|
|
{
|
|
return x * rhs.x + y * rhs.y + z * rhs.z;
|
|
}
|
|
|
|
double Vector3d::magnitude() const noexcept
|
|
{
|
|
return std::sqrt(x * x + y * y + z * z);
|
|
}
|
|
|
|
double Vector3d::magnitudeSquared() const noexcept
|
|
{
|
|
return x * x + y * y + z * z;
|
|
}
|
|
|
|
Vector3d Vector3d::cross(const Vector3d& rhs) const noexcept
|
|
{
|
|
return {
|
|
y * rhs.z - z * rhs.y,
|
|
z * rhs.x - x * rhs.z,
|
|
x * rhs.y - y * rhs.x
|
|
};
|
|
}
|
|
|
|
Vector3d Vector3d::normalized() const noexcept
|
|
{
|
|
double m = magnitude();
|
|
if (m == 0.0)
|
|
return Vector3d::Zero;
|
|
|
|
double inv = 1.0 / m;
|
|
return { x * inv, y * inv, z * inv };
|
|
}
|
|
|
|
void Vector3d::normalize() noexcept
|
|
{
|
|
double m = magnitude();
|
|
if (m == 0.0)
|
|
return;
|
|
|
|
double inv = 1.0 / m;
|
|
x *= inv;
|
|
y *= inv;
|
|
z *= inv;
|
|
}
|
|
|
|
Vector3d Vector3d::operator-() const noexcept
|
|
{
|
|
return Vector3d(-x, -y, -z);
|
|
}
|