Operator cleanup to Vector2 and Vector3. Also added a few methods, and made x,y,z variables a union with an array, so the vector is also indexable.

This commit is contained in:
Relintai 2021-11-16 03:08:16 +01:00
parent e74549776f
commit ff6dd8ea5d
4 changed files with 243 additions and 63 deletions

View File

@ -128,6 +128,10 @@ Vector2 &Vector2::operator+=(const Vector2 &b) {
return *this;
}
Vector2 Vector2::operator+(const Vector2 &b) const {
return Vector2(x + b.x, y + b.y);
}
Vector2 &Vector2::operator-=(const Vector2 &b) {
x -= b.x;
y -= b.y;
@ -135,38 +139,87 @@ Vector2 &Vector2::operator-=(const Vector2 &b) {
return *this;
}
Vector2 &Vector2::operator*=(const float b) {
x *= b;
y *= b;
Vector2 Vector2::operator-(const Vector2 &b) const {
return Vector2(x - b.x, y - b.y);
}
Vector2 &Vector2::operator*=(const Vector2 &b) {
x *= b.x;
y *= b.y;
return *this;
}
Vector2 operator*(Vector2 lhs, const float rhs) {
lhs.x *= rhs;
lhs.y *= rhs;
return lhs;
Vector2 Vector2::operator*(const Vector2 &b) const {
return Vector2(x * b.x, y * b.y);
}
Vector2 operator+(Vector2 lhs, const Vector2 &rhs) {
lhs.x += rhs.x;
lhs.y += rhs.y;
Vector2 &Vector2::operator/=(const Vector2 &b) {
x /= b.x;
y /= b.y;
return lhs;
return *this;
}
Vector2 operator-(Vector2 lhs, const Vector2 &rhs) {
lhs.x -= rhs.x;
lhs.y -= rhs.y;
return lhs;
Vector2 Vector2::operator/(const Vector2 &b) const {
return Vector2(x / b.x, y / b.y);
}
bool operator==(const Vector2 &a, const Vector2 &b) {
return a.is_equal_approx(b);
Vector2 &Vector2::operator+=(float scalar) {
x += scalar;
y += scalar;
return *this;
}
Vector2 Vector2::operator+(float scalar) const {
return Vector2(x + scalar, y + scalar);
}
Vector2 &Vector2::operator-=(float scalar) {
x += scalar;
y += scalar;
return *this;
}
Vector2 Vector2::operator-(float scalar) const {
return Vector2(x - scalar, y - scalar);
}
bool operator!=(const Vector2 &a, const Vector2 &b) {
return !(a == b);
Vector2 &Vector2::operator*=(float scalar) {
x *= scalar;
y *= scalar;
return *this;
}
Vector2 Vector2::operator*(float scalar) const {
return Vector2(x * scalar, y * scalar);
}
Vector2 &Vector2::operator/=(float scalar) {
x /= scalar;
y /= scalar;
return *this;
}
Vector2 Vector2::operator/(float scalar) const {
return Vector2(x / scalar, y / scalar);
}
Vector2 Vector2::operator-() const {
return Vector2(-x, -y);
}
bool Vector2::operator==(const Vector2 &b) const {
return x == b.x && y == y;
}
bool Vector2::operator!=(const Vector2 &b) const {
return x != b.x || y == b.y;
}
const float &Vector2::operator[](int axis) const {
return coordinates[axis];
}
float &Vector2::operator[](int axis) {
return coordinates[axis];
}
Vector2::operator String() const {
return "[" + String::num(x) + "," + String::num(y) + "]";
}

View File

@ -1,6 +1,8 @@
#ifndef VECTOR2_H
#define VECTOR2_H
#include "core/string.h"
class Vector2 {
public:
Vector2 abs() const;
@ -30,19 +32,46 @@ public:
Vector2(const float p_x, const float p_y);
Vector2 &operator+=(const Vector2 &b);
Vector2 operator+(const Vector2 &b) const;
Vector2 &operator-=(const Vector2 &b);
Vector2 operator-(const Vector2 &b) const;
Vector2 &operator*=(const Vector2 &b);
Vector2 operator*(const Vector2 &b) const;
Vector2 &operator/=(const Vector2 &b);
Vector2 operator/(const Vector2 &b) const;
friend Vector2 operator+(Vector2 lhs, const Vector2 &rhs);
friend Vector2 operator-(Vector2 lhs, const Vector2 &rhs);
Vector2 &operator+=(float scalar);
Vector2 operator+(float scalar) const;
Vector2 &operator-=(float scalar);
Vector2 operator-(float scalar) const;
Vector2 &operator*=(float scalar);
Vector2 operator*(float scalar) const;
Vector2 &operator/=(float scalar);
Vector2 operator/(float scalar) const;
Vector2 &operator*=(const float b);
friend Vector2 operator*(Vector2 lhs, const float rhs);
Vector2 operator-() const;
friend bool operator==(const Vector2 &a, const Vector2 &b);
friend bool operator!=(const Vector2 &a, const Vector2 &b);
bool operator==(const Vector2 &b) const;
bool operator!=(const Vector2 &b) const;
const float &operator[](int axis) const;
float &operator[](int axis);
operator String() const;
enum Axis {
AXIS_X = 0,
AXIS_Y
};
union {
struct {
float x;
float y;
};
float coordinates[2];
};
};
#endif

View File

@ -115,6 +115,16 @@ void Vector3::sub(const Vector3 &b) {
z -= b.z;
}
void Vector3::zero() {
x = 0;
y = 0;
z = 0;
}
Vector3 Vector3::inverse() const {
return Vector3(1.0 / x, 1.0 / y, 1.0 / z);
}
Vector3::Vector3() {
x = 0;
y = 0;
@ -141,6 +151,10 @@ Vector3 &Vector3::operator+=(const Vector3 &b) {
return *this;
}
Vector3 Vector3::operator+(const Vector3 &b) const {
return Vector3(x + b.x, y + b.y, z + b.z);
}
Vector3 &Vector3::operator-=(const Vector3 &b) {
x -= b.x;
y -= b.y;
@ -149,42 +163,93 @@ Vector3 &Vector3::operator-=(const Vector3 &b) {
return *this;
}
Vector3 &Vector3::operator*=(const float b) {
x *= b;
y *= b;
z *= b;
Vector3 Vector3::operator-(const Vector3 &b) const {
return Vector3(x - b.x, y - b.y, z - b.z);
}
Vector3 &Vector3::operator*=(const Vector3 &b) {
x *= b.x;
y *= b.y;
z *= b.z;
return *this;
}
Vector3 operator*(Vector3 lhs, const float rhs) {
lhs.x *= rhs;
lhs.y *= rhs;
lhs.z *= rhs;
return lhs;
Vector3 Vector3::operator*(const Vector3 &b) const {
return Vector3(x * b.x, y * b.y, z * b.z);
}
Vector3 operator+(Vector3 lhs, const Vector3 &rhs) {
lhs.x += rhs.x;
lhs.y += rhs.y;
lhs.z += rhs.z;
Vector3 &Vector3::operator/=(const Vector3 &b) {
x /= b.x;
y /= b.y;
z /= b.z;
return lhs;
return *this;
}
Vector3 operator-(Vector3 lhs, const Vector3 &rhs) {
lhs.x -= rhs.x;
lhs.y -= rhs.y;
lhs.z -= rhs.z;
return lhs;
Vector3 Vector3::operator/(const Vector3 &b) const {
return Vector3(x / b.x, y / b.y, z / b.z);
}
bool operator==(const Vector3 &a, const Vector3 &b) {
return a.is_equal_approx(b);
Vector3 &Vector3::operator+=(float scalar) {
x += scalar;
y += scalar;
z += scalar;
return *this;
}
Vector3 Vector3::operator+(float scalar) const {
return Vector3(x + scalar, y + scalar, z + scalar);
}
Vector3 &Vector3::operator-=(float scalar) {
x += scalar;
y += scalar;
z += scalar;
return *this;
}
Vector3 Vector3::operator-(float scalar) const {
return Vector3(x - scalar, y - scalar, z - scalar);
}
bool operator!=(const Vector3 &a, const Vector3 &b) {
return !(a == b);
Vector3 &Vector3::operator*=(float scalar) {
x *= scalar;
y *= scalar;
z *= scalar;
return *this;
}
Vector3 Vector3::operator*(float scalar) const {
return Vector3(x * scalar, y * scalar, z * scalar);
}
Vector3 &Vector3::operator/=(float scalar) {
x /= scalar;
y /= scalar;
z /= scalar;
return *this;
}
Vector3 Vector3::operator/(float scalar) const {
return Vector3(x / scalar, y / scalar, z / scalar);
}
Vector3 Vector3::operator-() const {
return Vector3(-x, -y, -z);
}
bool Vector3::operator==(const Vector3 &b) const {
return x == b.x && y == y && z == b.z;
}
bool Vector3::operator!=(const Vector3 &b) const {
return x != b.x || y == b.y || z == b.z;
}
const float &Vector3::operator[](int axis) const {
return coordinates[axis];
}
float &Vector3::operator[](int axis) {
return coordinates[axis];
}
Vector3::operator String() const {
return "[" + String::num(x) + "," + String::num(y) + "," + String::num(z) + "]";
}

View File

@ -1,6 +1,8 @@
#ifndef VECTOR3_H
#define VECTOR3_H
#include "core/string.h"
class Vector3 {
public:
Vector3 abs() const;
@ -24,25 +26,56 @@ public:
void add(const Vector3 &b);
void sub(const Vector3 &b);
void zero();
Vector3 inverse() const;
Vector3();
Vector3(const Vector3 &b);
Vector3(const float p_x, const float p_y, const float p_z);
Vector3 &operator+=(const Vector3 &b);
Vector3 operator+(const Vector3 &b) const;
Vector3 &operator-=(const Vector3 &b);
Vector3 operator-(const Vector3 &b) const;
Vector3 &operator*=(const Vector3 &b);
Vector3 operator*(const Vector3 &b) const;
Vector3 &operator/=(const Vector3 &b);
Vector3 operator/(const Vector3 &b) const;
friend Vector3 operator+(Vector3 lhs, const Vector3 &rhs);
friend Vector3 operator-(Vector3 lhs, const Vector3 &rhs);
Vector3 &operator+=(float scalar);
Vector3 operator+(float scalar) const;
Vector3 &operator-=(float scalar);
Vector3 operator-(float scalar) const;
Vector3 &operator*=(float scalar);
Vector3 operator*(float scalar) const;
Vector3 &operator/=(float scalar);
Vector3 operator/(float scalar) const;
Vector3 &operator*=(const float b);
friend Vector3 operator*(Vector3 lhs, const float rhs);
Vector3 operator-() const;
friend bool operator==(const Vector3 &a, const Vector3 &b);
friend bool operator!=(const Vector3 &a, const Vector3 &b);
bool operator==(const Vector3 &b) const;
bool operator!=(const Vector3 &b) const;
const float &operator[](int axis) const;
float &operator[](int axis);
operator String() const;
enum Axis {
AXIS_X = 0,
AXIS_Y,
AXIS_Z
};
union {
struct {
float x;
float y;
float z;
};
float coordinates[3];
};
};
#endif