El Octavio  1.0
This is a video game about adventures.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Rect.inl
Go to the documentation of this file.
1
2//
3// SFML - Simple and Fast Multimedia Library
4// Copyright (C) 2007-2018 Laurent Gomila (laurent@sfml-dev.org)
5//
6// This software is provided 'as-is', without any express or implied warranty.
7// In no event will the authors be held liable for any damages 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 freely,
11// subject to the following restrictions:
12//
13// 1. The origin of this software must not be misrepresented;
14// you must not claim that you wrote the original software.
15// If you use this software in a product, an acknowledgment
16// in the product documentation would be appreciated but is not required.
17//
18// 2. Altered source versions must be plainly marked as such,
19// and must not be misrepresented as being the original software.
20//
21// 3. This notice may not be removed or altered from any source distribution.
22//
24
25
27template <typename T>
29left (0),
30top (0),
31width (0),
32height(0)
33{
34
35}
36
37
39template <typename T>
40Rect<T>::Rect(T rectLeft, T rectTop, T rectWidth, T rectHeight) :
41left (rectLeft),
42top (rectTop),
43width (rectWidth),
44height(rectHeight)
45{
46
47}
48
49
51template <typename T>
52Rect<T>::Rect(const Vector2<T>& position, const Vector2<T>& size) :
53left (position.x),
54top (position.y),
55width (size.x),
56height(size.y)
57{
58
59}
60
61
63template <typename T>
64template <typename U>
65Rect<T>::Rect(const Rect<U>& rectangle) :
66left (static_cast<T>(rectangle.left)),
67top (static_cast<T>(rectangle.top)),
68width (static_cast<T>(rectangle.width)),
69height(static_cast<T>(rectangle.height))
70{
71}
72
73
75template <typename T>
76bool Rect<T>::contains(T x, T y) const
77{
78 // Rectangles with negative dimensions are allowed, so we must handle them correctly
79
80 // Compute the real min and max of the rectangle on both axes
81 T minX = std::min(left, static_cast<T>(left + width));
82 T maxX = std::max(left, static_cast<T>(left + width));
83 T minY = std::min(top, static_cast<T>(top + height));
84 T maxY = std::max(top, static_cast<T>(top + height));
85
86 return (x >= minX) && (x < maxX) && (y >= minY) && (y < maxY);
87}
88
89
91template <typename T>
92bool Rect<T>::contains(const Vector2<T>& point) const
93{
94 return contains(point.x, point.y);
95}
96
97
99template <typename T>
100bool Rect<T>::intersects(const Rect<T>& rectangle) const
101{
102 Rect<T> intersection;
103 return intersects(rectangle, intersection);
104}
105
106
108template <typename T>
109bool Rect<T>::intersects(const Rect<T>& rectangle, Rect<T>& intersection) const
110{
111 // Rectangles with negative dimensions are allowed, so we must handle them correctly
112
113 // Compute the min and max of the first rectangle on both axes
114 T r1MinX = std::min(left, static_cast<T>(left + width));
115 T r1MaxX = std::max(left, static_cast<T>(left + width));
116 T r1MinY = std::min(top, static_cast<T>(top + height));
117 T r1MaxY = std::max(top, static_cast<T>(top + height));
118
119 // Compute the min and max of the second rectangle on both axes
120 T r2MinX = std::min(rectangle.left, static_cast<T>(rectangle.left + rectangle.width));
121 T r2MaxX = std::max(rectangle.left, static_cast<T>(rectangle.left + rectangle.width));
122 T r2MinY = std::min(rectangle.top, static_cast<T>(rectangle.top + rectangle.height));
123 T r2MaxY = std::max(rectangle.top, static_cast<T>(rectangle.top + rectangle.height));
125 // Compute the intersection boundaries
126 T interLeft = std::max(r1MinX, r2MinX);
127 T interTop = std::max(r1MinY, r2MinY);
128 T interRight = std::min(r1MaxX, r2MaxX);
129 T interBottom = std::min(r1MaxY, r2MaxY);
130
131 // If the intersection is valid (positive non zero area), then there is an intersection
132 if ((interLeft < interRight) && (interTop < interBottom))
133 {
134 intersection = Rect<T>(interLeft, interTop, interRight - interLeft, interBottom - interTop);
135 return true;
137 else
138 {
139 intersection = Rect<T>(0, 0, 0, 0);
140 return false;
141 }
142}
143
144
146template <typename T>
147inline bool operator ==(const Rect<T>& left, const Rect<T>& right)
148{
149 return (left.left == right.left) && (left.width == right.width) &&
150 (left.top == right.top) && (left.height == right.height);
151}
153
155template <typename T>
156inline bool operator !=(const Rect<T>& left, const Rect<T>& right)
157{
158 return !(left == right);
159}
Utility class for manipulating 2D axis aligned rectangles.
Definition: Rect.hpp:43
Rect()
Default constructor.
bool contains(T x, T y) const
Check if a point is inside the rectangle's area.
Definition: Rect.inl:76
T width
Width of the rectangle.
Definition: Rect.hpp:159
T height
Height of the rectangle.
Definition: Rect.hpp:160
T left
Left coordinate of the rectangle.
Definition: Rect.hpp:157
T top
Top coordinate of the rectangle.
Definition: Rect.hpp:158
bool intersects(const Rect< T > &rectangle) const
Check the intersection between two rectangles.
Definition: Rect.inl:100
Utility template class for manipulating 2-dimensional vectors.
Definition: Vector2.hpp:38
T x
X coordinate of the vector.
Definition: Vector2.hpp:75
T y
Y coordinate of the vector.
Definition: Vector2.hpp:76
SFML_NETWORK_API bool operator==(const IpAddress &left, const IpAddress &right)
Overload of == operator to compare two IP addresses.
SFML_NETWORK_API bool operator!=(const IpAddress &left, const IpAddress &right)
Overload of != operator to compare two IP addresses.