mirror of
https://github.com/Relintai/rcpp_framework.git
synced 2025-02-20 15:14:26 +01:00
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:
parent
e74549776f
commit
ff6dd8ea5d
@ -128,6 +128,10 @@ Vector2 &Vector2::operator+=(const Vector2 &b) {
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Vector2 Vector2::operator+(const Vector2 &b) const {
|
||||||
|
return Vector2(x + b.x, y + b.y);
|
||||||
|
}
|
||||||
|
|
||||||
Vector2 &Vector2::operator-=(const Vector2 &b) {
|
Vector2 &Vector2::operator-=(const Vector2 &b) {
|
||||||
x -= b.x;
|
x -= b.x;
|
||||||
y -= b.y;
|
y -= b.y;
|
||||||
@ -135,38 +139,87 @@ Vector2 &Vector2::operator-=(const Vector2 &b) {
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector2 &Vector2::operator*=(const float b) {
|
Vector2 Vector2::operator-(const Vector2 &b) const {
|
||||||
x *= b;
|
return Vector2(x - b.x, y - b.y);
|
||||||
y *= b;
|
}
|
||||||
|
|
||||||
|
Vector2 &Vector2::operator*=(const Vector2 &b) {
|
||||||
|
x *= b.x;
|
||||||
|
y *= b.y;
|
||||||
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector2 operator*(Vector2 lhs, const float rhs) {
|
Vector2 Vector2::operator*(const Vector2 &b) const {
|
||||||
lhs.x *= rhs;
|
return Vector2(x * b.x, y * b.y);
|
||||||
lhs.y *= rhs;
|
|
||||||
|
|
||||||
return lhs;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector2 operator+(Vector2 lhs, const Vector2 &rhs) {
|
Vector2 &Vector2::operator/=(const Vector2 &b) {
|
||||||
lhs.x += rhs.x;
|
x /= b.x;
|
||||||
lhs.y += rhs.y;
|
y /= b.y;
|
||||||
|
|
||||||
return lhs;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector2 operator-(Vector2 lhs, const Vector2 &rhs) {
|
Vector2 Vector2::operator/(const Vector2 &b) const {
|
||||||
lhs.x -= rhs.x;
|
return Vector2(x / b.x, y / b.y);
|
||||||
lhs.y -= rhs.y;
|
|
||||||
|
|
||||||
return lhs;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator==(const Vector2 &a, const Vector2 &b) {
|
Vector2 &Vector2::operator+=(float scalar) {
|
||||||
return a.is_equal_approx(b);
|
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) {
|
Vector2 &Vector2::operator*=(float scalar) {
|
||||||
return !(a == b);
|
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) + "]";
|
||||||
}
|
}
|
@ -1,6 +1,8 @@
|
|||||||
#ifndef VECTOR2_H
|
#ifndef VECTOR2_H
|
||||||
#define VECTOR2_H
|
#define VECTOR2_H
|
||||||
|
|
||||||
|
#include "core/string.h"
|
||||||
|
|
||||||
class Vector2 {
|
class Vector2 {
|
||||||
public:
|
public:
|
||||||
Vector2 abs() const;
|
Vector2 abs() const;
|
||||||
@ -30,19 +32,46 @@ public:
|
|||||||
Vector2(const float p_x, const float p_y);
|
Vector2(const float p_x, const float p_y);
|
||||||
|
|
||||||
Vector2 &operator+=(const Vector2 &b);
|
Vector2 &operator+=(const Vector2 &b);
|
||||||
|
Vector2 operator+(const Vector2 &b) const;
|
||||||
Vector2 &operator-=(const Vector2 &b);
|
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);
|
Vector2 &operator+=(float scalar);
|
||||||
friend Vector2 operator-(Vector2 lhs, const Vector2 &rhs);
|
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);
|
Vector2 operator-() const;
|
||||||
friend Vector2 operator*(Vector2 lhs, const float rhs);
|
|
||||||
|
|
||||||
friend bool operator==(const Vector2 &a, const Vector2 &b);
|
bool operator==(const Vector2 &b) const;
|
||||||
friend bool operator!=(const Vector2 &a, const Vector2 &b);
|
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 x;
|
||||||
float y;
|
float y;
|
||||||
|
};
|
||||||
|
|
||||||
|
float coordinates[2];
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -115,6 +115,16 @@ void Vector3::sub(const Vector3 &b) {
|
|||||||
z -= b.z;
|
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() {
|
Vector3::Vector3() {
|
||||||
x = 0;
|
x = 0;
|
||||||
y = 0;
|
y = 0;
|
||||||
@ -141,6 +151,10 @@ Vector3 &Vector3::operator+=(const Vector3 &b) {
|
|||||||
return *this;
|
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) {
|
Vector3 &Vector3::operator-=(const Vector3 &b) {
|
||||||
x -= b.x;
|
x -= b.x;
|
||||||
y -= b.y;
|
y -= b.y;
|
||||||
@ -149,42 +163,93 @@ Vector3 &Vector3::operator-=(const Vector3 &b) {
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector3 &Vector3::operator*=(const float b) {
|
Vector3 Vector3::operator-(const Vector3 &b) const {
|
||||||
x *= b;
|
return Vector3(x - b.x, y - b.y, z - b.z);
|
||||||
y *= b;
|
}
|
||||||
z *= b;
|
|
||||||
|
Vector3 &Vector3::operator*=(const Vector3 &b) {
|
||||||
|
x *= b.x;
|
||||||
|
y *= b.y;
|
||||||
|
z *= b.z;
|
||||||
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector3 operator*(Vector3 lhs, const float rhs) {
|
Vector3 Vector3::operator*(const Vector3 &b) const {
|
||||||
lhs.x *= rhs;
|
return Vector3(x * b.x, y * b.y, z * b.z);
|
||||||
lhs.y *= rhs;
|
|
||||||
lhs.z *= rhs;
|
|
||||||
|
|
||||||
return lhs;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector3 operator+(Vector3 lhs, const Vector3 &rhs) {
|
Vector3 &Vector3::operator/=(const Vector3 &b) {
|
||||||
lhs.x += rhs.x;
|
x /= b.x;
|
||||||
lhs.y += rhs.y;
|
y /= b.y;
|
||||||
lhs.z += rhs.z;
|
z /= b.z;
|
||||||
|
|
||||||
return lhs;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector3 operator-(Vector3 lhs, const Vector3 &rhs) {
|
Vector3 Vector3::operator/(const Vector3 &b) const {
|
||||||
lhs.x -= rhs.x;
|
return Vector3(x / b.x, y / b.y, z / b.z);
|
||||||
lhs.y -= rhs.y;
|
|
||||||
lhs.z -= rhs.z;
|
|
||||||
|
|
||||||
return lhs;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator==(const Vector3 &a, const Vector3 &b) {
|
Vector3 &Vector3::operator+=(float scalar) {
|
||||||
return a.is_equal_approx(b);
|
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) {
|
Vector3 &Vector3::operator*=(float scalar) {
|
||||||
return !(a == b);
|
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) + "]";
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
#ifndef VECTOR3_H
|
#ifndef VECTOR3_H
|
||||||
#define VECTOR3_H
|
#define VECTOR3_H
|
||||||
|
|
||||||
|
#include "core/string.h"
|
||||||
|
|
||||||
class Vector3 {
|
class Vector3 {
|
||||||
public:
|
public:
|
||||||
Vector3 abs() const;
|
Vector3 abs() const;
|
||||||
@ -24,25 +26,56 @@ public:
|
|||||||
void add(const Vector3 &b);
|
void add(const Vector3 &b);
|
||||||
void sub(const Vector3 &b);
|
void sub(const Vector3 &b);
|
||||||
|
|
||||||
|
void zero();
|
||||||
|
Vector3 inverse() const;
|
||||||
|
|
||||||
Vector3();
|
Vector3();
|
||||||
Vector3(const Vector3 &b);
|
Vector3(const Vector3 &b);
|
||||||
Vector3(const float p_x, const float p_y, const float p_z);
|
Vector3(const float p_x, const float p_y, const float p_z);
|
||||||
|
|
||||||
Vector3 &operator+=(const Vector3 &b);
|
Vector3 &operator+=(const Vector3 &b);
|
||||||
|
Vector3 operator+(const Vector3 &b) const;
|
||||||
Vector3 &operator-=(const Vector3 &b);
|
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);
|
Vector3 &operator+=(float scalar);
|
||||||
friend Vector3 operator-(Vector3 lhs, const Vector3 &rhs);
|
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);
|
Vector3 operator-() const;
|
||||||
friend Vector3 operator*(Vector3 lhs, const float rhs);
|
|
||||||
|
|
||||||
friend bool operator==(const Vector3 &a, const Vector3 &b);
|
bool operator==(const Vector3 &b) const;
|
||||||
friend bool operator!=(const Vector3 &a, const Vector3 &b);
|
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 x;
|
||||||
float y;
|
float y;
|
||||||
float z;
|
float z;
|
||||||
|
};
|
||||||
|
|
||||||
|
float coordinates[3];
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user