Symphony Of Empires
rectangle.hpp
Go to the documentation of this file.
1 // Eng3D - General purpouse game engine
2 // Copyright (C) 2021, Eng3D contributors
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <https://www.gnu.org/licenses/>.
16 //
17 // ----------------------------------------------------------------------------
18 // Name:
19 // rectangle.hpp
20 //
21 // Abstract:
22 // Does some important stuff.
23 // ----------------------------------------------------------------------------
24 
25 #pragma once
26 
27 #include <glm/vec2.hpp>
28 #include <glm/common.hpp>
29 
30 namespace Eng3D {
31  struct Rectangle {
32  float left = 0.f, top = 0.f, right = 0.f, bottom = 0.f;
33  constexpr Rectangle() = default;
34  constexpr Rectangle(float x, float y, float width, float height)
35  : left{ x },
36  top{ y },
37  right{ x + width },
38  bottom{ y + height }
39  {
40 
41  }
42 
43  constexpr Rectangle(glm::vec2 position, glm::vec2 size)
44  : left{ position.x },
45  top{ position.y },
46  right{ position.x + size.x },
47  bottom{ position.y + size.y }
48  {
49 
50  }
51 
52  ~Rectangle() = default;
53 
56  constexpr float width() const {
57  return right - left;
58  }
59 
62  constexpr float height() const {
63  return bottom - top;
64  }
65 
68  constexpr glm::vec2 size() const {
69  return glm::vec2{ right - left, bottom - top };
70  }
71 
74  constexpr void size(glm::vec2 size) {
75  right = left + size.x;
76  bottom = top + size.y;
77  }
78 
79  template<typename T>
80  constexpr void size(T x, T y) {
81  size(glm::vec2(x, y));
82  }
83 
86  constexpr glm::vec2 position() const {
87  return glm::vec2{ left, top };
88  }
89 
92  constexpr void position(glm::vec2 position) {
93  left = position.x;
94  top = position.y;
95  }
96 
97  template<typename T>
98  constexpr void position(T x, T y) {
99  position(glm::vec2(x, y));
100  }
101 
104  constexpr void scale(glm::vec2 factor) {
105  left *= factor.x;
106  top *= factor.y;
107  right *= factor.x;
108  bottom *= factor.y;
109  }
110 
111  template<typename T>
112  constexpr void scale(T x, T y) {
113  scale(glm::vec2(x, y));
114  }
115 
118  constexpr void offset(glm::vec2 offset) {
119  left += offset.x;
120  top += offset.y;
121  right += offset.x;
122  bottom += offset.y;
123  }
124 
125  template<typename T>
126  constexpr void offset(T x, T y) {
127  offset(glm::vec2(x, y));
128  }
129 
134  constexpr bool contains(glm::vec2 pos) const {
135  return pos.x >= left && pos.x <= right && pos.y >= top && pos.y <= bottom;
136  }
137 
138  template<typename T>
139  constexpr bool contains(T x, T y) const {
140  return contains(glm::vec2(x, y));
141  }
142 
147  constexpr bool contains(const Rectangle& rect) const {
148  return contains(rect.left, rect.top)
149  || contains(rect.left, rect.bottom)
150  || contains(rect.right, rect.top)
151  || contains(rect.right, rect.bottom);
152  }
153 
157  constexpr Rectangle intersection(const Rectangle& rect) const {
158  const auto i_left = glm::max(this->left, rect.left);
159  const auto i_top = glm::max(this->top, rect.top);
160  const auto i_right = glm::min(this->right, rect.right);
161  const auto i_bottom = glm::min(this->bottom, rect.bottom);
162  return Eng3D::Rectangle{ i_left, i_top, i_right - i_left, i_bottom - i_top };
163  }
164 
168  constexpr Rectangle join(const Rectangle& rect) const {
169  const auto i_left = glm::min(this->left, rect.left);
170  const auto i_top = glm::min(this->top, rect.top);
171  const auto i_right = glm::max(this->right, rect.right);
172  const auto i_bottom = glm::max(this->bottom, rect.bottom);
173  return Eng3D::Rectangle{ i_left, i_top, i_right - i_left, i_bottom - i_top };
174  }
175  };
176  typedef struct Rectangle Rect;
177 }
struct Rectangle Rect
Definition: rectangle.hpp:176
constexpr void position(T x, T y)
Definition: rectangle.hpp:98
constexpr glm::vec2 size() const
Obtains the current size of the rectangle.
Definition: rectangle.hpp:68
constexpr void scale(T x, T y)
Definition: rectangle.hpp:112
constexpr bool contains(const Rectangle &rect) const
Checks if the rectangle is contains the rectangle.
Definition: rectangle.hpp:147
constexpr Rectangle join(const Rectangle &rect) const
Obtains the intersection rectangle from two other rectangles R1 and R2.
Definition: rectangle.hpp:168
constexpr Rectangle(glm::vec2 position, glm::vec2 size)
Definition: rectangle.hpp:43
constexpr float height() const
Obtains the height.
Definition: rectangle.hpp:62
constexpr void size(glm::vec2 size)
Sets the size of the rectangle.
Definition: rectangle.hpp:74
constexpr Rectangle()=default
constexpr Rectangle intersection(const Rectangle &rect) const
Obtains the intersection rectangle from two other rectangles R1 and R2.
Definition: rectangle.hpp:157
constexpr void size(T x, T y)
Definition: rectangle.hpp:80
constexpr Rectangle(float x, float y, float width, float height)
Definition: rectangle.hpp:34
constexpr bool contains(T x, T y) const
Definition: rectangle.hpp:139
constexpr void offset(glm::vec2 offset)
Offset the rectangle by the given parameter.
Definition: rectangle.hpp:118
constexpr void scale(glm::vec2 factor)
Scales the rectangle by factor.
Definition: rectangle.hpp:104
constexpr float width() const
Obtains the width.
Definition: rectangle.hpp:56
constexpr glm::vec2 position() const
Obtains the current position of the rectangle.
Definition: rectangle.hpp:86
constexpr bool contains(glm::vec2 pos) const
Checks if the point is contains the point.
Definition: rectangle.hpp:134
constexpr void position(glm::vec2 position)
Sets the base position of the rectangle, modifying it's size.
Definition: rectangle.hpp:92
constexpr void offset(T x, T y)
Definition: rectangle.hpp:126
~Rectangle()=default