1
Feature requests / sf::Rect intersects & contains - unsigned version
« on: July 08, 2016, 03:39:38 pm »
If u don't know much about QuadTree, then please read a bit of it: https://en.wikipedia.org/wiki/Quadtree
The idea is to make separate functions for unsigned version of ::intersects & ::contains, imagine you use QuadTree in your game server to detect collisions or objects around any given point, but since this can be repeated hundreds or even thousands times per frame - checking whether the number is negative or positive several times in one function can take some performance down, so i decided to make "unsigned" version to calculate and compare numbers as fast as possible because i use only number going from 0.f. People like me can benefit from this.
This is purely about Performance, so those functions can be called like: ::intersectsUnsigned && ::containsUnsigned
in my floatRect class i use two sf::Vectors to store position and size.
The idea is to make separate functions for unsigned version of ::intersects & ::contains, imagine you use QuadTree in your game server to detect collisions or objects around any given point, but since this can be repeated hundreds or even thousands times per frame - checking whether the number is negative or positive several times in one function can take some performance down, so i decided to make "unsigned" version to calculate and compare numbers as fast as possible because i use only number going from 0.f. People like me can benefit from this.
This is purely about Performance, so those functions can be called like: ::intersectsUnsigned && ::containsUnsigned
in my floatRect class i use two sf::Vectors to store position and size.
float floatRect::x() const
{
return m_position.x;
}
float floatRect::y() const
{
return m_position.y;
}
float floatRect::w() const
{
return m_size.x;
}
float floatRect::h() const
{
return m_size.y;
}
bool floatRect::contains(const float _x, const float _y) const
{
/*const float minX = std::min(m_position.x, m_position.x + m_size.x);
const float maxX = std::max(m_position.x, m_position.x + m_size.x);
const float minY = std::min(m_position.y, m_position.y + m_size.y);
const float maxY = std::max(m_position.y, m_position.y + m_size.y);
return (x >= minX) && (x < maxX) && (y >= minY) && (y < maxY);*/
const bool b1 = x() <= _x;
const bool b2 = y() <= _y;
const bool b3 = x() + w() >= _x;
const bool b4 = y() + h() >= _y;
return b1 && b2 && b3 && b4;
}
bool floatRect::intersects(const floatRect& _rect) const
{
/*
const float r1MinX = std::min(m_position.x, m_position.x + m_size.x);
const float r1MinY = std::min(m_position.y, m_position.y + m_size.y);
const float r2MinX = std::min(_rect.m_position.x, _rect.m_position.x + _rect.m_size.x);
const float r2MinY = std::min(_rect.m_position.y, _rect.m_position.y + _rect.m_size.y);
const float r1MaxX = std::max(m_position.x, m_position.x + m_size.x);
const float r1MaxY = std::max(m_position.y, m_position.y + m_size.y);
const float r2MaxX = std::max(_rect.m_position.x, _rect.m_position.x + _rect.m_size.x);
const float r2MaxY = std::max(_rect.m_position.y, _rect.m_position.y + _rect.m_size.y);
const float interLeft = std::max(r1MinX, r2MinX);
const float interTop = std::max(r1MinY, r2MinY);
const float interRight = std::min(r1MaxX, r2MaxX);
const float interBottom = std::min(r1MaxY, r2MaxY);
if ((interLeft < interRight) && (interTop < interBottom))
return true;
return false;*/
const bool b1 = x() < _rect.x() + _rect.w();
const bool b2 = y() < _rect.y() + _rect.h();
const bool b3 = x() + w() > _rect.x();
const bool b4 = h() + y() > _rect.y();
return b1 && b2 && b3 && b4;
}
{
return m_position.x;
}
float floatRect::y() const
{
return m_position.y;
}
float floatRect::w() const
{
return m_size.x;
}
float floatRect::h() const
{
return m_size.y;
}
bool floatRect::contains(const float _x, const float _y) const
{
/*const float minX = std::min(m_position.x, m_position.x + m_size.x);
const float maxX = std::max(m_position.x, m_position.x + m_size.x);
const float minY = std::min(m_position.y, m_position.y + m_size.y);
const float maxY = std::max(m_position.y, m_position.y + m_size.y);
return (x >= minX) && (x < maxX) && (y >= minY) && (y < maxY);*/
const bool b1 = x() <= _x;
const bool b2 = y() <= _y;
const bool b3 = x() + w() >= _x;
const bool b4 = y() + h() >= _y;
return b1 && b2 && b3 && b4;
}
bool floatRect::intersects(const floatRect& _rect) const
{
/*
const float r1MinX = std::min(m_position.x, m_position.x + m_size.x);
const float r1MinY = std::min(m_position.y, m_position.y + m_size.y);
const float r2MinX = std::min(_rect.m_position.x, _rect.m_position.x + _rect.m_size.x);
const float r2MinY = std::min(_rect.m_position.y, _rect.m_position.y + _rect.m_size.y);
const float r1MaxX = std::max(m_position.x, m_position.x + m_size.x);
const float r1MaxY = std::max(m_position.y, m_position.y + m_size.y);
const float r2MaxX = std::max(_rect.m_position.x, _rect.m_position.x + _rect.m_size.x);
const float r2MaxY = std::max(_rect.m_position.y, _rect.m_position.y + _rect.m_size.y);
const float interLeft = std::max(r1MinX, r2MinX);
const float interTop = std::max(r1MinY, r2MinY);
const float interRight = std::min(r1MaxX, r2MaxX);
const float interBottom = std::min(r1MaxY, r2MaxY);
if ((interLeft < interRight) && (interTop < interBottom))
return true;
return false;*/
const bool b1 = x() < _rect.x() + _rect.w();
const bool b2 = y() < _rect.y() + _rect.h();
const bool b3 = x() + w() > _rect.x();
const bool b4 = h() + y() > _rect.y();
return b1 && b2 && b3 && b4;
}