Created
August 7, 2021 02:14
-
-
Save ryutorion/9bf38f8f60bb45b18e361c8444b6946e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef VECTOR3_H_INCLUDED | |
#define VECTOR3_H_INCLUDED | |
#include <cmath> | |
struct Vector3 final | |
{ | |
float x; | |
float y; | |
float z; | |
[[nodiscard]] | |
Vector3() noexcept = default; | |
[[nodiscard]] | |
constexpr Vector3(const float v_x, const float v_y, const float v_z) noexcept | |
: x(v_x) | |
, y(v_y) | |
, z(v_z) | |
{} | |
[[nodiscard]] | |
explicit constexpr Vector3(const float v) noexcept | |
: Vector3(v, v, v) | |
{} | |
[[nodiscard]] | |
friend constexpr const Vector3 operator-(const Vector3 & v) noexcept | |
{ | |
return Vector3(-v.x, -v.y, -v.z); | |
} | |
[[nodiscard]] | |
friend constexpr const Vector3 operator+(const Vector3 & a, const Vector3 & b) noexcept | |
{ | |
return Vector3(a.x + b.x, a.y + b.y, a.z + b.z); | |
} | |
Vector3 & operator+=(const Vector3 & v) noexcept | |
{ | |
x += v.x; | |
y += v.y; | |
z += v.z; | |
return *this; | |
} | |
[[nodiscard]] | |
friend constexpr const Vector3 operator-(const Vector3 & a, const Vector3 & b) noexcept | |
{ | |
return Vector3(a.x - b.x, a.y - b.y, a.z - b.z); | |
} | |
Vector3 & operator-=(const Vector3 & v) noexcept | |
{ | |
x -= v.x; | |
y -= v.y; | |
z -= v.z; | |
return *this; | |
} | |
[[nodiscard]] | |
friend constexpr const Vector3 operator*(const Vector3 & a, const Vector3 & b) noexcept | |
{ | |
return Vector3(a.x * b.x, a.y * b.y, a.z * b.z); | |
} | |
[[nodiscard]] | |
friend constexpr const Vector3 operator*(const Vector3 & v, const float s) noexcept | |
{ | |
return Vector3(v.x * s, v.y * s, v.z * s); | |
} | |
[[nodiscard]] | |
friend constexpr const Vector3 operator*(const float s, const Vector3 & v) noexcept | |
{ | |
return Vector3(s * v.x, s * v.y, s * v.z); | |
} | |
Vector3 & operator*=(const Vector3 & v) noexcept | |
{ | |
x *= v.x; | |
y *= v.y; | |
z *= v.z; | |
return *this; | |
} | |
Vector3 & operator*=(const float s) noexcept | |
{ | |
x *= s; | |
y *= s; | |
z *= s; | |
return *this; | |
} | |
[[nodiscard]] | |
friend constexpr const Vector3 operator/(const Vector3 & v, const float s) noexcept | |
{ | |
return v * (1.0f / s); | |
} | |
Vector3 & operator/=(const float s) noexcept | |
{ | |
return *this *= (1.0f / s); | |
} | |
[[nodiscard]] | |
constexpr float dot(const Vector3 & v) const noexcept | |
{ | |
return x * v.x + y * v.y + z * v.z; | |
} | |
[[nodiscard]] | |
float length() const noexcept | |
{ | |
return std::sqrt(dot(*this)); | |
} | |
[[nodiscard]] | |
constexpr const Vector3 cross(const Vector3 & v) const noexcept | |
{ | |
return Vector3( | |
y * v.z - z * v.y, | |
z * v.x - x * v.z, | |
x * v.y - y * v.x | |
); | |
} | |
}; | |
#endif // VECTOR3_H_INCLUDED |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment