mirror of
https://github.com/Relintai/programming_tutorials.git
synced 2025-04-21 21:51:22 +02:00
Finished Vector2 and implemented Vector3. Small fixes to the alapok txt.
This commit is contained in:
parent
0a945d0ace
commit
10fbdf2f76
56
.vscode/settings.json
vendored
Executable file
56
.vscode/settings.json
vendored
Executable file
@ -0,0 +1,56 @@
|
||||
{
|
||||
"files.associations": {
|
||||
"array": "cpp",
|
||||
"atomic": "cpp",
|
||||
"hash_map": "cpp",
|
||||
"bit": "cpp",
|
||||
"*.tcc": "cpp",
|
||||
"cctype": "cpp",
|
||||
"cinttypes": "cpp",
|
||||
"clocale": "cpp",
|
||||
"cmath": "cpp",
|
||||
"compare": "cpp",
|
||||
"concepts": "cpp",
|
||||
"cstdarg": "cpp",
|
||||
"cstddef": "cpp",
|
||||
"cstdint": "cpp",
|
||||
"cstdio": "cpp",
|
||||
"cstdlib": "cpp",
|
||||
"cstring": "cpp",
|
||||
"ctime": "cpp",
|
||||
"cwchar": "cpp",
|
||||
"cwctype": "cpp",
|
||||
"deque": "cpp",
|
||||
"list": "cpp",
|
||||
"map": "cpp",
|
||||
"unordered_map": "cpp",
|
||||
"vector": "cpp",
|
||||
"exception": "cpp",
|
||||
"algorithm": "cpp",
|
||||
"functional": "cpp",
|
||||
"iterator": "cpp",
|
||||
"memory": "cpp",
|
||||
"memory_resource": "cpp",
|
||||
"random": "cpp",
|
||||
"string": "cpp",
|
||||
"string_view": "cpp",
|
||||
"system_error": "cpp",
|
||||
"tuple": "cpp",
|
||||
"type_traits": "cpp",
|
||||
"utility": "cpp",
|
||||
"fstream": "cpp",
|
||||
"initializer_list": "cpp",
|
||||
"iosfwd": "cpp",
|
||||
"iostream": "cpp",
|
||||
"istream": "cpp",
|
||||
"limits": "cpp",
|
||||
"new": "cpp",
|
||||
"ostream": "cpp",
|
||||
"ranges": "cpp",
|
||||
"sstream": "cpp",
|
||||
"stdexcept": "cpp",
|
||||
"streambuf": "cpp",
|
||||
"typeinfo": "cpp",
|
||||
"variant": "cpp"
|
||||
}
|
||||
}
|
28
01_alapok.txt
Normal file → Executable file
28
01_alapok.txt
Normal file → Executable file
@ -1,3 +1,9 @@
|
||||
rev 1:
|
||||
rev 2: abs() képlet hozzáadva.
|
||||
rev 3: Hozzáadtam vector és float-os közötti szorzás operátorokat mindkét vektorhoz.
|
||||
rev 4: Vector length képlet hozzáadva.
|
||||
rev 5: Vector dot() képletben hiányzott egy .y
|
||||
rev 6: lenght_squared -> length_squared
|
||||
|
||||
1. Implementáld az alábbi 2ds matematikai vektor osztályt:
|
||||
|
||||
@ -7,6 +13,7 @@
|
||||
| + x : float | -> Nincs getter, és szetter, mert azok lassítanának. (Függvényhívásnak van minimális erőforrás igénye)
|
||||
| + y : float | -> Ilyen matematikai osztályoknál, érdemes ilyeneket kioptimalizálni. PLussz leírni is sok. getx(), setx() etc.
|
||||
| + abs() : Vector2 | -> visszaadja egy új vektorba ennek a vektornak az abszolút értékét. (x, y abszolút értékét veszi)
|
||||
| | Abszolút érték képlet: ha egy a < 0 ret -a, else ret a;
|
||||
| + angle() float | -> atan2(x, y)
|
||||
| + angle_to(b : Vector2) : float | -> atan2(cross(b), dot(b));
|
||||
| + cross(b : Vector2) : float | -> Cross product -> x * b.y - y * b.x;
|
||||
@ -16,15 +23,15 @@
|
||||
| + distance_to(b : Vector2) : float | -> sqrt((x - b.x) * (x - b.x) + (y - b.y) * (y - b.y));
|
||||
| + dot(b : Vector2) : float | -> dot product -> Ha a 2 vektor 90 fokosz szöget zár be, akkor 0.
|
||||
| | Pozitív, ha a 2 vektor által bezárt szög kisebb mint 90 fok, negatív, ha nagyobb.
|
||||
| | képlet: x * b.x + y * b.
|
||||
| | képlet: x * b.x + y * b.y.
|
||||
| + is_equal_approx(b : Vector2) : bool | -> nagyjából egyenlőek-e a vektorok. A max különbség legyen 0.00001 (Epszilon).
|
||||
| + length() : float |
|
||||
| + lenght_squared() : float | -> A hossz a gyökvonás nélkül. (A gyökvonás relatíve lassú, és ha csak össze kell
|
||||
| + length() : float | -> sqrt(x * x + y * y)
|
||||
| + length_squared() : float | -> A hossz a gyökvonás nélkül. (A gyökvonás relatíve lassú, és ha csak össze kell
|
||||
| | hasonlítani hosszakat, akkor elég így)
|
||||
| + lerp(b : Vector2, t : float) : Vector2 | -> Linear interpolate -> a jelenlegi vektor és b közötti átmenetvektort adja vissza t paraméter felhasználásával.
|
||||
| | A t 0 és 1 közötti. Képlet: newx = x + (t * (b.x - x)); és newy = y + (t * (b.y - y));
|
||||
| + normalized() : Vector2 | -> A vektor normalizálva. Normalizált vektor = a hossza 1.
|
||||
| | float l = lenght_squared(); ha nem 0, gyököt kell vonni belőle, és le kell osztani x, y-t is vele
|
||||
| | float l = length_squared(); ha nem 0, gyököt kell vonni belőle, és le kell osztani x, y-t is vele
|
||||
| + normalize() | -> Maga a vektor normalizálódik.
|
||||
| + add(b: Vector2) | -> x += b.x, y += b.y
|
||||
| + sub(b: Vector2) | -> x -= b.x, y -= b.y
|
||||
@ -32,6 +39,8 @@
|
||||
| + operator-=(b: Vector2) |
|
||||
| + operator+(a: Vector2, b: Vector2) : Vector2 |
|
||||
| + operator-(a: Vector2, b: Vector2) : Vector2 |
|
||||
| + operator*=(b: float) | -> x és y-t is megszorozzuk b-vel.
|
||||
| + operator*(Vector2 a, float b) : Vector2 | -> x és y-t is megszorozzuk b-vel.
|
||||
| + operator==(b: Vector2, b: Vector2) : bool |
|
||||
| + operator!=(b: Vector2, b: Vector2) : bool |
|
||||
| + Vector2() |
|
||||
@ -48,6 +57,7 @@
|
||||
| + y : float | -> Ilyen matematikai osztályoknál, érdemes ilyeneket kioptimalizálni. PLussz leírni is sok. getx(), setx() etc.
|
||||
| + z : float |
|
||||
| + abs() : Vector3 | -> visszaadja egy új vektorba ennek a vektornak az abszolút értékét. (x, y, y abszolút értékét veszi)
|
||||
| | Abszolút érték képlet: ha egy a < 0 ret -a, else ret a;
|
||||
| + angle_to(b : Vector3) : float | -> atan2(cross(b).length(), dot(b));
|
||||
| + cross(b : Vector3) : Vector3 | -> Cross product -> x = (y * b.z) - (z * b.y), y = (z * b.x) - (x * b.z), z = (x * b.y) - (y * b.x)
|
||||
| + clamped(len : float) : Vector3 | -> normalized() * len
|
||||
@ -56,16 +66,16 @@
|
||||
| + distance_to(b : Vector3) : float | -> return (b - *this).length();
|
||||
| + dot(b : Vector3) : float | -> dot product -> Ha a 2 vektor 90 fokosz szöget zár be, akkor 0.
|
||||
| | Pozitív, ha a 2 vektor által bezárt szög kisebb mint 90 fok, negatív, ha nagyobb.
|
||||
| | képlet: x * b.x + y * b + z * b.z.
|
||||
| | képlet: x * b.x + y * b.y + z * b.z.
|
||||
| + is_equal_approx(b : Vector3) : bool | -> nagyjából egyenlőek-e a vektorok. A max különbség legyen 0.00001 (Epszilon).
|
||||
| + length() : float |
|
||||
| + lenght_squared() : float | -> A hossz a gyökvonás nélkül. (A gyökvonás relatíve lassú, és ha csak össze kell
|
||||
| + length() : float | -> sqrt(x * x + y * y + z * z)
|
||||
| + length_squared() : float | -> A hossz a gyökvonás nélkül. (A gyökvonás relatíve lassú, és ha csak össze kell
|
||||
| | hasonlítani hosszakat, akkor elég így)
|
||||
| + lerp(b : Vector3, t : float) : Vector3 | -> Linear interpolate -> a jelenlegi vektor és b közötti átmenetvektort adja vissza t paraméter felhasználásával.
|
||||
| | A t 0 és 1 közötti. Képlet: newx = x + (t * (b.x - x)); és newy = y + (t * (b.y - y));
|
||||
| | newy = z + (t * (b.z - z));
|
||||
| + normalized() : Vector3 | -> A vektor normalizálva. Normalizált vektor = a hossza 1.
|
||||
| | float l = lenght_squared(); ha nem 0, gyököt kell vonni belőle, és le kell osztani x, y, z-t is vele
|
||||
| | float l = length_squared(); ha nem 0, gyököt kell vonni belőle, és le kell osztani x, y, z-t is vele
|
||||
| + normalize() | -> A vektor normalizálódik.
|
||||
| + add(b: Vector3) | -> x += b.x, y += b.y, z += b.z
|
||||
| + sub(b: Vector3) | -> x -= b.x, y -= b.y, z -= b.z
|
||||
@ -73,6 +83,8 @@
|
||||
| + operator-=(b: Vector3) |
|
||||
| + operator+(a: Vector3, b: Vector3) : Vector3 |
|
||||
| + operator-(a: Vector3, b: Vector3) : Vector3 |
|
||||
| + operator*=(b: float) | -> x,y és z-t is megszorozzuk b-vel.
|
||||
| + operator*(Vector2 a, float b) : Vector2 | -> x,y és z-t is megszorozzuk b-vel.
|
||||
| + operator==(b: Vector3, b: Vector2) : bool |
|
||||
| + operator!=(b: Vector3, b: Vector2) : bool |
|
||||
| + Vector3() |
|
||||
|
0
01_alapok/.clang-format
Normal file → Executable file
0
01_alapok/.clang-format
Normal file → Executable file
@ -9,5 +9,6 @@ fi
|
||||
|
||||
g++ -Wall -g -Iinclude -c main.cpp -o obj/main.o
|
||||
g++ -Wall -g -Iinclude -c vector2.cpp -o obj/vector2.o
|
||||
g++ -o bin/program obj/main.o obj/vector2.o
|
||||
g++ -Wall -g -Iinclude -c vector3.cpp -o obj/vector3.o
|
||||
g++ -o bin/program obj/main.o obj/vector2.o obj/vector3.o
|
||||
|
||||
|
0
01_alapok/main.cpp
Normal file → Executable file
0
01_alapok/main.cpp
Normal file → Executable file
152
01_alapok/vector2.cpp
Normal file → Executable file
152
01_alapok/vector2.cpp
Normal file → Executable file
@ -2,120 +2,172 @@
|
||||
|
||||
#include <cmath>
|
||||
|
||||
/*
|
||||
| + abs() : Vector2 | -> visszaadja egy új vektorba ennek a vektornak az abszolút értékét. (x, y abszolút értékét veszi)
|
||||
| + angle() float | -> atan2(x, y)
|
||||
| + angle_to(b : Vector2) : float | -> atan2(cross(b), dot(b));
|
||||
| + cross(b : Vector2) : float | -> Cross product -> x * b.y - y * b.x;
|
||||
| + clamped(len : float) : Vector2 | -> normalized() * len
|
||||
| + direction_to(b : Vector2) : Vector2 | -> Visszaad egy normalizált vektort ami ebből b be mutat. (A képlet: (b - a).normalized()) (operator-)
|
||||
| + distance_to_squared(b : Vector2) : float | -> (x - b.x) * (x - b.x) + (y - b.y) * (y - b.y);
|
||||
| + distance_to(b : Vector2) : float | -> sqrt((x - b.x) * (x - b.x) + (y - b.y) * (y - b.y));
|
||||
| + dot(b : Vector2) : float | -> dot product -> Ha a 2 vektor 90 fokosz szöget zár be, akkor 0.
|
||||
| | Pozitív, ha a 2 vektor által bezárt szög kisebb mint 90 fok, negatív, ha nagyobb.
|
||||
| | képlet: x * b.x + y * b.
|
||||
| + is_equal_approx(b : Vector2) : bool | -> nagyjából egyenlőek-e a vektorok. A max különbség legyen 0.00001 (Epszilon).
|
||||
| + length() : float |
|
||||
| + lenght_squared() : float | -> A hossz a gyökvonás nélkül. (A gyökvonás relatíve lassú, és ha csak össze kell
|
||||
| | hasonlítani hosszakat, akkor elég így)
|
||||
| + lerp(b : Vector2, t : float) : Vector2 | -> Linear interpolate -> a jelenlegi vektor és b közötti átmenetvektort adja vissza t paraméter felhasználásával.
|
||||
| | A t 0 és 1 közötti. Képlet: newx = x + (t * (b.x - x)); és newy = y + (t * (b.y - y));
|
||||
| + normalized() : Vector2 | -> A vektor normalizálva. Normalizált vektor = a hossza 1.
|
||||
| | float l = lenght_squared(); ha nem 0, gyököt kell vonni belőle, és le kell osztani x, y-t is vele
|
||||
| + normalize() | -> Maga a vektor normalizálódik.
|
||||
| + add(b: Vector2) | -> x += b.x, y += b.y
|
||||
| + sub(b: Vector2) | -> x -= b.x, y -= b.y
|
||||
| + operator+=(b: Vector2) | etc
|
||||
| + operator-=(b: Vector2) |
|
||||
| + operator+(a: Vector2, b: Vector2) : Vector2 |
|
||||
| + operator-(a: Vector2, b: Vector2) : Vector2 |
|
||||
| + operator==(b: Vector2) : bool |
|
||||
| + operator!=(b: Vector2) : bool |
|
||||
| + Vector2() |
|
||||
| + Vector2(b : Vector2) |
|
||||
| + Vector2(x : float, y : float) |
|
||||
*/
|
||||
#define EPSILON 0.00001
|
||||
|
||||
Vector2 Vector2::abs() {
|
||||
Vector2 Vector2::abs() const {
|
||||
Vector2 b;
|
||||
|
||||
b.x = abs(x);
|
||||
b.y = abs(y);
|
||||
b.x = x >= 0 ? x : -x;
|
||||
b.y = y >= 0 ? y : -y;
|
||||
|
||||
return b;
|
||||
}
|
||||
|
||||
float Vector2::angle() {
|
||||
float Vector2::angle() const {
|
||||
return atan2(x, y);
|
||||
}
|
||||
|
||||
float Vector2::angle_to(const Vector2 &b) {
|
||||
float Vector2::angle_to(const Vector2 &b) const {
|
||||
return atan2(cross(b), dot(b));
|
||||
}
|
||||
|
||||
float Vector2::cross() {
|
||||
float Vector2::cross(const Vector2 &b) const {
|
||||
return x * b.y - y * b.x;
|
||||
}
|
||||
|
||||
Vector2 Vector2::clamped(float len) {
|
||||
Vector2 Vector2::clamped(float len) const {
|
||||
return normalized() * len;
|
||||
}
|
||||
|
||||
Vector2 Vector2::direction_to(const Vector2 &b) {
|
||||
Vector2 Vector2::direction_to(const Vector2 &b) const {
|
||||
return (b - *this).normalized();
|
||||
}
|
||||
|
||||
float Vector2::distance_to_squared(const Vector2 &b) {
|
||||
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) {
|
||||
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) {
|
||||
float Vector2::dot(const Vector2 &b) const {
|
||||
return x * b.x + y * b.y;
|
||||
}
|
||||
|
||||
bool Vector2::is_equal_approx(const Vector2 &b) {
|
||||
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() {
|
||||
float Vector2::length() const {
|
||||
return sqrt(x * x + y * y);
|
||||
}
|
||||
|
||||
float Vector2::lenght_squared() {
|
||||
float Vector2::length_squared() const {
|
||||
return x * x + y * y;
|
||||
}
|
||||
|
||||
float Vector2::lerp(const Vector2 &b, const float t) {
|
||||
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() {
|
||||
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 x, const float 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);
|
||||
}
|
||||
|
33
01_alapok/vector2.h
Normal file → Executable file
33
01_alapok/vector2.h
Normal file → Executable file
@ -3,23 +3,23 @@
|
||||
|
||||
class Vector2 {
|
||||
public:
|
||||
Vector2 abs();
|
||||
float angle();
|
||||
float angle_to(const Vector2 &b);
|
||||
float cross();
|
||||
Vector2 clamped(float len);
|
||||
Vector2 direction_to(const Vector2 &b);
|
||||
float distance_to_squared(const Vector2 &b);
|
||||
float distance_to(const Vector2 &b);
|
||||
float dot(const Vector2 &b);
|
||||
bool is_equal_approx(const Vector2 &b);
|
||||
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();
|
||||
float lenght_squared();
|
||||
float length() const;
|
||||
float length_squared() const;
|
||||
|
||||
float lerp(const Vector2 &b, const float t);
|
||||
Vector2 lerp(const Vector2 &b, const float t) const;
|
||||
|
||||
Vector2 normalized();
|
||||
Vector2 normalized() const;
|
||||
void normalize();
|
||||
|
||||
void add(const Vector2 &b);
|
||||
@ -27,7 +27,7 @@ public:
|
||||
|
||||
Vector2();
|
||||
Vector2(const Vector2 &b);
|
||||
Vector2(const float x, const float y);
|
||||
Vector2(const float p_x, const float p_y);
|
||||
|
||||
Vector2& operator+=(const Vector2& b);
|
||||
Vector2& operator-=(const Vector2& b);
|
||||
@ -35,6 +35,9 @@ public:
|
||||
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);
|
||||
|
||||
|
191
01_alapok/vector3.cpp
Executable file
191
01_alapok/vector3.cpp
Executable file
@ -0,0 +1,191 @@
|
||||
#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
01_alapok/vector3.h
Executable file
48
01_alapok/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