Replaced usage of get_union_rect by intersect_rects,

...as the function was actually computing the intersection of two
rectangles. Added a proper function for computing unions.
This commit is contained in:
Guillaume Melquiond 2010-08-10 12:08:19 +00:00
parent 46ab9253dd
commit e4ca82489c
4 changed files with 19 additions and 36 deletions

View File

@ -390,8 +390,7 @@ void tscrollbar_container::set_visible_area(const SDL_Rect& area)
tcontainer_::set_visible_area(area);
// Now get the visible part of the content.
content_visible_area_ =
get_rect_union(area, content_->get_rect());
content_visible_area_ = intersect_rects(area, content_->get_rect());
content_grid_->set_visible_area(content_visible_area_);
}

View File

@ -238,7 +238,7 @@ twidget::tdrawing_action twidget::get_drawing_action() const
void twidget::set_visible_area(const SDL_Rect& area)
{
clip_rect_ = get_rect_union(area, get_rect());
clip_rect_ = intersect_rects(area, get_rect());
if(clip_rect_ == get_rect()) {
drawing_action_ = DRAWN;

View File

@ -106,32 +106,26 @@ SDL_Rect intersect_rects(SDL_Rect const &rect1, SDL_Rect const &rect2)
SDL_Rect res;
res.x = std::max<int>(rect1.x, rect2.x);
res.y = std::max<int>(rect1.y, rect2.y);
res.w = std::max<int>(std::min<int>(rect1.x + rect1.w, rect2.x + rect2.w) - res.x, 0);
res.h = std::max<int>(std::min<int>(rect1.y + rect1.h, rect2.y + rect2.h) - res.y, 0);
int w = std::min<int>(rect1.x + rect1.w, rect2.x + rect2.w) - res.x;
int h = std::min<int>(rect1.y + rect1.h, rect2.y + rect2.h) - res.y;
if (w <= 0 || h <= 0) return empty_rect;
res.w = w;
res.h = h;
return res;
}
SDL_Rect get_rect_union(SDL_Rect const &rect1, SDL_Rect const& 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 = create_rect(
left_side,
top_side,
right_side - left_side,
bottom_side - top_side);
return result;
SDL_Rect union_rects(SDL_Rect const &rect1, SDL_Rect const &rect2)
{
if (rect1.w == 0 || rect1.h == 0) return rect2;
if (rect2.w == 0 || rect2.h == 0) return rect1;
SDL_Rect res;
res.x = std::min<int>(rect1.x, rect2.x);
res.y = std::min<int>(rect1.y, rect2.y);
res.w = std::max<int>(rect1.x + rect1.w, rect2.x + rect2.w) - res.x;
res.h = std::max<int>(rect1.y + rect1.h, rect2.y + rect2.h) - res.y;
return res;
}
SDL_Rect create_rect(const int x, const int y, const int w, const int h)
{
SDL_Rect rect;

View File

@ -56,18 +56,8 @@ SDLKey sdl_keysym_from_name(std::string const &keyname);
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);
SDL_Rect union_rects(const SDL_Rect &rect1, const SDL_Rect &rect2);
/**
* Returns the union of two rectangles.
*
* @param rect1 The first rectangle.
* @param rect2 The second rectangle.
*
* @returns The union of the two rectangles. If the
* rectangles don't intersect and empty
* rectangle is returned.
*/
SDL_Rect get_rect_union(const SDL_Rect& rect1, const SDL_Rect& rect2);
/**
* Creates an empty SDL_Rect.
*