1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Eclipse Public License, Version 1.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.eclipse.org/org/documents/epl-v10.php
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.ide.common.api;
/**
* Mutable rectangle bounds.
* <p/>
* To be valid, w >= 1 and h >= 1.
* By definition:
* - right side = x + w - 1.
* - bottom side = y + h - 1.
* <p>
* <b>NOTE: This is not a public or final API; if you rely on this be prepared
* to adjust your code for the next tools release.</b>
* </p>
*/
public class Rect {
public int x, y, w, h;
/** Initialize an invalid rectangle. */
public Rect() {
}
/** Initialize rectangle to the given values. They can be invalid. */
public Rect(int x, int y, int w, int h) {
set(x, y, w, h);
}
/** Initialize rectangle to the given values. They can be invalid. */
public Rect(Rect r) {
set(r);
}
/** Initialize rectangle to the given values. They can be invalid. */
public Rect set(int x, int y, int w, int h) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
return this;
}
/** Initialize rectangle to match the given one. */
public Rect set(Rect r) {
set(r.x, r.y, r.w, r.h);
return this;
}
/** Returns a new instance of a rectangle with the same values. */
public Rect copy() {
return new Rect(x, y, w, h);
}
/** Returns true if the rectangle has valid bounds, i.e. w>0 and h>0. */
public boolean isValid() {
return w > 0 && h > 0;
}
/** Returns true if the rectangle contains the x,y coordinates, borders included. */
public boolean contains(int x, int y) {
return isValid() &&
x >= this.x &&
y >= this.y &&
x < (this.x + this.w) &&
y < (this.y + this.h);
}
/** Returns true if the rectangle contains the x,y coordinates, borders included. */
public boolean contains(Point p) {
return p != null && contains(p.x, p.y);
}
/**
* Moves this rectangle by setting it's x,y coordinates to the new values.
* @return Returns self, for chaining.
*/
public Rect moveTo(int x, int y) {
this.x = x;
this.y = y;
return this;
}
/**
* Offsets this rectangle by adding the given x,y deltas to the x,y coordinates.
* @return Returns self, for chaining.
*/
public Rect offsetBy(int x, int y) {
this.x += x;
this.y += y;
return this;
}
public Point getCenter() {
return new Point(x + (w > 0 ? w / 2 : 0),
y + (h > 0 ? h / 2 : 0));
}
public Point getTopLeft() {
return new Point(x, y);
}
public Point getBottomLeft() {
return new Point(x,
y + (h > 0 ? h : 0));
}
public Point getTopRight() {
return new Point(x + (w > 0 ? w : 0),
y);
}
public Point getBottomRight() {
return new Point(x + (w > 0 ? w : 0),
y + (h > 0 ? h : 0));
}
@Override
public String toString() {
return String.format("Rect [%dx%d - %dx%d]", x, y, w, h);
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Rect) {
Rect rhs = (Rect) obj;
// validity must be equal on both sides.
if (isValid() != rhs.isValid()) {
return false;
}
// an invalid rect is equal to any other invalid rect regardless of coordinates
if (!isValid() && !rhs.isValid()) {
return true;
}
return this.x == rhs.x && this.y == rhs.y && this.w == rhs.w && this.h == rhs.h;
}
return false;
}
@Override
public int hashCode() {
int hc = x;
hc ^= ((y >> 8) & 0x0FFFFFF) | ((y & 0x00000FF) << 24);
hc ^= ((w >> 16) & 0x000FFFF) | ((w & 0x000FFFF) << 16);
hc ^= ((h >> 24) & 0x00000FF) | ((h & 0x0FFFFFF) << 8);
return hc;
}
}
|