mirror of
https://github.com/Relintai/rcpp_framework.git
synced 2024-11-10 00:52:11 +01:00
Added Vector2i.
This commit is contained in:
parent
bfa4a96c2a
commit
202df3bac6
181
core/math/vector2i.cpp
Executable file
181
core/math/vector2i.cpp
Executable file
@ -0,0 +1,181 @@
|
||||
#include "vector2i.h"
|
||||
|
||||
#include <cmath>
|
||||
|
||||
Vector2i Vector2i::abs() const {
|
||||
Vector2i b;
|
||||
|
||||
b.x = x >= 0 ? x : -x;
|
||||
b.y = y >= 0 ? y : -y;
|
||||
|
||||
return b;
|
||||
}
|
||||
|
||||
float Vector2i::angle() const {
|
||||
return atan2(x, y);
|
||||
}
|
||||
|
||||
float Vector2i::angle_to(const Vector2i &b) const {
|
||||
return atan2(cross(b), dot(b));
|
||||
}
|
||||
|
||||
float Vector2i::cross(const Vector2i &b) const {
|
||||
return x * b.y - y * b.x;
|
||||
}
|
||||
|
||||
float Vector2i::distance_to_squared(const Vector2i &b) const {
|
||||
return (x - b.x) * (x - b.x) + (y - b.y) * (y - b.y);
|
||||
}
|
||||
|
||||
float Vector2i::distance_to(const Vector2i &b) const {
|
||||
return sqrt((x - b.x) * (x - b.x) + (y - b.y) * (y - b.y));
|
||||
}
|
||||
|
||||
float Vector2i::dot(const Vector2i &b) const {
|
||||
return x * b.x + y * b.y;
|
||||
}
|
||||
|
||||
float Vector2i::length() const {
|
||||
return sqrt(x * x + y * y);
|
||||
}
|
||||
|
||||
float Vector2i::length_squared() const {
|
||||
return x * x + y * y;
|
||||
}
|
||||
|
||||
Vector2i Vector2i::lerp(const Vector2i &b, const float t) const {
|
||||
Vector2i v;
|
||||
|
||||
v.x = x + (t * (b.x - x));
|
||||
v.y = y + (t * (b.y - y));
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
void Vector2i::add(const Vector2i &b) {
|
||||
x += b.x;
|
||||
y += b.y;
|
||||
}
|
||||
|
||||
void Vector2i::sub(const Vector2i &b) {
|
||||
x -= b.x;
|
||||
y -= b.y;
|
||||
}
|
||||
|
||||
Vector2i::Vector2i() {
|
||||
x = 0;
|
||||
y = 0;
|
||||
}
|
||||
|
||||
Vector2i::Vector2i(const Vector2i &b) {
|
||||
x = b.x;
|
||||
y = b.y;
|
||||
}
|
||||
|
||||
Vector2i::Vector2i(const int p_x, const int p_y) {
|
||||
x = p_x;
|
||||
y = p_y;
|
||||
}
|
||||
|
||||
Vector2i &Vector2i::operator+=(const Vector2i &b) {
|
||||
x += b.x;
|
||||
y += b.y;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vector2i Vector2i::operator+(const Vector2i &b) const {
|
||||
return Vector2i(x + b.x, y + b.y);
|
||||
}
|
||||
|
||||
Vector2i &Vector2i::operator-=(const Vector2i &b) {
|
||||
x -= b.x;
|
||||
y -= b.y;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vector2i Vector2i::operator-(const Vector2i &b) const {
|
||||
return Vector2i(x - b.x, y - b.y);
|
||||
}
|
||||
|
||||
Vector2i &Vector2i::operator*=(const Vector2i &b) {
|
||||
x *= b.x;
|
||||
y *= b.y;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vector2i Vector2i::operator*(const Vector2i &b) const {
|
||||
return Vector2i(x * b.x, y * b.y);
|
||||
}
|
||||
|
||||
Vector2i &Vector2i::operator/=(const Vector2i &b) {
|
||||
x /= b.x;
|
||||
y /= b.y;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vector2i Vector2i::operator/(const Vector2i &b) const {
|
||||
return Vector2i(x / b.x, y / b.y);
|
||||
}
|
||||
|
||||
Vector2i &Vector2i::operator+=(int scalar) {
|
||||
x += scalar;
|
||||
y += scalar;
|
||||
|
||||
return *this;
|
||||
}
|
||||
Vector2i Vector2i::operator+(int scalar) const {
|
||||
return Vector2i(x + scalar, y + scalar);
|
||||
}
|
||||
Vector2i &Vector2i::operator-=(int scalar) {
|
||||
x += scalar;
|
||||
y += scalar;
|
||||
|
||||
return *this;
|
||||
}
|
||||
Vector2i Vector2i::operator-(int scalar) const {
|
||||
return Vector2i(x - scalar, y - scalar);
|
||||
}
|
||||
|
||||
Vector2i &Vector2i::operator*=(int scalar) {
|
||||
x *= scalar;
|
||||
y *= scalar;
|
||||
|
||||
return *this;
|
||||
}
|
||||
Vector2i Vector2i::operator*(int scalar) const {
|
||||
return Vector2i(x * scalar, y * scalar);
|
||||
}
|
||||
Vector2i &Vector2i::operator/=(int scalar) {
|
||||
x /= scalar;
|
||||
y /= scalar;
|
||||
|
||||
return *this;
|
||||
}
|
||||
Vector2i Vector2i::operator/(int scalar) const {
|
||||
return Vector2i(x / scalar, y / scalar);
|
||||
}
|
||||
Vector2i Vector2i::operator-() const {
|
||||
return Vector2i(-x, -y);
|
||||
}
|
||||
|
||||
bool Vector2i::operator==(const Vector2i &b) const {
|
||||
return x == b.x && y == b.y;
|
||||
}
|
||||
bool Vector2i::operator!=(const Vector2i &b) const {
|
||||
return x != b.x || y != b.y;
|
||||
}
|
||||
|
||||
const int &Vector2i::operator[](int axis) const {
|
||||
return coordinates[axis];
|
||||
}
|
||||
int &Vector2i::operator[](int axis) {
|
||||
return coordinates[axis];
|
||||
}
|
||||
|
||||
Vector2i::operator String() const {
|
||||
return "[" + String::num(x) + "," + String::num(y) + "]";
|
||||
}
|
71
core/math/vector2i.h
Executable file
71
core/math/vector2i.h
Executable file
@ -0,0 +1,71 @@
|
||||
#ifndef VECTOR2I_H
|
||||
#define VECTOR2I_H
|
||||
|
||||
#include "core/string.h"
|
||||
|
||||
class Vector2i {
|
||||
public:
|
||||
Vector2i abs() const;
|
||||
float angle() const;
|
||||
float angle_to(const Vector2i &b) const;
|
||||
float cross(const Vector2i &b) const;
|
||||
float distance_to_squared(const Vector2i &b) const;
|
||||
float distance_to(const Vector2i &b) const;
|
||||
float dot(const Vector2i &b) const;
|
||||
|
||||
float length() const;
|
||||
float length_squared() const;
|
||||
|
||||
Vector2i lerp(const Vector2i &b, const float t) const;
|
||||
|
||||
void add(const Vector2i &b);
|
||||
void sub(const Vector2i &b);
|
||||
|
||||
Vector2i();
|
||||
Vector2i(const Vector2i &b);
|
||||
Vector2i(const int p_x, const int p_y);
|
||||
|
||||
Vector2i &operator+=(const Vector2i &b);
|
||||
Vector2i operator+(const Vector2i &b) const;
|
||||
Vector2i &operator-=(const Vector2i &b);
|
||||
Vector2i operator-(const Vector2i &b) const;
|
||||
Vector2i &operator*=(const Vector2i &b);
|
||||
Vector2i operator*(const Vector2i &b) const;
|
||||
Vector2i &operator/=(const Vector2i &b);
|
||||
Vector2i operator/(const Vector2i &b) const;
|
||||
|
||||
Vector2i &operator+=(int scalar);
|
||||
Vector2i operator+(int scalar) const;
|
||||
Vector2i &operator-=(int scalar);
|
||||
Vector2i operator-(int scalar) const;
|
||||
Vector2i &operator*=(int scalar);
|
||||
Vector2i operator*(int scalar) const;
|
||||
Vector2i &operator/=(int scalar);
|
||||
Vector2i operator/(int scalar) const;
|
||||
|
||||
Vector2i operator-() const;
|
||||
|
||||
bool operator==(const Vector2i &b) const;
|
||||
bool operator!=(const Vector2i &b) const;
|
||||
|
||||
const int &operator[](int axis) const;
|
||||
int &operator[](int axis);
|
||||
|
||||
operator String() const;
|
||||
|
||||
enum Axis {
|
||||
AXIS_X = 0,
|
||||
AXIS_Y
|
||||
};
|
||||
|
||||
union {
|
||||
struct {
|
||||
int x;
|
||||
int y;
|
||||
};
|
||||
|
||||
int coordinates[2];
|
||||
};
|
||||
};
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user