add SkRect::intersect(a, b), matching what was already available in SkIRect

git-svn-id: http://skia.googlecode.com/svn/trunk@2513 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
reed@google.com 2011-10-21 14:05:06 +00:00
parent cb6c2f4667
commit f0f617a502
2 changed files with 21 additions and 0 deletions

View File

@ -485,6 +485,12 @@ struct SK_API SkRect {
fLeft < right && left < fRight &&
fTop < bottom && top < fBottom;
}
/** If rectangles a and b intersect, return true and set this rectangle to
* that intersection, otherwise return false and do not change this
* rectangle. If either rectangle is empty, do nothing and return false.
*/
bool intersect(const SkRect& a, const SkRect& b);
/**
* Return true if rectangles a and b are not empty and intersect.

View File

@ -122,6 +122,21 @@ bool SkRect::intersect(const SkRect& r) {
return this->intersect(r.fLeft, r.fTop, r.fRight, r.fBottom);
}
bool SkRect::intersect(const SkRect& a, const SkRect& b) {
SkASSERT(&a && &b);
if (!a.isEmpty() && !b.isEmpty() &&
a.fLeft < b.fRight && b.fLeft < a.fRight &&
a.fTop < b.fBottom && b.fTop < a.fBottom) {
fLeft = SkMaxScalar(a.fLeft, b.fLeft);
fTop = SkMaxScalar(a.fTop, b.fTop);
fRight = SkMinScalar(a.fRight, b.fRight);
fBottom = SkMinScalar(a.fBottom, b.fBottom);
return true;
}
return false;
}
void SkRect::join(SkScalar left, SkScalar top, SkScalar right,
SkScalar bottom) {
// do nothing if the params are empty