Nazara Engine  0.4
A fast, complete, cross-platform API designed for game development
Rect.hpp
1 // Copyright (C) 2017 Jérôme Leclercq
2 // This file is part of the "Nazara Engine - Mathematics module"
3 // For conditions of distribution and use, see copyright notice in Config.hpp
4 
5 #pragma once
6 
7 #ifndef NAZARA_RECT_HPP
8 #define NAZARA_RECT_HPP
9 
10 #include <Nazara/Core/String.hpp>
11 #include <Nazara/Math/Enums.hpp>
12 #include <Nazara/Math/Vector2.hpp>
13 
14 namespace Nz
15 {
16  struct SerializationContext;
17 
18  template<typename T>
19  class Rect
20  {
21  public:
22  Rect() = default;
23  Rect(T Width, T Height);
24  Rect(T X, T Y, T Width, T Height);
25  Rect(const T rect[4]);
26  Rect(const Vector2<T>& lengths);
27  Rect(const Vector2<T>& vec1, const Vector2<T>& vec2);
28  template<typename U> explicit Rect(const Rect<U>& rect);
29  Rect(const Rect& rect) = default;
30  ~Rect() = default;
31 
32  bool Contains(T X, T Y) const;
33  bool Contains(const Rect& rect) const;
34  bool Contains(const Vector2<T>& point) const;
35 
36  Rect& ExtendTo(T X, T Y);
37  Rect& ExtendTo(const Rect& rect);
38  Rect& ExtendTo(const Vector2<T>& point);
39 
40  Vector2<T> GetCenter() const;
41  Vector2<T> GetCorner(RectCorner corner) const;
42  Vector2<T> GetLengths() const;
43  Vector2<T> GetMaximum() const;
44  Vector2<T> GetMinimum() const;
45  Vector2<T> GetNegativeVertex(const Vector2<T>& normal) const;
46  Vector2<T> GetPosition() const;
47  Vector2<T> GetPositiveVertex(const Vector2<T>& normal) const;
48 
49  bool Intersect(const Rect& rect, Rect* intersection = nullptr) const;
50 
51  bool IsValid() const;
52 
53  Rect& MakeZero();
54 
55  Rect& Set(T Width, T Height);
56  Rect& Set(T X, T Y, T Width, T Height);
57  Rect& Set(const T rect[4]);
58  Rect& Set(const Rect<T>& rect);
59  Rect& Set(const Vector2<T>& lengths);
60  Rect& Set(const Vector2<T>& vec1, const Vector2<T>& vec2);
61  template<typename U> Rect& Set(const Rect<U>& rect);
62 
63  String ToString() const;
64 
65  Rect& Translate(const Vector2<T>& translation);
66 
67  T& operator[](std::size_t i);
68  T operator[](std::size_t i) const;
69 
70  Rect operator*(T scalar) const;
71  Rect operator*(const Vector2<T>& vec) const;
72  Rect operator/(T scalar) const;
73  Rect operator/(const Vector2<T>& vec) const;
74  Rect& operator=(const Rect& other) = default;
75 
76  Rect& operator*=(T scalar);
77  Rect& operator*=(const Vector2<T>& vec);
78  Rect& operator/=(T scalar);
79  Rect& operator/=(const Vector2<T>& vec);
80 
81  bool operator==(const Rect& rect) const;
82  bool operator!=(const Rect& rect) const;
83 
84  static Rect Lerp(const Rect& from, const Rect& to, T interpolation);
85  static Rect Zero();
86 
87  T x, y, width, height;
88  };
89 
90  typedef Rect<double> Rectd;
91  typedef Rect<float> Rectf;
92  typedef Rect<int> Recti;
93  typedef Rect<unsigned int> Rectui;
94  typedef Rect<Int32> Recti32;
95  typedef Rect<UInt32> Rectui32;
96 
97  template<typename T> bool Serialize(SerializationContext& context, const Rect<T>& rect);
98  template<typename T> bool Unserialize(SerializationContext& context, Rect<T>* rect);
99 }
100 
101 template<typename T>
102 std::ostream& operator<<(std::ostream& out, const Nz::Rect<T>& rect);
103 
104 #include <Nazara/Math/Rect.inl>
105 
106 #endif // NAZARA_RECT_HPP
bool Contains(T X, T Y) const
Tests whether the rectangle contains the provided point inclusive of the edge of the rectangle...
Definition: Rect.inl:118
static Rect Lerp(const Rect &from, const Rect &to, T interpolation)
Interpolates the rectangle to other one with a factor of interpolation.
Definition: Rect.inl:768
Rect & Set(T Width, T Height)
Sets the components of the rectangle with width and height.
Definition: Rect.inl:429
TODO: Inherit SoundEmitter from Node.
Definition: Algorithm.hpp:12
Rect & operator*=(T scalar)
Multiplies the lengths of this rectangle with the scalar.
Definition: Rect.inl:670
Vector2< T > GetMinimum() const
Gets a Vector2 for the minimum point.
Definition: Rect.inl:293
bool Unserialize(SerializationContext &context, bool *value)
Unserializes a boolean.
Definition: Algorithm.inl:279
Vector2< T > GetMaximum() const
Gets a Vector2 for the maximum point.
Definition: Rect.inl:280
Vector2< T > GetPosition() const
Gets a Vector2 for the position.
Definition: Rect.inl:329
Core class that represents a string.
Definition: String.hpp:22
Math class that represents an axis-aligned rectangle in two dimensions.
Definition: Rect.hpp:19
bool operator!=(const Rect &rect) const
Compares the rectangle to other one.
Definition: Rect.inl:748
Vector2< T > GetLengths() const
Gets a Vector2 for the lengths.
Definition: Rect.inl:267
Vector2< T > GetPositiveVertex(const Vector2< T > &normal) const
Computes the positive vertex of one direction.
Definition: Rect.inl:344
bool IsValid() const
Checks whether this rectangle is valid.
Definition: Rect.inl:395
Rect & ExtendTo(T X, T Y)
Extends the rectangle to contain the point in the boundary.
Definition: Rect.inl:166
bool Intersect(const Rect &rect, Rect *intersection=nullptr) const
Checks whether or not this rectangle intersects another one.
Definition: Rect.inl:366
Structure containing a serialization/unserialization context states.
Definition: SerializationContext.hpp:18
Rect & operator/=(T scalar)
Divides the lengths of this rectangle with the scalar.
Definition: Rect.inl:702
Rect operator*(T scalar) const
Multiplies the lengths with the scalar.
Definition: Rect.inl:618
Rect & MakeZero()
Makes the rectangle position (0, 0) and lengths (0, 0)
Definition: Rect.inl:408
Rect operator/(T scalar) const
Divides the lengths with the scalar.
Definition: Rect.inl:644
bool Serialize(SerializationContext &context, bool value)
Serializes a boolean.
Definition: Algorithm.inl:214
String ToString() const
Gives a string representation.
Definition: Rect.inl:553
Vector2< T > GetCenter() const
Gets a Vector2 for the center.
Definition: Rect.inl:225
T & operator[](std::size_t i)
Returns the ith element of the rectangle.
Definition: Rect.inl:586
Vector2< T > GetCorner(RectCorner corner) const
Gets the Vector2 for the corner.
Definition: Rect.inl:240
bool operator==(const Rect &rect) const
Compares the rectangle to other one.
Definition: Rect.inl:734
Vector2< T > GetNegativeVertex(const Vector2< T > &normal) const
Computes the negative vertex of one direction.
Definition: Rect.inl:308
static Rect Zero()
Shorthand for the rectangle (0, 0, 0, 0)
Definition: Rect.inl:795
Rect & Translate(const Vector2< T > &translation)
Translates the rectangle.
Definition: Rect.inl:568
Math class that represents an element of the two dimensional vector space.
Definition: Matrix4.hpp:21