mirror of
https://github.com/orange-cpp/omath.git
synced 2026-02-13 07:03:25 +00:00
added Vec4
This commit is contained in:
@@ -6,8 +6,7 @@
|
||||
|
||||
namespace uml
|
||||
{
|
||||
class Vector3 final
|
||||
{
|
||||
class Vector3 {
|
||||
public:
|
||||
float x = 0.f;
|
||||
float y = 0.f;
|
||||
|
||||
42
include/uml/Vector4.h
Normal file
42
include/uml/Vector4.h
Normal file
@@ -0,0 +1,42 @@
|
||||
//
|
||||
// Vector4.h
|
||||
//
|
||||
#pragma once
|
||||
|
||||
#include <uml/Vector3.h>
|
||||
|
||||
namespace uml
|
||||
{
|
||||
class Vector4 final : public Vector3
|
||||
{
|
||||
public:
|
||||
float w = 0.f;
|
||||
|
||||
Vector4(float x = 0.f, float y = 0.f, float z = 0.f, float w = 0.f) : Vector3(x, y, z), w(w) {}
|
||||
Vector4() = default;
|
||||
|
||||
bool operator==(const Vector4& src) const;
|
||||
bool operator!=(const Vector4& src) const;
|
||||
|
||||
Vector4& operator+=(const Vector4& v);
|
||||
Vector4& operator-=(const Vector4& v);
|
||||
Vector4& operator*=(float scalar);
|
||||
Vector4& operator*=(const Vector4& v);
|
||||
Vector4& operator/=(float scalar);
|
||||
Vector4& operator/=(const Vector4& v);
|
||||
|
||||
[[nodiscard]] float Length() const;
|
||||
[[nodiscard]] float LengthSqr() const;
|
||||
[[nodiscard]] float Dot(const Vector4& vOther) const;
|
||||
Vector4& Abs();
|
||||
Vector4 operator-() const;
|
||||
Vector4 operator+(const Vector4& v) const;
|
||||
Vector4 operator-(const Vector4& v) const;
|
||||
Vector4 operator*(float scalar) const;
|
||||
Vector4 operator*(const Vector4& v) const;
|
||||
Vector4 operator/(float scalar) const;
|
||||
Vector4 operator/(const Vector4& v) const;
|
||||
|
||||
[[nodiscard]] float Sum() const;
|
||||
};
|
||||
}
|
||||
@@ -3,4 +3,5 @@ target_sources(uml PRIVATE
|
||||
matrix.cpp
|
||||
angles.cpp
|
||||
ProjectilePredictor.cpp
|
||||
color.cpp)
|
||||
color.cpp
|
||||
Vector4.cpp)
|
||||
123
source/Vector4.cpp
Normal file
123
source/Vector4.cpp
Normal file
@@ -0,0 +1,123 @@
|
||||
//
|
||||
// Vector4.cpp
|
||||
//
|
||||
|
||||
#include "Vector4.h"
|
||||
#include <cmath>
|
||||
|
||||
namespace uml
|
||||
{
|
||||
bool Vector4::operator==(const Vector4& src) const
|
||||
{
|
||||
return Vector3::operator==(src) && w == src.w;
|
||||
}
|
||||
|
||||
bool Vector4::operator!=(const Vector4& src) const
|
||||
{
|
||||
return !(*this == src);
|
||||
}
|
||||
|
||||
Vector4& Vector4::operator+=(const Vector4& v)
|
||||
{
|
||||
Vector3::operator+=(v);
|
||||
w += v.w;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vector4& Vector4::operator-=(const Vector4& v)
|
||||
{
|
||||
Vector3::operator-=(v);
|
||||
w -= v.w;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vector4& Vector4::operator*=(float scalar)
|
||||
{
|
||||
Vector3::operator*=(scalar);
|
||||
w *= scalar;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vector4& Vector4::operator*=(const Vector4& v)
|
||||
{
|
||||
Vector3::operator*=(v);
|
||||
w *= v.w;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vector4& Vector4::operator/=(float scalar)
|
||||
{
|
||||
Vector3::operator/=(scalar);
|
||||
w /= scalar;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vector4& Vector4::operator/=(const Vector4& v)
|
||||
{
|
||||
Vector3::operator/=(v);
|
||||
w /= v.w;
|
||||
return *this;
|
||||
}
|
||||
|
||||
float Vector4::Length() const
|
||||
{
|
||||
return std::sqrt(LengthSqr());
|
||||
}
|
||||
|
||||
float Vector4::LengthSqr() const
|
||||
{
|
||||
return Vector3::LengthSqr() + w * w;
|
||||
}
|
||||
|
||||
float Vector4::Dot(const Vector4& vOther) const
|
||||
{
|
||||
return Vector3::Dot(vOther) + w * vOther.w;
|
||||
}
|
||||
|
||||
Vector4& Vector4::Abs()
|
||||
{
|
||||
Vector3::Abs();
|
||||
w = std::abs(w);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vector4 Vector4::operator-() const
|
||||
{
|
||||
return Vector4(-x, -y, -z, -w);
|
||||
}
|
||||
|
||||
Vector4 Vector4::operator+(const Vector4& v) const
|
||||
{
|
||||
return Vector4(x + v.x, y + v.y, z + v.z, w + v.w);
|
||||
}
|
||||
|
||||
Vector4 Vector4::operator-(const Vector4& v) const
|
||||
{
|
||||
return Vector4(x - v.x, y - v.y, z - v.z, w - v.w);
|
||||
}
|
||||
|
||||
Vector4 Vector4::operator*(float scalar) const
|
||||
{
|
||||
return Vector4(x * scalar, y * scalar, z * scalar, w * scalar);
|
||||
}
|
||||
|
||||
Vector4 Vector4::operator*(const Vector4& v) const
|
||||
{
|
||||
return Vector4(x * v.x, y * v.y, z * v.z, w * v.w);
|
||||
}
|
||||
|
||||
Vector4 Vector4::operator/(float scalar) const
|
||||
{
|
||||
return Vector4(x / scalar, y / scalar, z / scalar, w / scalar);
|
||||
}
|
||||
|
||||
Vector4 Vector4::operator/(const Vector4& v) const
|
||||
{
|
||||
return Vector4(x / v.x, y / v.y, z / v.z, w / v.w);
|
||||
}
|
||||
|
||||
float Vector4::Sum() const
|
||||
{
|
||||
return x + y + z + w;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user