Fixed the long outstanding bug that made BRect::Intersects() return false when one rect was completely contained into the other. Cleaned up (a bit) the code.

git-svn-id: file:///srv/svn/repos/haiku/trunk/current@5041 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Stefano Ceccherini
2003-10-16 08:00:44 +00:00
parent 0fc3ae8c5c
commit b17e9812fd
+91 -84
View File
@@ -1,5 +1,5 @@
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Copyright (c) 2001-2002, OpenBeOS // Copyright (c) 2001-2003, OpenBeOS
// //
// Permission is hereby granted, free of charge, to any person obtaining a // Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"), // copy of this software and associated documentation files (the "Software"),
@@ -38,19 +38,19 @@
// Globals --------------------------------------------------------------------- // Globals ---------------------------------------------------------------------
bool TestLineIntersect(const BRect& r, float x1, float y1, float x2, float y2,
bool vertical = true);
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
void BRect::InsetBy(BPoint p) void
BRect::InsetBy(BPoint point)
{ {
left += p.x; left += point.x;
right -= p.x; right -= point.x;
top += p.y; top += point.y;
bottom -= p.y; bottom -= point.y;
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
void BRect::InsetBy(float dx, float dy) void
BRect::InsetBy(float dx, float dy)
{ {
left += dx; left += dx;
right -= dx; right -= dx;
@@ -58,41 +58,47 @@ void BRect::InsetBy(float dx, float dy)
bottom -= dy; bottom -= dy;
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
BRect& BRect::InsetBySelf(BPoint p) BRect&
BRect::InsetBySelf(BPoint point)
{ {
this->InsetBy(p); InsetBy(point);
return *this; return *this;
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
BRect& BRect::InsetBySelf(float dx, float dy) BRect&
BRect::InsetBySelf(float dx, float dy)
{ {
this->InsetBy(dx, dy); InsetBy(dx, dy);
return *this; return *this;
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
BRect BRect::InsetByCopy(BPoint p) BRect
BRect::InsetByCopy(BPoint point)
{ {
BRect copy(*this); BRect copy(*this);
copy.InsetBy(p); copy.InsetBy(point);
return copy; return copy;
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
BRect BRect::InsetByCopy(float dx, float dy) BRect
BRect::InsetByCopy(float dx, float dy)
{ {
BRect copy(*this); BRect copy(*this);
copy.InsetBy(dx, dy); copy.InsetBy(dx, dy);
return copy; return copy;
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
void BRect::OffsetBy(BPoint p) void
BRect::OffsetBy(BPoint point)
{ {
left += p.x; left += point.x;
right += p.x; right += point.x;
top += p.y; top += point.y;
bottom += p.y; bottom += point.y;
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
void BRect::OffsetBy(float dx, float dy) void
BRect::OffsetBy(float dx, float dy)
{ {
left += dx; left += dx;
right += dx; right += dx;
@@ -100,41 +106,47 @@ void BRect::OffsetBy(float dx, float dy)
bottom += dy; bottom += dy;
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
BRect& BRect::OffsetBySelf(BPoint p) BRect&
BRect::OffsetBySelf(BPoint point)
{ {
this->OffsetBy(p); OffsetBy(point);
return *this; return *this;
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
BRect& BRect::OffsetBySelf(float dx, float dy) BRect&
BRect::OffsetBySelf(float dx, float dy)
{ {
this->OffsetBy(dx, dy); OffsetBy(dx, dy);
return *this; return *this;
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
BRect BRect::OffsetByCopy(BPoint p) BRect
BRect::OffsetByCopy(BPoint point)
{ {
BRect copy(*this); BRect copy(*this);
copy.OffsetBy(p); copy.OffsetBy(point);
return copy; return copy;
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
BRect BRect::OffsetByCopy(float dx, float dy) BRect
BRect::OffsetByCopy(float dx, float dy)
{ {
BRect copy(*this); BRect copy(*this);
copy.OffsetBy(dx, dy); copy.OffsetBy(dx, dy);
return copy; return copy;
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
void BRect::OffsetTo(BPoint p) void
BRect::OffsetTo(BPoint point)
{ {
right = (right - left) + p.x; right = (right - left) + point.x;
left = p.x; left = point.x;
bottom = (bottom - top) + p.y; bottom = (bottom - top) + point.y;
top = p.y; top = point.y;
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
void BRect::OffsetTo(float x, float y) void
BRect::OffsetTo(float x, float y)
{ {
right = (right - left) + x; right = (right - left) + x;
left = x; left = x;
@@ -142,100 +154,95 @@ void BRect::OffsetTo(float x, float y)
top=y; top=y;
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
BRect& BRect::OffsetToSelf(BPoint p) BRect&
BRect::OffsetToSelf(BPoint point)
{ {
this->OffsetTo(p); OffsetTo(point);
return *this; return *this;
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
BRect& BRect::OffsetToSelf(float dx, float dy) BRect&
BRect::OffsetToSelf(float dx, float dy)
{ {
this->OffsetTo(dx, dy); OffsetTo(dx, dy);
return *this; return *this;
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
BRect BRect::OffsetToCopy(BPoint p) BRect
BRect::OffsetToCopy(BPoint point)
{ {
BRect copy(*this); BRect copy(*this);
copy.OffsetTo(p); copy.OffsetTo(point);
return copy; return copy;
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
BRect BRect::OffsetToCopy(float dx, float dy) BRect
BRect::OffsetToCopy(float dx, float dy)
{ {
BRect copy(*this); BRect copy(*this);
copy.OffsetTo(dx, dy); copy.OffsetTo(dx, dy);
return copy; return copy;
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
void BRect::PrintToStream() const void
BRect::PrintToStream() const
{ {
printf("(l:%.1f t:%.1f r:%.1f b:%.1f)\n", left, top, right, bottom); printf("BRect(%.1f, %.1f, %.1f, %.1f)\n", left, top, right, bottom);
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
bool BRect::operator==(BRect r) const bool
BRect::operator==(BRect rect) const
{ {
return left == r.left && right == r.right && return left == rect.left && right == rect.right &&
top == r.top && bottom == r.bottom; top == rect.top && bottom == rect.bottom;
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
bool BRect::operator!=(BRect r) const bool
BRect::operator!=(BRect rect) const
{ {
return !(*this == r); return !(*this == rect);
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
BRect BRect::operator&(BRect r) const BRect
BRect::operator&(BRect rect) const
{ {
return BRect(max_c(left, r.left), max_c(top, r.top), return BRect(max_c(left, rect.left), max_c(top, rect.top),
min_c(right, r.right), min_c(bottom, r.bottom)); min_c(right, rect.right), min_c(bottom, rect.bottom));
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
BRect BRect::operator|(BRect r) const BRect
BRect::operator|(BRect rect) const
{ {
return BRect(min_c(left, r.left), min_c(top, r.top), return BRect(min_c(left, rect.left), min_c(top, rect.top),
max_c(right, r.right), max_c(bottom, r.bottom)); max_c(right, rect.right), max_c(bottom, rect.bottom));
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
bool BRect::Intersects(BRect r) const bool
BRect::Intersects(BRect rect) const
{ {
return TestLineIntersect(*this, r.left, r.top, r.left, r.bottom) || if (!(rect.left > right || rect.right < left
TestLineIntersect(*this, r.left, r.top, r.right, r.top, false) || || rect.top > bottom || rect.bottom < top))
TestLineIntersect(*this, r.right, r.top, r.right, r.bottom) || return true;
TestLineIntersect(*this, r.left, r.bottom, r.right, r.bottom, false);
return false;
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
bool BRect::Contains(BPoint p) const bool
BRect::Contains(BPoint point) const
{ {
return p.x >= left && p.x <= right && p.y >= top && p.y <= bottom; return point.x >= left && point.x <= right
&& point.y >= top && point.y <= bottom;
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
bool BRect::Contains(BRect r) const bool
BRect::Contains(BRect rect) const
{ {
return r.left >= left && r.right <= right && return rect.left >= left && rect.right <= right
r.top >= top && r.bottom <= bottom; && rect.top >= top && rect.bottom <= bottom;
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
//------------------------------------------------------------------------------
bool TestLineIntersect(const BRect& r, float x1, float y1, float x2, float y2,
bool vertical)
{
if (vertical)
{
return (x1 >= r.left && x1 <= r.right) &&
((y1 >= r.top && y1 <= r.bottom) ||
(y2 >= r.top && y2 <= r.bottom));
}
else
{
return (y1 >= r.top && y1 <= r.bottom) &&
((x1 >= r.left && x1 <= r.right) ||
(x2 >= r.left && x2 <= r.right));
}
}
//------------------------------------------------------------------------------
/* /*
* $Log $ * $Log $
* *