feat(vector): update C2Vector class

This commit is contained in:
VDm 2025-05-27 01:02:28 +04:00
parent 8e406788b1
commit f28f744a5b
5 changed files with 248 additions and 2 deletions

View file

@ -1,4 +1,174 @@
#include "tempest/vector/C2Vector.hpp"
#include "tempest/Math.hpp"
#include <storm/Error.hpp>
C2Vector C2Vector::FromAxisAngle(float axang, float mag) {
float x = mag * CMath::cos(axang);
float y = mag * CMath::sin(axang);
return C2Vector(x, y);
}
C2Vector C2Vector::Min(const C2Vector& a, const C2Vector& b) {
float x = std::fmin(a.x, b.x);
float y = std::fmin(a.y, b.y);
return C2Vector(x, y);
}
C2Vector C2Vector::Max(const C2Vector& a, const C2Vector& b) {
float x = std::fmax(a.x, b.x);
float y = std::fmax(a.y, b.y);
return C2Vector(x, y);
}
C2Vector C2Vector::Lerp(const C2Vector& a, const C2Vector& l, const C2Vector& h) {
return C2Vector(
l.x + (h.x - l.x) * a.x,
l.y + (h.y - h.y) * a.y);
}
float C2Vector::Dot(const C2Vector& l, const C2Vector& r) {
return (l.x * r.x) + (l.y * r.y);
}
float C2Vector::Cross(const C2Vector& l, const C2Vector& r) {
return (l.x * r.y) - (r.x * l.y);
}
void C2Vector::Get(float& ox, float& oy) const {
ox = this->x;
oy = this->y;
}
void C2Vector::Set(float nx, float ny) {
this->x = nx;
this->y = ny;
}
C2Vector& C2Vector::operator+=(float a) {
this->x += a;
this->y += a;
return *this;
}
C2Vector& C2Vector::operator+=(const C2Vector& a) {
this->x += a.x;
this->y += a.y;
return *this;
}
C2Vector& C2Vector::operator-=(float a) {
this->x -= a;
this->y -= a;
return *this;
}
C2Vector& C2Vector::operator-=(const C2Vector& a) {
this->x -= a.x;
this->y -= a.y;
return *this;
}
C2Vector& C2Vector::operator*=(float a) {
this->x *= a;
this->y *= a;
return *this;
}
C2Vector& C2Vector::operator*=(const C2Vector& a) {
this->x *= a.x;
this->y *= a.y;
return *this;
}
C2Vector& C2Vector::operator/=(float a) {
this->x /= a;
this->y /= a;
return *this;
}
C2Vector& C2Vector::operator/=(const C2Vector& a) {
this->x /= a.x;
this->y /= a.y;
return *this;
}
C2Vector C2Vector::operator-() {
return C2Vector(-this->x, -this->y);
}
float& C2Vector::operator[](uint32_t sub) {
STORM_ASSERT(sub < C2Vector::eComponents);
switch (sub) {
case 1:
return this->y;
case 0:
default:
return this->x;
}
}
const float& C2Vector::operator[](uint32_t sub) const {
STORM_ASSERT(sub < C2Vector::eComponents);
switch (sub) {
case 1:
return this->y;
case 0:
default:
return this->x;
}
}
float C2Vector::SquaredMag() const {
return (this->x * this->x) + (this->y * this->y);
}
float C2Vector::Mag() const {
return CMath::sqrt(this->SquaredMag());
}
float C2Vector::SumC() const {
return this->x + this->y;
}
bool C2Vector::IsUnit() const {
return CMath::fequalz(this->SquaredMag(), 1.0f, 0.001f);
}
float C2Vector::AxisAngle() const {
return this->AxisAngle(this->Mag());
}
float C2Vector::AxisAngle(float mag) const {
if (CMath::fequal(mag, 0.0f)) {
return 0.0f;
}
float angle = CMath::acos(this->x / mag);
if (this->y >= 0.0f) {
return angle;
}
else {
return 6.2831855f - angle;
}
}
void C2Vector::Normalize() {
float mag = this->Mag();
STORM_ASSERT(mag > 0.0f);
mag = 1.0f / mag;
this->x *= mag;
this->y *= mag;
}
void C2Vector::Scale(float a) {
float mag = this->Mag();
STORM_ASSERT(mag > 0.0f);
mag = a * 1.0f / mag;
this->x *= mag;
this->y *= mag;
}
bool C2Vector::operator==(const C2Vector& v) {
return this->x == v.x && this->y == v.y;

View file

@ -1,17 +1,63 @@
#ifndef TEMPEST_VECTOR_C_2VECTOR_HPP
#define TEMPEST_VECTOR_C_2VECTOR_HPP
#include <cstdint>
class C2Vector {
public:
// Member variables
float x = 0.0f;
float y = 0.0f;
enum : uint32_t {
eComponents = 2
};
// Static functions
static C2Vector FromAxisAngle(float axang, float mag);
static C2Vector Min(const C2Vector& a, const C2Vector& b);
static C2Vector Max(const C2Vector& a, const C2Vector& b);
static C2Vector Lerp(const C2Vector& a, const C2Vector& l, const C2Vector& h);
static float Dot(const C2Vector& l, const C2Vector& r);
static float Cross(const C2Vector& l, const C2Vector& r);
// Member functions
C2Vector() = default;
C2Vector(float x, float y)
: x(x)
, y(y) {};
C2Vector(float a)
: x(a)
, y(a) {};
void Get(float& ox, float& oy) const;
void Set(float nx, float ny);
C2Vector& operator+=(float a);
C2Vector& operator+=(const C2Vector& a);
C2Vector& operator-=(float a);
C2Vector& operator-=(const C2Vector& a);
C2Vector& operator*=(float a);
C2Vector& operator*=(const C2Vector& a);
C2Vector& operator/=(float a);
C2Vector& operator/=(const C2Vector& a);
C2Vector operator-();
float& operator[](uint32_t sub);
const float& operator[](uint32_t sub) const;
float SquaredMag() const;
float Mag() const;
float SumC() const;
bool IsUnit() const;
float AxisAngle() const;
float AxisAngle(float mag) const;
void Normalize();
void Scale(float a);
bool operator==(const C2Vector& v);
};

View file

@ -14,6 +14,10 @@ class C3Vector {
// Member functions
C3Vector() = default;
C3Vector(float a)
: x(a)
, y(a)
, z(a) {};
C3Vector(float x, float y, float z)
: x(x)
, y(y)