Rect.cpp revision edbf3b6af777b721cd2a1ef461947e51e88241e1
1/*
2 *  Rect.cpp
3 *  Android
4 *
5 *  Created on 10/14/05.
6 *  Copyright 2005 The Android Open Source Project
7 *
8 */
9
10#include <ui/Rect.h>
11
12namespace android {
13
14inline int min(int a, int b) {
15    return (a<b) ? a : b;
16}
17
18inline int max(int a, int b) {
19    return (a>b) ? a : b;
20}
21
22void Rect::makeInvalid() {
23    left = 0;
24    top = 0;
25    right = -1;
26    bottom = -1;
27}
28
29bool Rect::operator < (const Rect& rhs) const
30{
31    if (top<rhs.top) {
32        return true;
33    } else if (top == rhs.top) {
34        if (left < rhs.left) {
35            return true;
36        } else if (left == rhs.left) {
37            if (bottom<rhs.bottom) {
38                return true;
39            } else if (bottom == rhs.bottom) {
40                if (right<rhs.right) {
41                    return true;
42                }
43            }
44        }
45    }
46    return false;
47}
48
49Rect& Rect::offsetTo(int x, int y)
50{
51    right -= left - x;
52    bottom -= top - y;
53    left = x;
54    top = y;
55    return *this;
56}
57
58Rect& Rect::offsetBy(int x, int y)
59{
60    left += x;
61    top  += y;
62    right+= x;
63    bottom+=y;
64    return *this;
65}
66
67Rect Rect::operator + (const Point& rhs) const
68{
69    return Rect(left+rhs.x, top+rhs.y, right+rhs.x, bottom+rhs.y);
70}
71
72Rect Rect::operator - (const Point& rhs) const
73{
74    return Rect(left-rhs.x, top-rhs.y, right-rhs.x, bottom-rhs.y);
75}
76
77bool Rect::intersect(const Rect& with, Rect* result) const
78{
79    result->left    = max(left, with.left);
80    result->top     = max(top, with.top);
81    result->right   = min(right, with.right);
82    result->bottom  = min(bottom, with.bottom);
83    return !(result->isEmpty());
84}
85
86}; // namespace android
87