Format Vector2 and Vector3.

This commit is contained in:
Relintai 2021-03-27 13:44:06 +01:00
parent 10fbdf2f76
commit d0f267f69f
4 changed files with 213 additions and 215 deletions

View File

@ -5,169 +5,168 @@
#define EPSILON 0.00001
Vector2 Vector2::abs() const {
Vector2 b;
Vector2 b;
b.x = x >= 0 ? x : -x;
b.y = y >= 0 ? y : -y;
b.x = x >= 0 ? x : -x;
b.y = y >= 0 ? y : -y;
return b;
return b;
}
float Vector2::angle() const {
return atan2(x, y);
return atan2(x, y);
}
float Vector2::angle_to(const Vector2 &b) const {
return atan2(cross(b), dot(b));
return atan2(cross(b), dot(b));
}
float Vector2::cross(const Vector2 &b) const {
return x * b.y - y * b.x;
return x * b.y - y * b.x;
}
Vector2 Vector2::clamped(float len) const {
return normalized() * len;
return normalized() * len;
}
Vector2 Vector2::direction_to(const Vector2 &b) const {
return (b - *this).normalized();
return (b - *this).normalized();
}
float Vector2::distance_to_squared(const Vector2 &b) const {
return (x - b.x) * (x - b.x) + (y - b.y) * (y - b.y);
return (x - b.x) * (x - b.x) + (y - b.y) * (y - b.y);
}
float Vector2::distance_to(const Vector2 &b) const {
return sqrt((x - b.x) * (x - b.x) + (y - b.y) * (y - b.y));
return sqrt((x - b.x) * (x - b.x) + (y - b.y) * (y - b.y));
}
float Vector2::dot(const Vector2 &b) const {
return x * b.x + y * b.y;
return x * b.x + y * b.y;
}
bool Vector2::is_equal_approx(const Vector2 &b) const {
if (x + EPSILON < b.x && x - EPSILON > b.x && y + EPSILON < b.y && y - EPSILON > b.y) {
return true;
}
if (x + EPSILON < b.x && x - EPSILON > b.x && y + EPSILON < b.y && y - EPSILON > b.y) {
return true;
}
return false;
return false;
}
float Vector2::length() const {
return sqrt(x * x + y * y);
return sqrt(x * x + y * y);
}
float Vector2::length_squared() const {
return x * x + y * y;
return x * x + y * y;
}
Vector2 Vector2::lerp(const Vector2 &b, const float t) const {
Vector2 v;
Vector2 v;
v.x = x + (t * (b.x - x));
v.y = y + (t * (b.y - y));
v.x = x + (t * (b.x - x));
v.y = y + (t * (b.y - y));
return v;
return v;
}
Vector2 Vector2::normalized() const {
Vector2 v;
Vector2 v;
float l = length_squared();
float l = length_squared();
if (l != 0) {
l = sqrt(l);
if (l != 0) {
l = sqrt(l);
v.x = x / l;
v.y = y / l;
}
v.x = x / l;
v.y = y / l;
}
return v;
return v;
}
void Vector2::normalize() {
float l = length_squared();
float l = length_squared();
if (l != 0) {
l = sqrt(l);
x = x / l;
y = y / l;
}
if (l != 0) {
l = sqrt(l);
x = x / l;
y = y / l;
}
}
void Vector2::add(const Vector2 &b) {
x += b.x;
y += b.y;
x += b.x;
y += b.y;
}
void Vector2::sub(const Vector2 &b) {
x -= b.x;
y -= b.y;
x -= b.x;
y -= b.y;
}
Vector2::Vector2() {
x = 0;
y = 0;
x = 0;
y = 0;
}
Vector2::Vector2(const Vector2 &b) {
x = b.x;
y = b.y;
x = b.x;
y = b.y;
}
Vector2::Vector2(const float p_x, const float p_y) {
x = p_x;
y = p_y;
x = p_x;
y = p_y;
}
Vector2 &Vector2::operator+=(const Vector2 &b) {
x += b.x;
y += b.y;
x += b.x;
y += b.y;
return *this;
return *this;
}
Vector2 &Vector2::operator-=(const Vector2 &b) {
x -= b.x;
y -= b.y;
x -= b.x;
y -= b.y;
return *this;
return *this;
}
Vector2& Vector2::operator*=(const float b) {
x *= b;
y *= b;
Vector2 &Vector2::operator*=(const float b) {
x *= b;
y *= b;
return *this;
return *this;
}
Vector2 operator*(Vector2 lhs, const float rhs) {
lhs.x *= rhs;
lhs.y *= rhs;
lhs.x *= rhs;
lhs.y *= rhs;
return lhs;
return lhs;
}
Vector2 operator+(Vector2 lhs, const Vector2 &rhs) {
lhs.x += rhs.x;
lhs.y += rhs.y;
lhs.x += rhs.x;
lhs.y += rhs.y;
return lhs;
return lhs;
}
Vector2 operator-(Vector2 lhs, const Vector2 &rhs) {
lhs.x -= rhs.x;
lhs.y -= rhs.y;
lhs.x -= rhs.x;
lhs.y -= rhs.y;
return lhs;
return lhs;
}
bool operator==(const Vector2 &a, const Vector2 &b) {
return a.is_equal_approx(b);
return a.is_equal_approx(b);
}
bool operator!=(const Vector2 &a, const Vector2 &b) {
return !(a == b);
return !(a == b);
}

View File

@ -3,46 +3,46 @@
class Vector2 {
public:
Vector2 abs() const;
float angle() const;
float angle_to(const Vector2 &b) const;
float cross(const Vector2 &b) const;
Vector2 clamped(float len) const;
Vector2 direction_to(const Vector2 &b) const;
float distance_to_squared(const Vector2 &b) const;
float distance_to(const Vector2 &b) const;
float dot(const Vector2 &b) const;
bool is_equal_approx(const Vector2 &b) const;
Vector2 abs() const;
float angle() const;
float angle_to(const Vector2 &b) const;
float cross(const Vector2 &b) const;
Vector2 clamped(float len) const;
Vector2 direction_to(const Vector2 &b) const;
float distance_to_squared(const Vector2 &b) const;
float distance_to(const Vector2 &b) const;
float dot(const Vector2 &b) const;
bool is_equal_approx(const Vector2 &b) const;
float length() const;
float length_squared() const;
float length() const;
float length_squared() const;
Vector2 lerp(const Vector2 &b, const float t) const;
Vector2 lerp(const Vector2 &b, const float t) const;
Vector2 normalized() const;
void normalize();
Vector2 normalized() const;
void normalize();
void add(const Vector2 &b);
void sub(const Vector2 &b);
void add(const Vector2 &b);
void sub(const Vector2 &b);
Vector2();
Vector2(const Vector2 &b);
Vector2(const float p_x, const float p_y);
Vector2();
Vector2(const Vector2 &b);
Vector2(const float p_x, const float p_y);
Vector2& operator+=(const Vector2& b);
Vector2& operator-=(const Vector2& b);
Vector2 &operator+=(const Vector2 &b);
Vector2 &operator-=(const Vector2 &b);
friend Vector2 operator+(Vector2 lhs, const Vector2 &rhs);
friend Vector2 operator-(Vector2 lhs, const Vector2 &rhs);
friend Vector2 operator+(Vector2 lhs, const Vector2 &rhs);
friend Vector2 operator-(Vector2 lhs, const Vector2 &rhs);
Vector2& operator*=(const float b);
friend Vector2 operator*(Vector2 lhs, const float rhs);
Vector2 &operator*=(const float b);
friend Vector2 operator*(Vector2 lhs, const float rhs);
friend bool operator==(const Vector2& a, const Vector2& b);
friend bool operator!=(const Vector2& a, const Vector2& b);
friend bool operator==(const Vector2 &a, const Vector2 &b);
friend bool operator!=(const Vector2 &a, const Vector2 &b);
float x;
float y;
float x;
float y;
};
#endif

View File

@ -5,187 +5,186 @@
#define EPSILON 0.00001
Vector3 Vector3::abs() const {
Vector3 b;
Vector3 b;
b.x = x >= 0 ? x : -x;
b.y = y >= 0 ? y : -y;
b.z = z >= 0 ? z : -z;
b.x = x >= 0 ? x : -x;
b.y = y >= 0 ? y : -y;
b.z = z >= 0 ? z : -z;
return b;
return b;
}
float Vector3::angle_to(const Vector3 &b) const {
return atan2(cross(b).length(), dot(b));
return atan2(cross(b).length(), dot(b));
}
Vector3 Vector3::cross(const Vector3 &b) const {
Vector3 v;
Vector3 v;
v.x = (y * b.z) - (z * b.y);
v.y = (z * b.x) - (x * b.z);
v.z = (x * b.y) - (y * b.x);
v.x = (y * b.z) - (z * b.y);
v.y = (z * b.x) - (x * b.z);
v.z = (x * b.y) - (y * b.x);
return v;
return v;
}
Vector3 Vector3::clamped(float len) const {
return normalized() * len;
return normalized() * len;
}
Vector3 Vector3::direction_to(const Vector3 &b) const {
return (b - *this).normalized();
return (b - *this).normalized();
}
float Vector3::distance_to_squared(const Vector3 &b) const {
return (b - *this).length_squared();
return (b - *this).length_squared();
}
float Vector3::distance_to(const Vector3 &b) const {
return (b - *this).length();
return (b - *this).length();
}
float Vector3::dot(const Vector3 &b) const {
return x * b.x + y * b.y + z * b.z;
return x * b.x + y * b.y + z * b.z;
}
bool Vector3::is_equal_approx(const Vector3 &b) const {
if (x + EPSILON < b.x && x - EPSILON > b.x && y + EPSILON < b.y && y - EPSILON > b.y &&
z + EPSILON < b.z && z - EPSILON > b.z) {
return true;
}
if (x + EPSILON < b.x && x - EPSILON > b.x && y + EPSILON < b.y && y - EPSILON > b.y &&
z + EPSILON < b.z && z - EPSILON > b.z) {
return true;
}
return false;
return false;
}
float Vector3::length() const {
return sqrt(x * x + y * y + z * z);
return sqrt(x * x + y * y + z * z);
}
float Vector3::length_squared() const {
return x * x + y * y + z * z;
return x * x + y * y + z * z;
}
Vector3 Vector3::lerp(const Vector3 &b, const float t) const {
Vector3 v;
Vector3 v;
v.x = x + (t * (b.x - x));
v.y = y + (t * (b.y - y));
v.z = z + (t * (b.z - z));
v.x = x + (t * (b.x - x));
v.y = y + (t * (b.y - y));
v.z = z + (t * (b.z - z));
return v;
return v;
}
Vector3 Vector3::normalized() const {
Vector3 v;
Vector3 v;
float l = length_squared();
float l = length_squared();
if (l != 0) {
l = sqrt(l);
if (l != 0) {
l = sqrt(l);
v.x = x / l;
v.y = y / l;
v.z = z / l;
}
v.x = x / l;
v.y = y / l;
v.z = z / l;
}
return v;
return v;
}
void Vector3::normalize() {
float l = length_squared();
float l = length_squared();
if (l != 0) {
l = sqrt(l);
x = x / l;
y = y / l;
z = z / l;
}
if (l != 0) {
l = sqrt(l);
x = x / l;
y = y / l;
z = z / l;
}
}
void Vector3::add(const Vector3 &b) {
x += b.x;
y += b.y;
z += b.z;
x += b.x;
y += b.y;
z += b.z;
}
void Vector3::sub(const Vector3 &b) {
x -= b.x;
y -= b.y;
z -= b.z;
x -= b.x;
y -= b.y;
z -= b.z;
}
Vector3::Vector3() {
x = 0;
y = 0;
z = 0;
x = 0;
y = 0;
z = 0;
}
Vector3::Vector3(const Vector3 &b) {
x = b.x;
y = b.y;
z = b.z;
x = b.x;
y = b.y;
z = b.z;
}
Vector3::Vector3(const float p_x, const float p_y, const float p_z) {
x = p_x;
y = p_y;
z = p_z;
x = p_x;
y = p_y;
z = p_z;
}
Vector3 &Vector3::operator+=(const Vector3 &b) {
x += b.x;
y += b.y;
z += b.z;
x += b.x;
y += b.y;
z += b.z;
return *this;
return *this;
}
Vector3 &Vector3::operator-=(const Vector3 &b) {
x -= b.x;
y -= b.y;
z -= b.z;
x -= b.x;
y -= b.y;
z -= b.z;
return *this;
return *this;
}
Vector3& Vector3::operator*=(const float b) {
x *= b;
y *= b;
z *= b;
Vector3 &Vector3::operator*=(const float b) {
x *= b;
y *= b;
z *= b;
return *this;
return *this;
}
Vector3 operator*(Vector3 lhs, const float rhs) {
lhs.x *= rhs;
lhs.y *= rhs;
lhs.z *= rhs;
lhs.x *= rhs;
lhs.y *= rhs;
lhs.z *= rhs;
return lhs;
return lhs;
}
Vector3 operator+(Vector3 lhs, const Vector3 &rhs) {
lhs.x += rhs.x;
lhs.y += rhs.y;
lhs.z += rhs.z;
lhs.x += rhs.x;
lhs.y += rhs.y;
lhs.z += rhs.z;
return lhs;
return lhs;
}
Vector3 operator-(Vector3 lhs, const Vector3 &rhs) {
lhs.x -= rhs.x;
lhs.y -= rhs.y;
lhs.z -= rhs.z;
lhs.x -= rhs.x;
lhs.y -= rhs.y;
lhs.z -= rhs.z;
return lhs;
return lhs;
}
bool operator==(const Vector3 &a, const Vector3 &b) {
return a.is_equal_approx(b);
return a.is_equal_approx(b);
}
bool operator!=(const Vector3 &a, const Vector3 &b) {
return !(a == b);
return !(a == b);
}

View File

@ -3,46 +3,46 @@
class Vector3 {
public:
Vector3 abs() const;
float angle_to(const Vector3 &b) const;
Vector3 cross(const Vector3 &b) const;
Vector3 clamped(float len) const;
Vector3 direction_to(const Vector3 &b) const;
float distance_to_squared(const Vector3 &b) const;
float distance_to(const Vector3 &b) const;
float dot(const Vector3 &b) const;
bool is_equal_approx(const Vector3 &b) const;
Vector3 abs() const;
float angle_to(const Vector3 &b) const;
Vector3 cross(const Vector3 &b) const;
Vector3 clamped(float len) const;
Vector3 direction_to(const Vector3 &b) const;
float distance_to_squared(const Vector3 &b) const;
float distance_to(const Vector3 &b) const;
float dot(const Vector3 &b) const;
bool is_equal_approx(const Vector3 &b) const;
float length() const;
float length_squared() const;
float length() const;
float length_squared() const;
Vector3 lerp(const Vector3 &b, const float t) const;
Vector3 lerp(const Vector3 &b, const float t) const;
Vector3 normalized() const;
void normalize();
Vector3 normalized() const;
void normalize();
void add(const Vector3 &b);
void sub(const Vector3 &b);
void add(const Vector3 &b);
void sub(const Vector3 &b);
Vector3();
Vector3(const Vector3 &b);
Vector3(const float p_x, const float p_y, const float p_z);
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);
Vector3 &operator+=(const Vector3 &b);
Vector3 &operator-=(const Vector3 &b);
friend Vector3 operator+(Vector3 lhs, const Vector3 &rhs);
friend Vector3 operator-(Vector3 lhs, const Vector3 &rhs);
friend Vector3 operator+(Vector3 lhs, const Vector3 &rhs);
friend Vector3 operator-(Vector3 lhs, const Vector3 &rhs);
Vector3& operator*=(const float b);
friend Vector3 operator*(Vector3 lhs, const float rhs);
Vector3 &operator*=(const float b);
friend Vector3 operator*(Vector3 lhs, const float rhs);
friend bool operator==(const Vector3& a, const Vector3& b);
friend bool operator!=(const Vector3& a, const Vector3& b);
friend bool operator==(const Vector3 &a, const Vector3 &b);
friend bool operator!=(const Vector3 &a, const Vector3 &b);
float x;
float y;
float z;
float x;
float y;
float z;
};
#endif