Dan Stoza | 71bded5 | 2016-10-19 11:10:33 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 | #pragma once |
| 18 | |
Lloyd Pique | ea62928 | 2019-12-03 15:57:10 -0800 | [diff] [blame] | 19 | #include <ostream> |
| 20 | |
Dan Stoza | 71bded5 | 2016-10-19 11:10:33 -0700 | [diff] [blame] | 21 | namespace android { |
Dan Stoza | 71bded5 | 2016-10-19 11:10:33 -0700 | [diff] [blame] | 22 | |
| 23 | class FloatRect { |
Dan Stoza | 5a423ea | 2017-02-16 14:10:39 -0800 | [diff] [blame] | 24 | public: |
Dan Stoza | 71bded5 | 2016-10-19 11:10:33 -0700 | [diff] [blame] | 25 | FloatRect() = default; |
| 26 | constexpr FloatRect(float _left, float _top, float _right, float _bottom) |
| 27 | : left(_left), top(_top), right(_right), bottom(_bottom) {} |
| 28 | |
| 29 | float getWidth() const { return right - left; } |
| 30 | float getHeight() const { return bottom - top; } |
| 31 | |
Dan Stoza | 80d6116 | 2017-12-20 15:57:52 -0800 | [diff] [blame] | 32 | FloatRect intersect(const FloatRect& other) const { |
Eliot Courtney | 38d8d9a | 2018-06-25 20:28:30 +0900 | [diff] [blame] | 33 | FloatRect intersection = { |
Dan Stoza | 80d6116 | 2017-12-20 15:57:52 -0800 | [diff] [blame] | 34 | // Inline to avoid tromping on other min/max defines or adding a |
| 35 | // dependency on STL |
| 36 | (left > other.left) ? left : other.left, |
| 37 | (top > other.top) ? top : other.top, |
| 38 | (right < other.right) ? right : other.right, |
| 39 | (bottom < other.bottom) ? bottom : other.bottom |
| 40 | }; |
Eliot Courtney | 38d8d9a | 2018-06-25 20:28:30 +0900 | [diff] [blame] | 41 | if (intersection.getWidth() < 0 || intersection.getHeight() < 0) { |
| 42 | return {0, 0, 0, 0}; |
| 43 | } |
| 44 | return intersection; |
Dan Stoza | 80d6116 | 2017-12-20 15:57:52 -0800 | [diff] [blame] | 45 | } |
| 46 | |
Dan Stoza | 71bded5 | 2016-10-19 11:10:33 -0700 | [diff] [blame] | 47 | float left = 0.0f; |
| 48 | float top = 0.0f; |
| 49 | float right = 0.0f; |
| 50 | float bottom = 0.0f; |
| 51 | }; |
| 52 | |
| 53 | inline bool operator==(const FloatRect& a, const FloatRect& b) { |
| 54 | return a.left == b.left && a.top == b.top && a.right == b.right && a.bottom == b.bottom; |
| 55 | } |
| 56 | |
Lloyd Pique | ea62928 | 2019-12-03 15:57:10 -0800 | [diff] [blame] | 57 | static inline void PrintTo(const FloatRect& rect, ::std::ostream* os) { |
| 58 | *os << "FloatRect(" << rect.left << ", " << rect.top << ", " << rect.right << ", " |
| 59 | << rect.bottom << ")"; |
| 60 | } |
| 61 | |
Dan Stoza | 71bded5 | 2016-10-19 11:10:33 -0700 | [diff] [blame] | 62 | } // namespace android |