Moved the math class to a new math subfolder in core/, and also added my other math realted classes.

This commit is contained in:
Relintai 2021-11-13 11:51:43 +01:00
parent 29c27ab82a
commit 59e859519b
12 changed files with 819 additions and 1 deletions

View File

@ -5,6 +5,7 @@ Import("env")
env.core_sources = []
env.add_source_files(env.core_sources, "*.cpp")
env.add_source_files(env.core_sources, "./math/*.cpp")
env.add_source_files(env.core_sources, "./containers/*.cpp")
env.add_source_files(env.core_sources, "./log/*.cpp")
env.add_source_files(env.core_sources, "./html/*.cpp")

102
core/math/color.cpp Normal file
View File

@ -0,0 +1,102 @@
#include "color.h"
uint32_t Color::to_key() const {
uint32_t val = 0;
//<< = left shift int típusoknál -> azaz az intek bitjeit eltolja eggyel balra:
//pl.: 001010101 ből lesz: 010101010, aztán 101010100, aztán 010101000 etc
//az r az uint8_t, azaz egy 8 bites int (unsigned byte típus pl java/c#-ban)
//tehát alakítsuk át 32 bites uint-é: static_cast<uint32_t>(r)
//Ez az új c++-os castolás, lehetne így is: (uint32_t) r
//De, nem ajánlják már, mert ez 4-5 ilyen static_cast, dynamic_cast etc függvényt
//próbál végig, megadott sorrendben, és csak kiírtam a megfelelőt expliciten.
//static_cast<uint32_t>(r) << 24: (8db 0) (8db 0) (8db 0) (r száma) Ez lessz: (r száma) (8db 0) (8db 0) (8db 0)
//static_cast<uint32_t>(g) << 24: (8db 0) (8db 0) (8db 0) (g száma) Ez lessz: (8db 0) (g száma) (8db 0) (8db 0)
//static_cast<uint32_t>(b) << 24: (8db 0) (8db 0) (8db 0) (b száma) Ez lessz: (8db 0) (8db 0) (b száma) (8db 0)
//static_cast<uint32_t>(a) << 24: (8db 0) (8db 0) (8db 0) (a száma) Ez lessz: (8db 0) (8db 0) (8db 0) (a száma)
//| a bináris vagy -> a számok birtjeit össze bináris vagyolja -> azaz ahol valaho 1 van, ott az eredmény be egy lesz: 00100 | 00010 -> 00110
//|= -> bináris vagy egyenlő
val |= static_cast<uint32_t>(r) << 24;
val |= static_cast<uint32_t>(g) << 16;
val |= static_cast<uint32_t>(b) << 8;
val |= static_cast<uint32_t>(a) << 0;
//azaz a végeredmény:
//1 uint32t, aminek a bitjei: (r száma) (g száma) (b száma) (a száma)
return val;
}
void Color::from_key(const uint32_t key) {
//A to_key függvény megfordítása.
//& itt a bináris és.
//azok a bitek lesznek 1ek, amik mindkét operandusba 1-ek: 001010 & 111110 -> 001010
//A 16-os számrendszerbeli számokat 0x<valami> formában lehet megadni.
//Azért használtam 16os számrendszerbeli számokat, mert itt sokkal szemléletesebbek.
//Ugyanis-> 0xF = 1111
//Azaz minden F 4db 1es bitet jelöl.
//Így már biztosan látszik, hogy mit csinálunk
//key & 0xFF000000 kiszedjük a legelől levő 8 bitet (Minden más bit biztosan 0 lesz.), etc
r = (key & 0xFF000000) >> 24;
g = (key & 0x00FF0000) >> 16;
b = (key & 0x0000FF00) >> 8;
a = (key & 0x000000FF) >> 0;
//>> az itt a right shift, ugyan az mint a left shift, csak itt a biteket jobbra mozgatjuk
//Nyilván, egyéb módon is meg lehet csinálni ezt a függvényt.
//pl csak right shiftelgetni, és castolni uint8_t-vé az eredményt, etc.
//Azért cisnáltam így, mert szerintem ezt a módszert a leghasznosabb látni hosszú távon.
//Pl. ugyan így lehet hálózati csomagokat méretre optimalizálni.
}
#ifdef SDL_AVAILABLE
SDL_Color Color::to_sdl_color() const {
SDL_Color c;
c.r = r;
c.g = g;
c.b = b;
c.a = a;
return c;
}
void Color::from_sdl_color(const SDL_Color &key) {
r = key.r;
g = key.g;
b = key.b;
a = key.a;
}
#endif
Color::Color() {
r = 0;
g = 0;
b = 0;
a = 255;
}
Color::Color(uint8_t p_r, uint8_t p_g, uint8_t p_b, uint8_t p_a) {
r = p_r;
g = p_g;
b = p_b;
a = p_a;
}
Color::Color(const uint32_t key) {
from_key(key);
}
Color::~Color() {
}

