Harmonic Flow Framework (libhffwk)
Cross platform C++ 2D Game Engine Framework
Rect.h
Go to the documentation of this file.
1 /*
2  Harmonic Flow Framework
3  Copyright (C) 2011-2018 Andreas Widen <andreas@harmonicflow.org>
4 
5  This software is provided 'as-is', without any express or implied
6  warranty. In no event will the authors be held liable for any damages
7  arising from the use of this software.
8 
9  Permission is granted to anyone to use this software for any purpose,
10  including commercial applications, and to alter it and redistribute it
11  freely, subject to the following restrictions:
12 
13  1. The origin of this software must not be misrepresented; you must not
14  claim that you wrote the original software. If you use this software
15  in a product, an acknowledgment in the product documentation would be
16  appreciated but is not required.
17  2. Altered source versions must be plainly marked as such, and must not be
18  misrepresented as being the original software.
19  3. This notice may not be removed or altered from any source distribution.
20 */
21 #pragma once
22 
23 /** @file
24  * @brief Specialized Boundary representing a Rect.
25 */
26 
27 #include "Boundary.h"
28 
29 namespace HFCore
30 {
31  /**
32  * @brief Specialized Boundary representing a Rect.
33  */
34  class Rect : public Boundary
35  {
36  public:
37  /**
38  * @brief Constructor.
39  *
40  * @param x X (or minX).
41  * @param y Y (or minY).
42  * @param width Width (or maxX).
43  * @param height Height (or maxY).
44  * @param isCentered Is centered or not.
45  */
46  Rect(fp32_hf x = 0, fp32_hf y = 0, fp32_hf width = 0,
47  fp32_hf height = 0, bool isCentered = false);
48 
49  /**
50  * @brief Destructor.
51  */
52  virtual ~Rect();
53 
54  /**
55  * @brief Overload of binary operator ==.
56  *
57  * This operator compares strict equality between two rectangles.
58  *
59  * @param rect Rect to compare against *this.
60  *
61  * @return true if equal, else false.
62  */
63  bool operator==(const Rect &rect) const;
64 
65  /**
66  * @brief Overload of binary operator !=.
67  *
68  * This operator compares strict difference between two
69  * rectangles.
70  *
71  * @param rect Rect to compare against *this.
72  *
73  * @return true if not equal, else false.
74  */
75  bool operator!=(const Rect &rect) const;
76 
77  /**
78  * @brief Overload of binary operator =.
79  *
80  * This operator assign *this the values of rect.
81  *
82  * @param rect Rect to assign the values for *this.
83  *
84  * @return *this assigned the values of rect.
85  */
86  virtual Rect &operator=(const Rect &rect);
87 
88  /**
89  * @brief Initializes the Rect.
90  *
91  * @param x X (or minX).
92  * @param y Y (or minY).
93  * @param width Width (or maxX).
94  * @param height Height (or maxY).
95  * @param isCentered Is centered or not.
96  *
97  * @see setMinX()
98  * @see setMinY()
99  * @see setMaxX()
100  * @see setMaxY()
101  */
102  void set(fp32_hf x, fp32_hf y, fp32_hf width, fp32_hf height,
103  bool isCentered = false);
104 
105  /**
106  * @brief Checks if two Rect's intersect and return intersection
107  * Rect.
108  *
109  * @param rect Rect to check for intersection against.
110  *
111  * @return Intersection Rect.
112  */
113  virtual Rect intersect(const Rect &rect);
114 
115  /**
116  * @brief Check is two Rect's intersect.
117  *
118  * @param rect Rect to check for intersection against.
119  *
120  * @return true if Rect's intersect, else false.
121  */
122  virtual bool intersects(const Rect &rect);
123 
124  /**
125  * @brief Get Rect Union of two Rect's.
126  *
127  * Union Rect is min of rect and *this x and y (or minX and minY)
128  * and max of rect and *this width and height (or maxX and maxY).
129  *
130  * @param rect Rect to Union with *this.
131  *
132  * @return Rect Union of rect and *this.
133  */
134  virtual Rect Union(const Rect &rect);
135 
136  /**
137  * @brief Initialize Rect with 0 for all members.
138  *
139  * @return Rect initialized with 0 for all members.
140  */
141  static Rect ZERO() { return Rect(0, 0, 0, 0); }
142 
143  /**
144  * @brief Get Rect X (or minX).
145  *
146  * @return Rect X (or minX).
147  *
148  * @see getMinX()
149  */
150  inline fp32_hf getX() const { return mMinX; }
151 
152  /**
153  * @brief Get Rect Y (or minY).
154  *
155  * @return Rect Y (or minY).
156  *
157  * @see getMinY()
158  */
159  inline fp32_hf getY() const { return mMinY; }
160 
161  /**
162  * @brief Get Rect Width (or maxX).
163  *
164  * @return Rect Width (or maxX).
165  *
166  * @see getMaxX()
167  */
168  inline fp32_hf getWidth() const { return mMaxX - mMinX; }
169 
170  /**
171  * @brief Get Rect Height (or maxY).
172  *
173  * @return Rect Height (or maxY).
174  *
175  * @see getMaxY()
176  */
177  inline fp32_hf getHeight() const { return mMaxY - mMinY; }
178 
179  /**
180  * @brief Get Rect minX (or X).
181  *
182  * @return Rect minX (or X).
183  *
184  * @see getX()
185  */
186  inline fp32_hf getMinX() const { return mMinX; }
187 
188  /**
189  * @brief Get Rect minY (or Y).
190  *
191  * @return Rect minY (or Y).
192  *
193  * @see getY()
194  */
195  inline fp32_hf getMinY() const { return mMinY; }
196 
197  /**
198  * @brief Get Rect maxX (or Width).
199  *
200  * @return Rect maxX (or Width).
201  *
202  * @see getWidth()
203  */
204  inline fp32_hf getMaxX() const { return mMaxX; }
205 
206  /**
207  * @brief Get Rect maxY (or Height).
208  *
209  * @return Rect maxY (or Height).
210  *
211  * @see getHeight()
212  */
213  inline fp32_hf getMaxY() const { return mMaxY; }
214 
215  /**
216  * @brief Set Rect maxX (or Width).
217  *
218  * @param maxx Rect maxX (or Width) to set.
219  *
220  * @see set()
221  */
222  inline void setMaxX(fp32_hf maxx) { mMaxX = maxx; }
223 
224  /**
225  * @brief Set Rect maxY (or Height).
226  *
227  * @param maxy Rect maxY (or Height) to set.
228  *
229  * @see set()
230  */
231  inline void setMaxY(fp32_hf maxy) { mMaxY = maxy; }
232 
233  /**
234  * @brief Set Rect minX (or X).
235  *
236  * @param minx Rect minX (or X) to set.
237  *
238  * @see set()
239  */
240  inline void setMinX(fp32_hf minx) { mMinX = minx; }
241 
242  /**
243  * @brief Set Rect minY (or Y).
244  *
245  * @param miny Rect minY (or Y) to set.
246  *
247  * @see set()
248  */
249  inline void setMinY(fp32_hf miny) { mMinY = miny; }
250 
251  // Boundary overrides:
252  virtual bool contains(const Vector2 &point);
253  virtual Boundary *clone();
254 
255  private:
256  fp32_hf mMinX;
257  fp32_hf mMinY;
258  fp32_hf mMaxX;
259  fp32_hf mMaxY;
260  };
261 }
fp32_hf getMaxX() const
Get Rect maxX (or Width).
Definition: Rect.h:204
virtual Rect & operator=(const Rect &rect)
Overload of binary operator =.
Definition: Rect.cpp:71
fp32_hf getWidth() const
Get Rect Width (or maxX).
Definition: Rect.h:168
Rect(fp32_hf x=0, fp32_hf y=0, fp32_hf width=0, fp32_hf height=0, bool isCentered=false)
Constructor.
Definition: Rect.cpp:27
bool operator!=(const Rect &rect) const
Overload of binary operator !=.
Definition: Rect.cpp:138
static Rect ZERO()
Initialize Rect with 0 for all members.
Definition: Rect.h:141
fp32_hf getMaxY() const
Get Rect maxY (or Height).
Definition: Rect.h:213
virtual bool contains(const Vector2 &point)
Check if a point collides or not.
Definition: Rect.cpp:54
virtual bool intersects(const Rect &rect)
Check is two Rect&#39;s intersect.
Definition: Rect.cpp:115
fp32_hf getY() const
Get Rect Y (or minY).
Definition: Rect.h:159
virtual Rect intersect(const Rect &rect)
Checks if two Rect&#39;s intersect and return intersection Rect.
Definition: Rect.cpp:81
Boundary is a base class for other classes such as Circle, Rect etc.
Definition: Boundary.h:36
A two-dimensional floating point vector and associated math functions.
Definition: Vector2.h:42
bool operator==(const Rect &rect) const
Overload of binary operator ==.
Definition: Rect.cpp:125
fp32_hf getX() const
Get Rect X (or minX).
Definition: Rect.h:150
fp32_hf getMinX() const
Get Rect minX (or X).
Definition: Rect.h:186
Definition: Actor.h:34
virtual Boundary * clone()
Clone all class properties and return a new copy / clone.
Definition: Rect.cpp:59
Specialized Boundary representing a Rect.
Definition: Rect.h:34
virtual Rect Union(const Rect &rect)
Get Rect Union of two Rect&#39;s.
Definition: Rect.cpp:103
fp32_hf getMinY() const
Get Rect minY (or Y).
Definition: Rect.h:195
void setMinY(fp32_hf miny)
Set Rect minY (or Y).
Definition: Rect.h:249
void setMaxX(fp32_hf maxx)
Set Rect maxX (or Width).
Definition: Rect.h:222
fp32_hf getHeight() const
Get Rect Height (or maxY).
Definition: Rect.h:177
void setMinX(fp32_hf minx)
Set Rect minX (or X).
Definition: Rect.h:240
virtual ~Rect()
Destructor.
Definition: Rect.cpp:32
void setMaxY(fp32_hf maxy)
Set Rect maxY (or Height).
Definition: Rect.h:231
Boundary is a base class for other classes such as Circle, Rect etc.
float fp32_hf
A type definition for float.
Definition: HFDataTypes.h:359