R-Type  2
Doom but in better
Loading...
Searching...
No Matches
Rect.cpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2024
3** rtype (Workspace)
4** File description:
5** Rect.cpp
6*/
7
14#include "Recoded/Rect.hpp"
15
16template <typename T>
17Recoded::Rect<T>::Rect() : position({ 0,0 }), size({ 0,0 }) {};
18
19template <typename T>
20Recoded::Rect<T>::Rect(std::pair<T, T> position, std::pair<T, T> size)
21 : position(position), size(size)
22{
23}
24
25template <typename T>
26template <typename U>
28{
29 return Rect<U>(
30 std::pair<U, U>(static_cast<U>(position.first), static_cast<U>(position.second)),
31 std::pair<U, U>(static_cast<U>(size.first), static_cast<U>(size.second))
32 );
33}
34
35template <typename T>
36constexpr bool Recoded::Rect<T>::contains(std::pair<T, T> point) const
37{
38 return point.first > position.first && point.first < (position.first + size.first) &&
39 point.second > position.second && point.second < (position.second + size.second);
40}
41
42template <typename T>
43std::optional<Recoded::Rect<T>> Recoded::Rect<T>::findIntersection(const Rect<T> &rect) const
44{
45 T x_intersection_min = std::max(position.first, rect.position.first);
46 T y_intersection_min = std::max(position.second, rect.position.second);
47 T x_intersection_max = std::min(position.first + size.first, rect.position.first + rect.size.first);
48 T y_intersection_max = std::min(position.second + size.second, rect.position.second + rect.size.second);
49
50 if (x_intersection_min < x_intersection_max && y_intersection_min < y_intersection_max) {
51 PRETTY_INFO << "Intersection found, returning new rect instance." << std::endl;
52 return Rect<T>(
53 std::pair<T, T>(x_intersection_min, y_intersection_min),
54 std::pair<T, T>(x_intersection_max - x_intersection_min, y_intersection_max - y_intersection_min)
55 );
56 } else {
57 PRETTY_WARNING << "No Intersections were found." << std::endl;
58 return std::nullopt;
59 }
60}
61
62template <typename T>
63std::pair<T, T> Recoded::Rect<T>::getCenter() const
64{
65 return { position.first + size.first / 2, position.second + size.second / 2 };
66}
67
68template <typename T>
69constexpr bool Recoded::operator==(const Recoded::Rect<T> &lhs, const Recoded::Rect<T> &rhs)
70{
71 return lhs.position == rhs.position && lhs.size == rhs.size;
72}
73
74template <typename T>
75constexpr bool Recoded::operator!=(const Recoded::Rect<T> &lhs, const Recoded::Rect<T> &rhs)
76{
77 return !(lhs == rhs);
78}
79
80template class Recoded::Rect<int>;
81template bool Recoded::operator==(const Recoded::Rect<int> &lhs, const Recoded::Rect<int> &rhs);
82template bool Recoded::operator!=(const Recoded::Rect<int> &lhs, const Recoded::Rect<int> &rhs);
83template std::ostream &Recoded::operator<<(std::ostream &os, const Recoded::Rect<int> &rhs);
84
85template class Recoded::Rect<long int>;
86template bool Recoded::operator==(const Recoded::Rect<long int> &lhs, const Recoded::Rect<long int> &rhs);
87template bool Recoded::operator!=(const Recoded::Rect<long int> &lhs, const Recoded::Rect<long int> &rhs);
88template std::ostream &Recoded::operator<<(std::ostream &os, const Recoded::Rect<long int> &rhs);
89
90template class Recoded::Rect<long long int>;
93template std::ostream &Recoded::operator<<(std::ostream &os, const Recoded::Rect<long long int> &rhs);
94
95template class Recoded::Rect<unsigned int>;
98template std::ostream &Recoded::operator<<(std::ostream &os, const Recoded::Rect<unsigned int> &rhs);
99
103template std::ostream &Recoded::operator<<(std::ostream &os, const Recoded::Rect<unsigned long int> &rhs);
104
108template std::ostream &Recoded::operator<<(std::ostream &os, const Recoded::Rect<unsigned long long int> &rhs);
109
110template class Recoded::Rect<float>;
111template bool Recoded::operator==(const Recoded::Rect<float> &lhs, const Recoded::Rect<float> &rhs);
112template bool Recoded::operator!=(const Recoded::Rect<float> &lhs, const Recoded::Rect<float> &rhs);
113template std::ostream &Recoded::operator<<(std::ostream &os, const Recoded::Rect<float> &rhs);
114
115template class Recoded::Rect<double>;
116template bool Recoded::operator==(const Recoded::Rect<double> &lhs, const Recoded::Rect<double> &rhs);
117template bool Recoded::operator!=(const Recoded::Rect<double> &lhs, const Recoded::Rect<double> &rhs);
118template std::ostream &Recoded::operator<<(std::ostream &os, const Recoded::Rect<double> &rhs);
#define PRETTY_INFO
Info log with details and colour.
#define PRETTY_WARNING
Warning log with details and colour.
Defines a rectangle class (Rect) mimicking sf::Rect without relying on SFML.
A generic 2D rectangle class that holds position and size as pairs.
Definition Rect.hpp:38
std::pair< T, T > size
The size of the rectangle (width, height)
Definition Rect.hpp:98
Rect()
Default constructor, initializes the rectangle with default values (0, 0) for position and size.
Definition Rect.cpp:17
std::optional< Rect< T > > findIntersection(const Rect< T > &rect) const
Finds the intersection between this rectangle and another.
Definition Rect.cpp:43
constexpr bool contains(std::pair< T, T > point) const
Checks if a point is inside the rectangle (non-inclusive).
Definition Rect.cpp:36
std::pair< T, T > getCenter() const
Gets the center of the rectangle.
Definition Rect.cpp:63
std::pair< T, T > position
the position (top and left corner)
Definition Rect.hpp:97
constexpr bool operator!=(const Rect< T > &lhs, const Rect< T > &rhs)
Inequality operators for the Rect.
Definition Rect.cpp:75
std::ostream & operator<<(std::ostream &os, const Rect< T > &rectangle)
Operator in charge of outputing the values contained in the _rect when it is passed through a << oper...
Definition Rect.hpp:112
constexpr bool operator==(const Rect< T > &lhs, const Rect< T > &rhs)
Overload that allows the user to check if 2 rect instances are identical.
Definition Rect.cpp:69