31
core/math/color.h Normal file
View File

@ -0,0 +1,31 @@
#ifndef COLOR_H
#define COLOR_H
#include <inttypes.h>
#ifdef SDL_AVAILABLE
#include <SDL.h>
#endif
class Color {
public:
uint32_t to_key() const;
void from_key(const uint32_t key);
#ifdef SDL_AVAILABLE
SDL_Color to_sdl_color() const;
void from_sdl_color(const SDL_Color &key);
#endif
Color();
Color(uint8_t p_r, uint8_t p_g, uint8_t p_b, uint8_t p_a = 255);
Color(const uint32_t key);
virtual ~Color();
uint8_t r;
uint8_t g;
uint8_t b;
uint8_t a;
};
#endif

180
core/math/rect2.cpp Normal file
View File

@ -0,0 +1,180 @@
#include "rect2.h"
#include "math.h"
float Rect2::get_area() const {
return w * h;
}
bool Rect2::intersects(const Rect2 &b) const {
if (x >= (b.x + b.w))
return false;
if ((x + w) <= b.x)
return false;
if (y >= (b.y + b.h))
return false;
if ((y + h) <= b.y)
return false;
return true;
}
bool Rect2::intersects_include_borders(const Rect2 &b) const {
if (x > (b.x + b.w))
return false;
if ((x + w) < b.x)
return false;
if (y > (b.y + b.h))
return false;
if ((y + h) < b.y)
return false;
return true;
}
bool Rect2::encloses(const Rect2 &b) const {
return (b.x >= x) && (b.y >= y) &&
((b.x + b.w) <= (x + w)) &&
((b.y + b.h) <= (y + h));
}
bool Rect2::has_no_area() const {
if (w == 0 && h == 0) {
return true;
}
return false;
}
bool Rect2::has_point(const float px, const float py) const {
if (px > x && px < x + w && py > y && py < py + h) {
return true;
}
return false;
}
bool Rect2::is_equal_approx(const Rect2 &b) const {
if (x + EPSILON < b.x && x - EPSILON > b.x && y + EPSILON < b.y && y - EPSILON > b.y &&
w + EPSILON < b.w && w - EPSILON > b.w && h + EPSILON < b.h && h - EPSILON > b.h) {
return true;
}
return false;
}
void Rect2::grow(const float by) {
x -= by;
y -= by;
h += by;
w += by;
}
void Rect2::shrink(const float by) {
x += by;
y += by;
h -= by;
w -= by;
}
#ifdef SDL_AVAILABLE
SDL_Rect Rect2::as_rect() const {
SDL_Rect r;
r.x = x;
r.y = y;
r.w = w;
r.h = h;
return r;
}
SDL_FRect Rect2::as_frect() const {
SDL_FRect r;
r.x = x;
r.y = y;
r.w = w;
r.h = h;
return r;
}
#endif
Rect2 &Rect2::operator+=(const Rect2 &b) {
x += b.x;
y += b.y;
w += b.w;
h += b.h;
return *this;
}
Rect2 &Rect2::operator-=(const Rect2 &b) {
x -= b.x;
y -= b.y;
w -= b.w;
h -= b.h;
return *this;
}
Rect2 operator+(Rect2 lhs, const Rect2 &rhs) {
lhs.x += rhs.x;
lhs.y += rhs.y;
lhs.w += rhs.w;
lhs.h += rhs.h;
return lhs;
}
Rect2 operator-(Rect2 lhs, const Rect2 &rhs) {
lhs.x -= rhs.x;
lhs.y -= rhs.y;
lhs.w -= rhs.w;
lhs.h -= rhs.h;
return lhs;
}
bool operator==(const Rect2 &a, const Rect2 &b) {
return a.is_equal_approx(b);
}
bool operator!=(const Rect2 &a, const Rect2 &b) {
return !a.is_equal_approx(b);
}
Rect2::Rect2() {
x = 0;
y = 0;
w = 0;
h = 0;
}
Rect2::Rect2(const Rect2 &b) {
x = b.x;
y = b.y;
w = b.w;
h = b.h;
}
Rect2::Rect2(const float rx, const float ry) {
x = rx;
y = ry;
w = 0;
h = 0;
}
Rect2::Rect2(const float rx, const float ry, const float rw, const float rh) {
x = rx;
y = ry;
w = rw;
h = rh;
}

