FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Add `sf::Rect<T>::getCenter()` · criptych/SFML@d3a79e6 · GitHub

/ SFML Public
forked from SFML/SFML

Commit d3a79e6

Browse files
committed
Add sf::Rect<T>::getCenter()
1 parent 0d4c34c commit d3a79e6

5 files changed

Lines changed: 28 additions & 4 deletions

File tree

‎include/SFML/Graphics/Rect.hpp‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class Rect
109109
///
110110
/// \return Position of rectangle
111111
///
112-
/// \see getSize
112+
/// \see getSize, getCenter
113113
///
114114
////////////////////////////////////////////////////////////
115115
constexpr Vector2<T> getPosition() const;
@@ -119,11 +119,21 @@ class Rect
119119
///
120120
/// \return Size of rectangle
121121
///
122-
/// \see getPosition
122+
/// \see getPosition, getCenter
123123
///
124124
////////////////////////////////////////////////////////////
125125
constexpr Vector2<T> getSize() const;
126126

127+
////////////////////////////////////////////////////////////
128+
/// \brief Get the position of the center of the rectangle
129+
///
130+
/// \return Center of rectangle
131+
///
132+
/// \see getSize, getPosition
133+
///
134+
////////////////////////////////////////////////////////////
135+
constexpr Vector2<T> getCenter() const;
136+
127137
////////////////////////////////////////////////////////////
128138
// Member data
129139
////////////////////////////////////////////////////////////

‎include/SFML/Graphics/Rect.inl‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,14 @@ constexpr Vector2<T> Rect<T>::getSize() const
127127
}
128128

129129

130+
////////////////////////////////////////////////////////////
131+
template <typename T>
132+
constexpr Vector2<T> Rect<T>::getCenter() const
133+
{
134+
return getPosition() + getSize() / T{2};
135+
}
136+
137+
130138
////////////////////////////////////////////////////////////
131139
template <typename T>
132140
constexpr bool operator==(const Rect<T>& left, const Rect<T>& right)

‎src/SFML/Graphics/Shape.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ void Shape::update()
221221
m_insideBounds = m_vertices.getBounds();
222222

223223
// Compute the center and make it the first vertex
224-
m_vertices[0].position = m_insideBounds.getPosition() + m_insideBounds.getSize() / 2.f;
224+
m_vertices[0].position = m_insideBounds.getCenter();
225225

226226
// Color
227227
updateFillColors();

‎src/SFML/Graphics/View.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ void View::setViewport(const FloatRect& viewport)
9090
////////////////////////////////////////////////////////////
9191
void View::reset(const FloatRect& rectangle)
9292
{
93-
m_center = rectangle.getPosition() + rectangle.getSize() / 2.f;
93+
m_center = rectangle.getCenter();
9494
m_size = rectangle.getSize();
9595
m_rotation = Angle::Zero;
9696

‎test/Graphics/Rect.test.cpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ TEST_CASE("[Graphics] sf::Rect")
103103
STATIC_CHECK(sf::IntRect({1, 2}, {3, 4}).getSize() == sf::Vector2i(3, 4));
104104
}
105105

106+
SECTION("getCenter()")
107+
{
108+
STATIC_CHECK(sf::IntRect({}, {}).getCenter() == sf::Vector2i());
109+
STATIC_CHECK(sf::IntRect({1, 2}, {4, 6}).getCenter() == sf::Vector2i(3, 5));
110+
}
111+
106112
SECTION("Operators")
107113
{
108114
SECTION("operator==")

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL