Vector2 full implementation
This commit is contained in:
@@ -18,12 +18,9 @@ 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;
|
||||
vector2();
|
||||
vector2() : x(0), y(0) {}
|
||||
vector2(float X, float Y) : x(X), y(Y) {}
|
||||
vector2(const vector2&) = default; // Copy Constructor
|
||||
vector2(vector2&&) = default; // Move Constructor
|
||||
@@ -39,30 +36,28 @@ public:
|
||||
float lengthSquared() const;
|
||||
float magnitude() const;
|
||||
float dot(const vector2& rhs) const;
|
||||
vector2 project(const vector2& rhs);
|
||||
vector2 project(const vector2& rhs) const;
|
||||
vector2 normalize() const;
|
||||
vector2 lerp(const vector2& rhs, float alpha) const;
|
||||
vector2 operator+(const vector2& rhs) const;
|
||||
vector2 operator-(const vector2& rhs) const;
|
||||
vector2 operator*(float rhs) const;
|
||||
vector2 operator/(float rhs) const;
|
||||
vector2 operator+() const;
|
||||
vector2 operator+() const; // TODO: Implement
|
||||
vector2 operator-() const;
|
||||
public:
|
||||
float x = 0;
|
||||
float y = 0;
|
||||
};
|
||||
|
||||
class vector3 : public numeric_vector<3> {
|
||||
class vector3 : public numeric_vector<3>
|
||||
{
|
||||
public:
|
||||
vector3() : x(0), y(0), z(0) {}
|
||||
vector3(float X, float Y, float Z): x(X), y(Y), z(Z) {}
|
||||
vector3(const vector3&); // Copy
|
||||
vector3(const vector3& rhs);
|
||||
vector3(vector3&&) = default;
|
||||
vector3(const vector2&);
|
||||
vector3 operator=(const vector3& rhs)
|
||||
{
|
||||
this->x = rhs.x;
|
||||
this->y = rhs.y;
|
||||
this->z = rhs.z;
|
||||
}
|
||||
vector3& operator=(const vector3& rhs);
|
||||
float operator[](std::size_t index) override;
|
||||
bool IsWithinMarginOfError(const vector3& rhs, float margin=0.001f) const;
|
||||
bool operator == (const vector3& rhs) const;
|
||||
@@ -148,7 +143,9 @@ class AxisAngle {
|
||||
float angle;
|
||||
};
|
||||
|
||||
class Position : public vector3 {
|
||||
using Position = vector3;
|
||||
|
||||
/*class Position : public vector3 {
|
||||
public:
|
||||
bool operator==(const Position& p) const {
|
||||
return (x == p.x) && (y == p.y) && (z == p.z);
|
||||
@@ -187,7 +184,8 @@ public:
|
||||
float distanceFrom(Position p) {
|
||||
return sqrt(pow(this->x - p.x, 2) + pow(this->y - p.y, 2) + pow(this->z - p.z, 2));
|
||||
}
|
||||
};
|
||||
};*/
|
||||
|
||||
// The CFrame is fundamentally 4 vectors (position, forward, right, up vector)
|
||||
class CoordinateFrame
|
||||
{
|
||||
|
@@ -2,6 +2,95 @@
|
||||
|
||||
#pragma region vector2
|
||||
|
||||
bool vector2::IsWithinMarginOfError(const vector2& rhs, float margin) const
|
||||
{
|
||||
return this->distance(rhs) <= margin;
|
||||
}
|
||||
|
||||
bool vector2::operator==(const vector2& rhs) const
|
||||
{
|
||||
return this->IsWithinMarginOfError(rhs);
|
||||
}
|
||||
|
||||
bool vector2::operator!=(const vector2& rhs) const
|
||||
{
|
||||
return this->IsWithinMarginOfError(rhs) == false;
|
||||
}
|
||||
|
||||
vector2 vector2::min(const vector2& min) const
|
||||
{
|
||||
return {
|
||||
std::min(this->x, min.x),
|
||||
std::min(this->y, min.y)
|
||||
};
|
||||
}
|
||||
|
||||
vector2 vector2::max(const vector2& max) const
|
||||
{
|
||||
return {
|
||||
std::max(this->x, max.x),
|
||||
std::max(this->y, max.y)
|
||||
};
|
||||
}
|
||||
|
||||
vector2 vector2::clamp(const vector2& min, const vector2& max) const
|
||||
{
|
||||
return {
|
||||
std::clamp(this->x, min.x, max.x),
|
||||
std::clamp(this->y, min.y, max.y)
|
||||
};
|
||||
}
|
||||
|
||||
float vector2::distance(const vector2& to) const
|
||||
{
|
||||
return ((*this)-to).magnitude();
|
||||
}
|
||||
|
||||
float vector2::length() const
|
||||
{
|
||||
return std::sqrt(lengthSquared());
|
||||
}
|
||||
|
||||
float vector2::lengthSquared() const
|
||||
{
|
||||
return (x*x + y*y);
|
||||
}
|
||||
|
||||
float vector2::magnitude() const
|
||||
{
|
||||
return std::sqrt(lengthSquared());
|
||||
}
|
||||
|
||||
float vector2::dot(const vector2& rhs) const
|
||||
{
|
||||
auto a = this->normalize();
|
||||
auto b = rhs.normalize();
|
||||
|
||||
return a.x * b.x + a.y * b.y;
|
||||
}
|
||||
|
||||
vector2 vector2::project(const vector2& rhs) const
|
||||
{
|
||||
float scalar = this->dot(rhs) / (rhs.magnitude()*rhs.magnitude());
|
||||
return rhs * scalar;
|
||||
}
|
||||
|
||||
vector2 vector2::normalize() const
|
||||
{
|
||||
if (length() > 0)
|
||||
return {
|
||||
x / length(),
|
||||
y / length()
|
||||
};
|
||||
else
|
||||
return {0,0};
|
||||
}
|
||||
|
||||
vector2 vector2::lerp(const vector2& rhs, float alpha) const
|
||||
{
|
||||
return this->operator*(1.0f - alpha) + (rhs * alpha);
|
||||
}
|
||||
|
||||
vector2 vector2::operator+(const vector2& rhs) const
|
||||
{
|
||||
return {this->x + rhs.x, this->y + rhs.y};
|
||||
@@ -28,6 +117,13 @@ vector2 vector2::operator/(float rhs) const
|
||||
};
|
||||
}
|
||||
|
||||
vector2 vector2::operator-() const
|
||||
{
|
||||
return {-x, -y};
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
#pragma endregion
|
||||
|
||||
@@ -70,6 +166,21 @@ vector3 vector3::operator-() const
|
||||
return {-x, -y, -z};
|
||||
}
|
||||
|
||||
vector3::vector3(const vector3& rhs)
|
||||
{
|
||||
this->x = rhs.x;
|
||||
this->y = rhs.y;
|
||||
this->z = rhs.z;
|
||||
}
|
||||
|
||||
vector3& vector3::operator=(const vector3& rhs)
|
||||
{
|
||||
this->x = rhs.x;
|
||||
this->y = rhs.y;
|
||||
this->z = rhs.z;
|
||||
return *this;
|
||||
}
|
||||
|
||||
float vector3::operator[](std::size_t index)
|
||||
{
|
||||
assert(index < 3);
|
||||
@@ -89,6 +200,11 @@ bool vector3::operator==(const vector3& rhs) const
|
||||
return this->IsWithinMarginOfError(rhs);
|
||||
}
|
||||
|
||||
bool vector3::operator!=(const vector3& rhs) const
|
||||
{
|
||||
return this->IsWithinMarginOfError(rhs) == false;
|
||||
}
|
||||
|
||||
vector3 vector3::min(const vector3& min) const
|
||||
{
|
||||
return {
|
||||
@@ -110,9 +226,9 @@ vector3 vector3::max(const vector3& max) const
|
||||
vector3 vector3::clamp(const vector3& min, const vector3& max) const
|
||||
{
|
||||
return {
|
||||
std::clamp(min.x, this->x, max.x),
|
||||
std::clamp(min.y, this->y, max.y),
|
||||
std::clamp(min.z, this->z, max.z)
|
||||
std::clamp(this->x, min.x, max.x),
|
||||
std::clamp(this->y, min.y, max.y),
|
||||
std::clamp(this->z, min.z, max.z)
|
||||
};
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user