Rect.cpp revision 59332804306c054082a39e9b004146bd03ae1d30
1/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <system/graphics.h>
18#include <ui/Rect.h>
19
20namespace android {
21
22static inline int32_t min(int32_t a, int32_t b) {
23    return (a<b) ? a : b;
24}
25
26static inline int32_t max(int32_t a, int32_t b) {
27    return (a>b) ? a : b;
28}
29
30void Rect::makeInvalid() {
31    left = 0;
32    top = 0;
33    right = -1;
34    bottom = -1;
35}
36
37bool Rect::operator < (const Rect& rhs) const
38{
39    if (top<rhs.top) {
40        return true;
41    } else if (top == rhs.top) {
42        if (left < rhs.left) {
43            return true;
44        } else if (left == rhs.left) {
45            if (bottom<rhs.bottom) {
46                return true;
47            } else if (bottom == rhs.bottom) {
48                if (right<rhs.right) {
49                    return true;
50                }
51            }
52        }
53    }
54    return false;
55}
56
57Rect& Rect::offsetTo(int32_t x, int32_t y)
58{
59    right -= left - x;
60    bottom -= top - y;
61    left = x;
62    top = y;
63    return *this;
64}
65
66Rect& Rect::offsetBy(int32_t x, int32_t y)
67{
68    left += x;
69    top  += y;
70    right+= x;
71    bottom+=y;
72    return *this;
73}
74
75const Rect Rect::operator + (const Point& rhs) const
76{
77    const Rect result(left+rhs.x, top+rhs.y, right+rhs.x, bottom+rhs.y);
78    return result;
79}
80
81const Rect Rect::operator - (const Point& rhs) const
82{
83    const Rect result(left-rhs.x, top-rhs.y, right-rhs.x, bottom-rhs.y);
84    return result;
85}
86
87bool Rect::intersect(const Rect& with, Rect* result) const
88{
89    result->left    = max(left, with.left);
90    result->top     = max(top, with.top);
91    result->right   = min(right, with.right);
92    result->bottom  = min(bottom, with.bottom);
93    return !(result->isEmpty());
94}
95
96Rect Rect::transform(uint32_t xform, int32_t width, int32_t height) {
97    Rect result(*this);
98    if (xform & HAL_TRANSFORM_FLIP_H) {
99        result = Rect(width - result.right, result.top,
100                width - result.left, result.bottom);
101    }
102    if (xform & HAL_TRANSFORM_FLIP_V) {
103        result = Rect(result.left, height - result.bottom,
104                result.right, height - result.top);
105    }
106    if (xform & HAL_TRANSFORM_ROT_90) {
107        int left = height - result.bottom;
108        int top = result.left;
109        int right = height - result.top;
110        int bottom = result.right;
111        result = Rect(left, top, right, bottom);
112    }
113    return result;
114}
115
116}; // namespace android
117