mirror of
https://github.com/Relintai/programming_tutorials.git
synced 2025-04-21 21:51:22 +02:00
Vector2, 3, and Rect2 now uses Math.
This commit is contained in:
parent
e3e3c1fec6
commit
9f9a534f22
@ -10,6 +10,9 @@ fi
|
||||
#-Iinclude
|
||||
|
||||
g++ -Wall -g -c math.cpp -o obj/math.o
|
||||
g++ -Wall -g -c vector2.cpp -o obj/vector2.o
|
||||
g++ -Wall -g -c vector3.cpp -o obj/vector3.o
|
||||
g++ -Wall -g -c rect2.cpp -o obj/rect2.o
|
||||
g++ -Wall -g -c main.cpp -o obj/main.o
|
||||
g++ -o bin/program obj/math.o obj/main.o
|
||||
|
||||
|
156
02_math_osztaly/rect2.cpp
Normal file
156
02_math_osztaly/rect2.cpp
Normal file
@ -0,0 +1,156 @@
|
||||
#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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
37
02_math_osztaly/rect2.h
Normal file
37
02_math_osztaly/rect2.h
Normal file
@ -0,0 +1,37 @@
|
||||
#ifndef RECT2_H
|
||||
#define RECT2_H
|
||||
|
||||
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);
|
||||
|
||||
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
|
170
02_math_osztaly/vector2.cpp
Executable file
170
02_math_osztaly/vector2.cpp
Executable file
@ -0,0 +1,170 @@
|
||||
#include "vector2.h"
|
||||
|
||||
#include "math.h"
|
||||
|
||||
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 Math::atan2(x, y);
|
||||
}
|
||||
|
||||
float Vector2::angle_to(const Vector2 &b) const {
|
||||
return Math::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
02_math_osztaly/vector2.h
Executable file
48
02_math_osztaly/vector2.h
Executable 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
|
188
02_math_osztaly/vector3.cpp
Executable file
188
02_math_osztaly/vector3.cpp
Executable file
@ -0,0 +1,188 @@
|
||||
#include "vector3.h"
|
||||
|
||||
#include "math.h"
|
||||
|
||||
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 Math::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
02_math_osztaly/vector3.h
Executable file
48
02_math_osztaly/vector3.h
Executable 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
|
Loading…
Reference in New Issue
Block a user