Implementating Vector2 class

This commit is contained in:
2023-12-13 06:07:32 -06:00
parent 1c4727386a
commit 2db092d94d
2 changed files with 164 additions and 10 deletions

View File

@@ -17,13 +17,20 @@ public:
virtual float operator[](std::size_t index) = 0;
};
class vector2 : public numeric_vector<2> {};
class vector2 : public numeric_vector<2>
{
public:
float x = 0;
float y = 0;
float z = 0;
vector2();
};
class vector3 : public numeric_vector<3>
{
public:
float x = 0; //pitch
float y = 0; //yaw
float z = 0; //roll
float x = 0;
float y = 0;
float z = 0;
public:
vector3() : x(0), y(0), z(0) {}
vector3(float X, float Y, float Z): x(X), y(Y), z(Z) {}
@@ -49,11 +56,6 @@ public:
vector3 lerp(const vector3& goal, float alpha) const;
vector3 operator+(const vector3& rhs) const;
vector3 operator-(const vector3& rhs) const;
float operator*(const vector3& rhs) const
{
return this->dot(rhs);
}
vector3 operator/(const vector3& rhs) const;
vector3 operator*(float rhs) const;
vector3 operator/(float rhs) const;
vector3 operator+() const;
@@ -62,8 +64,11 @@ public:
class vector4 : public numeric_vector<4> {};
class Angle : public vector3 {
class Angle {
public:
float pitch;
float yaw;
float roll;
bool operator==(const Angle& a) const {
return (x == a.x) && (y == a.y) && (z == a.z);
}

149
src/types/vector.cpp Normal file
View File

@@ -0,0 +1,149 @@
#include <types/vector3.h>
vector3 vector3::operator+(const vector3& rhs) const
{
return {this->x + rhs.x, this->y + rhs.y, this->z + rhs.z};
}
vector3 vector3::operator-(const vector3& rhs) const
{
return {
this->x- rhs.x,
this->y-rhs.y,
this->z-rhs.z
};
}
vector3 vector3::operator*(float rhs) const
{
return {
this->x * rhs,
this->y * rhs,
this->z * rhs
};
}
vector3 vector3::operator/(float rhs) const
{
return {
this->x / rhs,
this->y / rhs,
this->z / rhs
};
}
vector3 vector3::operator-() const
{
return {-x, -y, -z};
}
float vector3::operator[](std::size_t index)
{
assert(index < 3);
if (index==0) return x;
if (index==1) return y;
if (index==2) return z;
return 0;
}
bool vector3::IsWithinMarginOfError(const vector3& rhs, float margin) const
{
return this->distance(rhs) <= margin;
}
bool vector3::operator==(const vector3& rhs) const
{
return this->IsWithinMarginOfError(rhs);
}
vector3 vector3::min(const vector3& min) const
{
return {
std::min(this->x, min.x),
std::min(this->y, min.y),
std::min(this->z, min.z)
};
}
vector3 vector3::max(const vector3& max) const
{
return {
std::max(this->x, max.x),
std::max(this->y, max.y),
std::max(this->z, max.z)
};
}
vector3 vector3::clamp(const vector3& min, const vector3& max) const
{
return {
std::ranges::clamp(min.x, this->x, max.x),
std::ranges::clamp(min.y, this->y, max.y),
std::ranges::clamp(min.z, this->z, max.z)
};
}
float vector3::distance(const vector3& to) const
{
return ((*this)-to).magnitude();
}
float vector3::length() const
{
return std::sqrt(lengthSquared());
}
float vector3::lengthSquared() const
{
return (x*x + y*y + z*z);
}
float vector3::magnitude() const
{
return std::sqrt(x*x + y*y + z*z);
}
float vector3::dot(const vector3& rhs) const
{
auto a = this->normalize();
auto b = rhs.normalize();
return a.x * b.x +
a.y * b.y +
a.z * b.z;
}
vector3 vector3::project(const vector3& rhs) const
{
float scalar = this->dot(rhs) / (rhs.magnitude()*rhs.magnitude());
return rhs * scalar;
}
vector3 vector3::cross(const vector3& rhs) const
{
return {
this->y * rhs.z - this->z * rhs.y,
this->z * rhs.x - this->x * rhs.z,
this->x * rhs.y - this->y * rhs.x
};
}
vector3 vector3::normalize() const
{
if (length() > 0)
return {
x / length(),
y / length(),
z / length()
};
else
return {0,0,0};
}
vector3 vector3::lerp(const vector3& goal, float alpha) const
{
return this->operator*(1.0f - alpha) + (goal * alpha);
}