Rect2 class.

This commit is contained in:
Relintai 2021-03-27 14:25:42 +01:00
parent d0f267f69f
commit 7f80e0d3d8
4 changed files with 216 additions and 21 deletions

View File

@ -4,6 +4,9 @@ rev 3: Hozzáadtam vector és float-os közötti szorzás operátorokat mindkét
rev 4: Vector length képlet hozzáadva.
rev 5: Vector dot() képletben hiányzott egy .y
rev 6: lenght_squared -> length_squared
rev 7: Rect2 is_equal_approx paraméter típus kijavítva.
rev 8: Rect2 grow, shrink visszatérési érték javítva (Már nincs)
rev 9: intersects_include_borders typo javítva
1. Implementáld az alábbi 2ds matematikai vektor osztályt:
@ -108,9 +111,9 @@ rev 6: lenght_squared -> length_squared
| + encloses(b : Rect2) : bool | -> Lejjebb
| + has_no_area() : bool |
| + has_point(x : float, y : float) : bool |
| + is_equal_approx(b : Vector3) : bool |
| + grow(by : float) : Rect2 | -> x, y, csökkent, w,h növel by-al
| + shrink(by : float) : Rect2 | -> x, y, növel, w,h csökkent by-al
| + is_equal_approx(b : Rect2) : bool |
| + grow(by : float) | -> x, y, csökkent, w,h növel by-al
| + shrink(by : float) | -> x, y, növel, w,h csökkent by-al
| + operator+=(b: Rect2) |
| + operator-=(b: Rect2) |
| + operator+(a: Rect2, b: Rect2) : Rect2 |
@ -123,22 +126,6 @@ rev 6: lenght_squared -> length_squared
| + Rect2(x : float, y : float, w : float, h : float) |
------------------------------------------------------|
intersects_include_borders:
if (x > (b.x + b.w))
return false;
if ((x + w) < b.x)
return false;
if (y > (b.z + b.h))
return false;
if ((y + h) < b.y)
return false;
return true;
intersects:
if (x >= (b.x + b.w))
return false;
@ -153,7 +140,21 @@ intersects:
return false;
return true;
}
intersects_include_borders:
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;
Encloses:

View File

@ -10,5 +10,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++ -Wall -g -Iinclude -c vector3.cpp -o obj/vector3.o
g++ -o bin/program obj/main.o obj/vector2.o obj/vector3.o
g++ -Wall -g -Iinclude -c rect2.cpp -o obj/rect2.o
g++ -o bin/program obj/main.o obj/vector2.o obj/vector3.o obj/rect2.o

156
01_alapok/rect2.cpp Normal file
View File

@ -0,0 +1,156 @@
#include "rect2.h"
#define EPSILON 0.00001
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
01_alapok/rect2.h Normal file
View 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