MathFu
An open source project by FPL.
 All Classes Namespaces Files Functions Variables Typedefs Friends Groups Pages
rect.h
1 /*
2 * Copyright 2016 Google Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #ifndef MATHFU_RECT_H_
17 #define MATHFU_RECT_H_
18 
19 #include "mathfu/vector.h"
20 
21 namespace mathfu {
22 
23 /// @addtogroup mathfu_rect
24 /// @{
25 /// @class Rect "mathfu/rect.h"
26 /// @brief Rect of type T containing position (pos) and width.
27 ///
28 /// Rect contains two 2D <b>Vector</b>s of type <b>T</b> representing position
29 /// (pos) and size.
30 ///
31 /// @tparam T type of Rect elements.
32 template <class T>
33 struct Rect {
34  Vector<T, 2> pos;
35  Vector<T, 2> size;
36 
37  /// @brief Create a rect from a vector4 of the same type.
38  ///
39  /// @param v Vector that the data will be copied from.
40  explicit Rect(const Vector<T, 4>& v)
41  : pos(v.x, v.y), size(v.z, v.w) {}
42 
43  /// @brief Create a rect from x, y, width and height values.
44  ///
45  /// @param x the given x value.
46  /// @param y the given y value.
47  /// @param width the given width value.
48  /// @param height the given height value.
49  inline Rect(T x = static_cast<T>(0), T y = static_cast<T>(0),
50  T width = static_cast<T>(0), T height = static_cast<T>(0))
51  : pos(x, y), size(width, height) {}
52 
53  /// @brief Create a rect from two vector2 representing position and size.
54  ///
55  /// @param pos Vector representing the position vector (x and y values).
56  /// @param size Vector represening the size vector (width and height values).
57  inline Rect(const Vector<T, 2>& pos, const Vector<T, 2>& size)
58  : pos(pos), size(size) {}
59 };
60 /// @}
61 
62 /// @brief Check if two rects are identical.
63 ///
64 /// @param r1 Rect to be tested.
65 /// @param r2 Other rect to be tested.
66 template <class T>
67 bool operator==(const Rect<T>& r1, const Rect<T>& r2) {
68  return (r1.pos == r2.pos && r1.size == r2.size);
69 }
70 
71 /// @brief Check if two rects are <b>not</b> identical.
72 ///
73 /// @param r1 Rect to be tested.
74 /// @param r2 Other rect to be tested.
75 template <class T>
76 bool operator!=(const Rect<T>& r1, const Rect<T>& r2) {
77  return !(r1 == r2);
78 }
79 
80 } // namespace mathfu
81 
82 #endif // MATHFU_RECT_H_
Definition: vector_2.h:24
Rect of type T containing position (pos) and width.
Definition: rect.h:33
Definition: vector_4.h:25
Rect(const Vector< T, 4 > &v)
Create a rect from a vector4 of the same type.
Definition: rect.h:40
Vector class and functions.
Rect(T x=static_cast< T >(0), T y=static_cast< T >(0), T width=static_cast< T >(0), T height=static_cast< T >(0))
Create a rect from x, y, width and height values.
Definition: rect.h:49
bool operator==(const Rect< T > &r1, const Rect< T > &r2)
Check if two rects are identical.
Definition: rect.h:67
bool operator!=(const Rect< T > &r1, const Rect< T > &r2)
Check if two rects are not identical.
Definition: rect.h:76
Rect(const Vector< T, 2 > &pos, const Vector< T, 2 > &size)
Create a rect from two vector2 representing position and size.
Definition: rect.h:57