1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
template <typename T>
bool Rect<T>::intersects(const Rect<T>& rectangle, Rect<T>& intersection) const
{
// Rectangles with negative dimensions are allowed, so we must handle them correctly
// Compute the min and max of the first rectangle on both axes
T r1MinX = std::min(left, static_cast<T>(left + width));
T r1MaxX = std::max(left, static_cast<T>(left + width));
T r1MinY = std::min(top, static_cast<T>(top + height));
T r1MaxY = std::max(top, static_cast<T>(top + height));
// Compute the min and max of the second rectangle on both axes
T r2MinX = std::min(rectangle.left, static_cast<T>(rectangle.left + rectangle.width));
T r2MaxX = std::max(rectangle.left, static_cast<T>(rectangle.left + rectangle.width));
T r2MinY = std::min(rectangle.top, static_cast<T>(rectangle.top + rectangle.height));
T r2MaxY = std::max(rectangle.top, static_cast<T>(rectangle.top + rectangle.height));
// Compute the intersection boundaries
T interLeft = std::max(r1MinX, r2MinX);
T interTop = std::max(r1MinY, r2MinY);
T interRight = std::min(r1MaxX, r2MaxX);
T interBottom = std::min(r1MaxY, r2MaxY);
// If the intersection is valid (positive non zero area), then there is an intersection
if ((interLeft < interRight) && (interTop < interBottom))
{
intersection = Rect<T>(interLeft, interTop, interRight - interLeft, interBottom - interTop);
return true;
}
else
{
intersection = Rect<T>(0, 0, 0, 0);
return false;
}
}
| |