blob: bec2552fdec3ba5d7f130ffcc16e7b21df033570 [file] [log] [blame]
Dan Stoza71bded52016-10-19 11:10:33 -07001/*
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 Piqueea629282019-12-03 15:57:10 -080019#include <ostream>
20
Dan Stoza71bded52016-10-19 11:10:33 -070021namespace android {
Dan Stoza71bded52016-10-19 11:10:33 -070022
23class FloatRect {
Dan Stoza5a423ea2017-02-16 14:10:39 -080024public:
Dan Stoza71bded52016-10-19 11:10:33 -070025 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 Stoza80d61162017-12-20 15:57:52 -080032 FloatRect intersect(const FloatRect& other) const {
Eliot Courtney38d8d9a2018-06-25 20:28:30 +090033 FloatRect intersection = {
Dan Stoza80d61162017-12-20 15:57:52 -080034 // 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 Courtney38d8d9a2018-06-25 20:28:30 +090041 if (intersection.getWidth() < 0 || intersection.getHeight() < 0) {
42 return {0, 0, 0, 0};
43 }
44 return intersection;
Dan Stoza80d61162017-12-20 15:57:52 -080045 }
46
Dan Stoza71bded52016-10-19 11:10:33 -070047 float left = 0.0f;
48 float top = 0.0f;
49 float right = 0.0f;
50 float bottom = 0.0f;
51};
52
53inline 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 Piqueea629282019-12-03 15:57:10 -080057static inline void PrintTo(const FloatRect& rect, ::std::ostream* os) {
58 *os << "FloatRect(" << rect.left << ", " << rect.top << ", " << rect.right << ", "
59 << rect.bottom << ")";
60}
61
Dan Stoza71bded52016-10-19 11:10:33 -070062} // namespace android