Add get_rect_union() for future usage.

This commit is contained in:
Mark de Wever 2009-01-01 01:03:16 +00:00
parent 463d153779
commit f1c73760c9
2 changed files with 33 additions and 0 deletions

View File

@ -83,6 +83,29 @@ SDL_Rect intersect_rects(SDL_Rect const &rect1, SDL_Rect const &rect2)
return res;
}
SDL_Rect get_rect_union(const SDL_Rect& rect1, const SDL_Rect& rect2)
{
const int left_side = std::max(rect1.x, rect2.x);
const int right_side = std::min(rect1.x + rect1.w, rect2.x + rect2.w);
if(left_side > right_side) {
return empty_rect;
}
const int top_side = std::max(rect1.y, rect2.y);
const int bottom_side = std::min(rect1.y + rect1.h, rect2.y + rect2.h);
if(top_side > bottom_side) {
return empty_rect;
}
SDL_Rect result = {
left_side,
top_side,
right_side - left_side,
bottom_side - top_side};
return result;
}
SDL_Rect create_rect(const int x, const int y, const int w, const int h)
{
SDL_Rect rect = { x, y, w, h };

View File

@ -57,6 +57,16 @@ bool point_in_rect(int x, int y, const SDL_Rect& rect);
bool rects_overlap(const SDL_Rect& rect1, const SDL_Rect& rect2);
SDL_Rect intersect_rects(SDL_Rect const &rect1, SDL_Rect const &rect2);
/**
* Returns the union of two rectangles.
*
* @param rect1 The first rectangle.
* @param rect2 The second rectangle.
*
* @returns The union of rect1 and rect2.
*/
SDL_Rect get_rect_union(const SDL_Rect& rect1, const SDL_Rect& rect2);
/**
* Creates an empty SDL_Rect.
*