| 1 |
/* |
| 2 |
* Copyright (C) 2009 The Android Open Source Project |
| 3 |
* |
| 4 |
* Licensed under the Eclipse Public License, Version 1.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.eclipse.org/org/documents/epl-v10.php |
| 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.ide.common.api; |
| 18 |
|
| 19 |
import com.android.annotations.NonNull; |
| 20 |
import com.android.annotations.Nullable; |
| 21 |
import com.google.common.annotations.Beta; |
| 22 |
|
| 23 |
|
| 24 |
/** |
| 25 |
* Mutable rectangle bounds. |
| 26 |
* <p> |
| 27 |
* To be valid, w ≥ 1 and h ≥ 1. |
| 28 |
* By definition: |
| 29 |
* - right side = x + w - 1. |
| 30 |
* - bottom side = y + h - 1. |
| 31 |
* <p> |
| 32 |
* <b>NOTE: This is not a public or final API; if you rely on this be prepared |
| 33 |
* to adjust your code for the next tools release.</b> |
| 34 |
* </p> |
| 35 |
*/ |
| 36 |
@Beta |
| 37 |
public class Rect { |
| 38 |
public int x, y, w, h; |
| 39 |
|
| 40 |
/** Initialize an invalid rectangle. */ |
| 41 |
public Rect() { |
| 42 |
} |
| 43 |
|
| 44 |
/** Initialize rectangle to the given values. They can be invalid. */ |
| 45 |
public Rect(int x, int y, int w, int h) { |
| 46 |
set(x, y, w, h); |
| 47 |
} |
| 48 |
|
| 49 |
/** Initialize rectangle to the given values. They can be invalid. */ |
| 50 |
public Rect(@NonNull Rect r) { |
| 51 |
set(r); |
| 52 |
} |
| 53 |
|
| 54 |
/** Initialize rectangle to the given values. They can be invalid. */ |
| 55 |
@NonNull |
| 56 |
public Rect set(int x, int y, int w, int h) { |
| 57 |
this.x = x; |
| 58 |
this.y = y; |
| 59 |
this.w = w; |
| 60 |
this.h = h; |
| 61 |
return this; |
| 62 |
} |
| 63 |
|
| 64 |
/** Initialize rectangle to match the given one. */ |
| 65 |
@NonNull |
| 66 |
public Rect set(@NonNull Rect r) { |
| 67 |
set(r.x, r.y, r.w, r.h); |
| 68 |
return this; |
| 69 |
} |
| 70 |
|
| 71 |
/** Returns a new instance of a rectangle with the same values. */ |
| 72 |
@NonNull |
| 73 |
public Rect copy() { |
| 74 |
return new Rect(x, y, w, h); |
| 75 |
} |
| 76 |
|
| 77 |
/** Returns true if the rectangle has valid bounds, i.e. w>0 and h>0. */ |
| 78 |
public boolean isValid() { |
| 79 |
return w > 0 && h > 0; |
| 80 |
} |
| 81 |
|
| 82 |
/** Returns true if the rectangle contains the x,y coordinates, borders included. */ |
| 83 |
public boolean contains(int x, int y) { |
| 84 |
return isValid() |
| 85 |
&& x >= this.x |
| 86 |
&& y >= this.y |
| 87 |
&& x < (this.x + this.w) |
| 88 |
&& y < (this.y + this.h); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Returns true if this rectangle intersects the given rectangle. |
| 93 |
* Two rectangles intersect if they overlap. |
| 94 |
* @param other the other rectangle to test |
| 95 |
* @return true if the two rectangles overlap |
| 96 |
*/ |
| 97 |
public boolean intersects(@Nullable Rect other) { |
| 98 |
if (other == null) { |
| 99 |
return false; |
| 100 |
} |
| 101 |
if (x2() <= other.x |
| 102 |
|| other.x2() <= x |
| 103 |
|| y2() <= other.y |
| 104 |
|| other.y2() <= y) { |
| 105 |
return false; |
| 106 |
} |
| 107 |
|
| 108 |
return true; |
| 109 |
} |
| 110 |
|
| 111 |
/** Returns true if the rectangle fully contains the given rectangle */ |
| 112 |
public boolean contains(@Nullable Rect rect) { |
| 113 |
return rect != null && x <= rect.x |
| 114 |
&& y <= rect.y |
| 115 |
&& x2() >= rect.x2() |
| 116 |
&& y2() >= rect.y2(); |
| 117 |
} |
| 118 |
|
| 119 |
/** Returns true if the rectangle contains the x,y coordinates, borders included. */ |
| 120 |
public boolean contains(@Nullable Point p) { |
| 121 |
return p != null && contains(p.x, p.y); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Moves this rectangle by setting it's x,y coordinates to the new values. |
| 126 |
* @return Returns self, for chaining. |
| 127 |
*/ |
| 128 |
@NonNull |
| 129 |
public Rect moveTo(int x, int y) { |
| 130 |
this.x = x; |
| 131 |
this.y = y; |
| 132 |
return this; |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Offsets this rectangle by adding the given x,y deltas to the x,y coordinates. |
| 137 |
* @return Returns self, for chaining. |
| 138 |
*/ |
| 139 |
@NonNull |
| 140 |
public Rect offsetBy(int x, int y) { |
| 141 |
this.x += x; |
| 142 |
this.y += y; |
| 143 |
return this; |
| 144 |
} |
| 145 |
|
| 146 |
@NonNull |
| 147 |
public Point getCenter() { |
| 148 |
return new Point(x + (w > 0 ? w / 2 : 0), |
| 149 |
y + (h > 0 ? h / 2 : 0)); |
| 150 |
} |
| 151 |
|
| 152 |
@NonNull |
| 153 |
public Point getTopLeft() { |
| 154 |
return new Point(x, y); |
| 155 |
} |
| 156 |
|
| 157 |
@NonNull |
| 158 |
public Point getBottomLeft() { |
| 159 |
return new Point(x, |
| 160 |
y + (h > 0 ? h : 0)); |
| 161 |
} |
| 162 |
|
| 163 |
@NonNull |
| 164 |
public Point getTopRight() { |
| 165 |
return new Point(x + (w > 0 ? w : 0), |
| 166 |
y); |
| 167 |
} |
| 168 |
|
| 169 |
@NonNull |
| 170 |
public Point getBottomRight() { |
| 171 |
return new Point(x + (w > 0 ? w : 0), |
| 172 |
y + (h > 0 ? h : 0)); |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Returns the X coordinate of the right hand side of the rectangle |
| 177 |
* |
| 178 |
* @return the X coordinate of the right hand side of the rectangle |
| 179 |
*/ |
| 180 |
public int x2() { |
| 181 |
return x + w; |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Returns the Y coordinate of the bottom of the rectangle |
| 186 |
* |
| 187 |
* @return the Y coordinate of the bottom of the rectangle |
| 188 |
*/ |
| 189 |
public int y2() { |
| 190 |
return y + h; |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Returns the X coordinate of the center of the rectangle |
| 195 |
* |
| 196 |
* @return the X coordinate of the center of the rectangle |
| 197 |
*/ |
| 198 |
public int centerX() { |
| 199 |
return x + w / 2; |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Returns the Y coordinate of the center of the rectangle |
| 204 |
* |
| 205 |
* @return the Y coordinate of the center of the rectangle |
| 206 |
*/ |
| 207 |
public int centerY() { |
| 208 |
return y + h / 2; |
| 209 |
} |
| 210 |
|
| 211 |
@Override |
| 212 |
public String toString() { |
| 213 |
return String.format("Rect [(%d,%d)-(%d,%d): %dx%d]", x, y, x + w, y + h, w, h); |
| 214 |
} |
| 215 |
|
| 216 |
@Override |
| 217 |
public boolean equals(Object obj) { |
| 218 |
if (obj instanceof Rect) { |
| 219 |
Rect rhs = (Rect) obj; |
| 220 |
// validity must be equal on both sides. |
| 221 |
if (isValid() != rhs.isValid()) { |
| 222 |
return false; |
| 223 |
} |
| 224 |
// an invalid rect is equal to any other invalid rect regardless of coordinates |
| 225 |
if (!isValid() && !rhs.isValid()) { |
| 226 |
return true; |
| 227 |
} |
| 228 |
|
| 229 |
return this.x == rhs.x && this.y == rhs.y && this.w == rhs.w && this.h == rhs.h; |
| 230 |
} |
| 231 |
|
| 232 |
return false; |
| 233 |
} |
| 234 |
|
| 235 |
@Override |
| 236 |
public int hashCode() { |
| 237 |
int hc = x; |
| 238 |
hc ^= ((y >> 8) & 0x0FFFFFF) | ((y & 0x00000FF) << 24); |
| 239 |
hc ^= ((w >> 16) & 0x000FFFF) | ((w & 0x000FFFF) << 16); |
| 240 |
hc ^= ((h >> 24) & 0x00000FF) | ((h & 0x0FFFFFF) << 8); |
| 241 |
return hc; |
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* Returns the center point in the rectangle |
| 246 |
* |
| 247 |
* @return the center point in the rectangle |
| 248 |
*/ |
| 249 |
@NonNull |
| 250 |
public Point center() { |
| 251 |
return new Point(x + w / 2, y + h / 2); |
| 252 |
} |
| 253 |
} |