46
core/math/rect2.h Normal file
View File

@ -0,0 +1,46 @@
#ifndef RECT2_H
#define RECT2_H
#ifdef SDL_AVAILABLE
#include <SDL.h>
#endif
class Rect2 {
public:
float get_area() const;
bool intersects(const Rect2 &b) const;
bool intersects_include_borders(const Rect2 &b) const;
bool encloses(const Rect2 &b) const;
bool has_no_area() const;
bool has_point(const float px, const float py) const;
bool is_equal_approx(const Rect2 &b) const;
void grow(const float by);
void shrink(const float by);
#ifdef SDL_AVAILABLE
SDL_Rect as_rect() const;
SDL_FRect as_frect() const;
#endif
Rect2 &operator+=(const Rect2 &b);
Rect2 &operator-=(const Rect2 &b);
friend Rect2 operator+(Rect2 lhs, const Rect2 &rhs);
friend Rect2 operator-(Rect2 lhs, const Rect2 &rhs);
friend bool operator==(const Rect2 &a, const Rect2 &b);
friend bool operator!=(const Rect2 &a, const Rect2 &b);
Rect2();
Rect2(const Rect2 &b);
Rect2(const float rx, const float ry);
Rect2(const float rx, const float ry, const float rw, const float rh);
float x;
float y;
float w;
float h;
};
#endif

172
core/math/vector2.cpp Executable file
View File

@ -0,0 +1,172 @@
#include "vector2.h"
#include <cmath>
#define EPSILON 0.00001
Vector2 Vector2::abs() const {
Vector2 b;
b.x = x >= 0 ? x : -x;
b.y = y >= 0 ? y : -y;
return b;
}
float Vector2::angle() const {
return atan2(x, y);
}
float Vector2::angle_to(const Vector2 &b) const {
return atan2(cross(b), dot(b));
}
float Vector2::cross(const Vector2 &b) const {
return x * b.y - y * b.x;
}
Vector2 Vector2::clamped(float len) const {
return normalized() * len;
}
Vector2 Vector2::direction_to(const Vector2 &b) const {
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);
}
float Vector2::distance_to(const Vector2 &b) const {
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;
}
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;
}
return false;
}
float Vector2::length() const {
return sqrt(x * x + y * y);
}
float Vector2::length_squared() const {
return x * x + y * y;
}
Vector2 Vector2::lerp(const Vector2 &b, const float t) const {
Vector2 v;
v.x = x + (t * (b.x - x));
v.y = y + (t * (b.y - y));
return v;
}
Vector2 Vector2::normalized() const {
Vector2 v;
float l = length_squared();
if (l != 0) {
l = sqrt(l);
v.x = x / l;
v.y = y / l;
}
return v;
}
void Vector2::normalize() {
float l = length_squared();
if (l != 0) {
l = sqrt(l);
x = x / l;
y = y / l;
}
}
void Vector2::add(const Vector2 &b) {
x += b.x;
y += b.y;
}
void Vector2::sub(const Vector2 &b) {
x -= b.x;
y -= b.y;
}
Vector2::Vector2() {
x = 0;
y = 0;
}
Vector2::Vector2(const Vector2 &b) {
x = b.x;
y = b.y;
}
Vector2::Vector2(const float p_x, const float p_y) {
x = p_x;
y = p_y;
}
Vector2 &Vector2::operator+=(const Vector2 &b) {
x += b.x;
y += b.y;
return *this;
}
Vector2 &Vector2::operator-=(const Vector2 &b) {
x -= b.x;
y -= b.y;
return *this;
}
Vector2 &Vector2::operator*=(const float b) {
x *= b;
y *= b;
return *this;
}
Vector2 operator*(Vector2 lhs, const float rhs) {
lhs.x *= rhs;
lhs.y *= rhs;
return lhs;
}
Vector2 operator+(Vector2 lhs, const Vector2 &rhs) {
lhs.x += rhs.x;
lhs.y += rhs.y;
return lhs;
}
Vector2 operator-(Vector2 lhs, const Vector2 &rhs) {
lhs.x -= rhs.x;
lhs.y -= rhs.y;
return lhs;
}
bool operator==(const Vector2 &a, const Vector2 &b) {
return a.is_equal_approx(b);
}
bool operator!=(const Vector2 &a, const Vector2 &b) {
return !(a == b);
}

