1 /*
2  * Copyright (C) 2020 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 package com.android.server.wm.traces.common
18 
19 open class Rect(
20     val left: Int = 0,
21     val top: Int = 0,
22     val right: Int = 0,
23     val bottom: Int = 0
24 ) {
25     val height: Int get() = bottom - top
26     val width: Int get() = right - left
centerXnull27     fun centerX(): Int = left + right / 2
28     fun centerY(): Int = top + bottom / 2
29     /**
30      * Returns true if the rectangle is empty (left >= right or top >= bottom)
31      */
32     val isEmpty: Boolean = width == 0 || height == 0
33 
34     val isNotEmpty: Boolean = !isEmpty
35 
36     /**
37      * Returns a [RectF] version fo this rectangle.
38      */
39     fun toRectF(): RectF {
40         return RectF(left.toFloat(), top.toFloat(), right.toFloat(), bottom.toFloat())
41     }
42 
prettyPrintnull43     open fun prettyPrint(): String = prettyPrint(this)
44 
45     override fun equals(other: Any?): Boolean = other?.toString() == this.toString()
46 
47     /**
48      * Returns true iff the specified rectangle r is inside or equal to this
49      * rectangle. An empty rectangle never contains another rectangle.
50      *
51      * @param rect The rectangle being tested for containment.
52      * @return true iff the specified rectangle r is inside or equal to this
53      *              rectangle
54      */
55     operator fun contains(rect: Rect): Boolean {
56         val thisRect = toRectF()
57         val otherRect = rect.toRectF()
58         return thisRect.contains(otherRect)
59     }
60 
61     /**
62      * If the specified rectangle intersects this rectangle, return true and set
63      * this rectangle to that intersection, otherwise return false and do not
64      * change this rectangle. No check is performed to see if either rectangle
65      * is empty. To just test for intersection, use intersects()
66      *
67      * @param rect The rectangle being intersected with this rectangle.
68      * @return A rectangle with the intersection coordinates
69      */
intersectionnull70     fun intersection(rect: Rect): Rect {
71         val thisRect = toRectF()
72         val otherRect = rect.toRectF()
73         return thisRect.intersection(otherRect).toRect()
74     }
75 
hashCodenull76     override fun hashCode(): Int {
77         var result = left
78         result = 31 * result + top
79         result = 31 * result + right
80         result = 31 * result + bottom
81         return result
82     }
83 
toStringnull84     override fun toString(): String = if (isEmpty) "[empty]" else prettyPrint()
85 
86     companion object {
87         val EMPTY = Rect()
88 
89         fun prettyPrint(rect: Rect): String = "(${rect.left}, ${rect.top}) - " +
90             "(${rect.right}, ${rect.bottom})"
91     }
92 }