Add sf::Rect<T>::getCenter()

This commit is contained in:
Chris Thrasher 2023-11-12 21:05:30 -07:00
parent 0d4c34cf9b
commit d3a79e6282
5 changed files with 28 additions and 4 deletions

View File

@ -109,7 +109,7 @@ public:
/// ///
/// \return Position of rectangle /// \return Position of rectangle
/// ///
/// \see getSize /// \see getSize, getCenter
/// ///
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
constexpr Vector2<T> getPosition() const; constexpr Vector2<T> getPosition() const;
@ -119,11 +119,21 @@ public:
/// ///
/// \return Size of rectangle /// \return Size of rectangle
/// ///
/// \see getPosition /// \see getPosition, getCenter
/// ///
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
constexpr Vector2<T> getSize() const; constexpr Vector2<T> getSize() const;
////////////////////////////////////////////////////////////
/// \brief Get the position of the center of the rectangle
///
/// \return Center of rectangle
///
/// \see getSize, getPosition
///
////////////////////////////////////////////////////////////
constexpr Vector2<T> getCenter() const;
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
// Member data // Member data
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////

View File

@ -127,6 +127,14 @@ constexpr Vector2<T> Rect<T>::getSize() const
} }
////////////////////////////////////////////////////////////
template <typename T>
constexpr Vector2<T> Rect<T>::getCenter() const
{
return getPosition() + getSize() / T{2};
}
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
template <typename T> template <typename T>
constexpr bool operator==(const Rect<T>& left, const Rect<T>& right) constexpr bool operator==(const Rect<T>& left, const Rect<T>& right)

View File

@ -221,7 +221,7 @@ void Shape::update()
m_insideBounds = m_vertices.getBounds(); m_insideBounds = m_vertices.getBounds();
// Compute the center and make it the first vertex // Compute the center and make it the first vertex
m_vertices[0].position = m_insideBounds.getPosition() + m_insideBounds.getSize() / 2.f; m_vertices[0].position = m_insideBounds.getCenter();
// Color // Color
updateFillColors(); updateFillColors();

View File

@ -90,7 +90,7 @@ void View::setViewport(const FloatRect& viewport)
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void View::reset(const FloatRect& rectangle) void View::reset(const FloatRect& rectangle)
{ {
m_center = rectangle.getPosition() + rectangle.getSize() / 2.f; m_center = rectangle.getCenter();
m_size = rectangle.getSize(); m_size = rectangle.getSize();
m_rotation = Angle::Zero; m_rotation = Angle::Zero;

View File

@ -103,6 +103,12 @@ TEST_CASE("[Graphics] sf::Rect")
STATIC_CHECK(sf::IntRect({1, 2}, {3, 4}).getSize() == sf::Vector2i(3, 4)); STATIC_CHECK(sf::IntRect({1, 2}, {3, 4}).getSize() == sf::Vector2i(3, 4));
} }
SECTION("getCenter()")
{
STATIC_CHECK(sf::IntRect({}, {}).getCenter() == sf::Vector2i());
STATIC_CHECK(sf::IntRect({1, 2}, {4, 6}).getCenter() == sf::Vector2i(3, 5));
}
SECTION("Operators") SECTION("Operators")
{ {
SECTION("operator==") SECTION("operator==")