48
core/math/vector2.h Executable file
View File

@ -0,0 +1,48 @@
#ifndef VECTOR2_H
#define VECTOR2_H
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;
float length() const;
float length_squared() const;
Vector2 lerp(const Vector2 &b, const float t) const;
Vector2 normalized() const;
void normalize();
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 &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);
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);
float x;
float y;
};
#endif

190
core/math/vector3.cpp Executable file
View File

@ -0,0 +1,190 @@
#include "vector3.h"
#include <cmath>
#define EPSILON 0.00001
Vector3 Vector3::abs() const {
Vector3 b;
b.x = x >= 0 ? x : -x;
b.y = y >= 0 ? y : -y;
b.z = z >= 0 ? z : -z;
return b;
}
float Vector3::angle_to(const Vector3 &b) const {
return atan2(cross(b).length(), dot(b));
}
Vector3 Vector3::cross(const Vector3 &b) const {
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);
return v;
}
Vector3 Vector3::clamped(float len) const {
return normalized() * len;
}
Vector3 Vector3::direction_to(const Vector3 &b) const {
return (b - *this).normalized();
}
float Vector3::distance_to_squared(const Vector3 &b) const {
return (b - *this).length_squared();
}
float Vector3::distance_to(const Vector3 &b) const {
return (b - *this).length();
}
float Vector3::dot(const Vector3 &b) const {
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;
}
return false;
}
float Vector3::length() const {
return sqrt(x * x + y * y + z * z);
}
float Vector3::length_squared() const {
return x * x + y * y + z * z;
}
Vector3 Vector3::lerp(const Vector3 &b, const float t) const {
Vector3 v;
v.x = x + (t * (b.x - x));
v.y = y + (t * (b.y - y));
v.z = z + (t * (b.z - z));
return v;
}
Vector3 Vector3::normalized() const {
Vector3 v;
float l = length_squared();
if (l != 0) {
l = sqrt(l);
v.x = x / l;
v.y = y / l;
v.z = z / l;
}
return v;
}
void Vector3::normalize() {
float l = length_squared();
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;
}
void Vector3::sub(const Vector3 &b) {
x -= b.x;
y -= b.y;
z -= b.z;
}
Vector3::Vector3() {
x = 0;
y = 0;
z = 0;
}
Vector3::Vector3(const Vector3 &b) {
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;
}
Vector3 &Vector3::operator+=(const Vector3 &b) {
x += b.x;
y += b.y;
z += b.z;
return *this;
}
Vector3 &Vector3::operator-=(const Vector3 &b) {
x -= b.x;
y -= b.y;
z -= b.z;
return *this;
}
Vector3 &Vector3::operator*=(const float b) {
x *= b;
y *= b;
z *= b;
return *this;
}
Vector3 operator*(Vector3 lhs, const float rhs) {
lhs.x *= rhs;
lhs.y *= rhs;
lhs.z *= rhs;
return lhs;
}
Vector3 operator+(Vector3 lhs, const Vector3 &rhs) {
lhs.x += rhs.x;
lhs.y += rhs.y;
lhs.z += rhs.z;
return lhs;
}
Vector3 operator-(Vector3 lhs, const Vector3 &rhs) {
lhs.x -= rhs.x;
lhs.y -= rhs.y;
lhs.z -= rhs.z;
return lhs;
}
bool operator==(const Vector3 &a, const Vector3 &b) {
return a.is_equal_approx(b);
}
bool operator!=(const Vector3 &a, const Vector3 &b) {
return !(a == b);
}

48
core/math/vector3.h Executable file
View File

@ -0,0 +1,48 @@
#ifndef VECTOR3_H
#define VECTOR3_H
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;
float length() const;
float length_squared() const;
Vector3 lerp(const Vector3 &b, const float t) const;
Vector3 normalized() const;
void normalize();
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 &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);
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);
float x;
float y;
float z;
};
#endif

View File

@ -1,6 +1,6 @@
#include "string.h"
#include "core/math.h"
#include "core/math/math.h"
#include "error_macros.h"
#include <stdlib.h>
#include <cstdio>