1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 package org
.mozilla
.gecko
.gfx
;
8 import android
.graphics
.Point
;
9 import android
.graphics
.PointF
;
10 import android
.graphics
.Rect
;
11 import android
.graphics
.RectF
;
13 import org
.mozilla
.gecko
.util
.FloatUtils
;
15 public final class RectUtils
{
16 private RectUtils() {}
18 public static RectF
expand(RectF rect
, float moreWidth
, float moreHeight
) {
19 float halfMoreWidth
= moreWidth
/ 2;
20 float halfMoreHeight
= moreHeight
/ 2;
21 return new RectF(rect
.left
- halfMoreWidth
,
22 rect
.top
- halfMoreHeight
,
23 rect
.right
+ halfMoreWidth
,
24 rect
.bottom
+ halfMoreHeight
);
27 public static RectF
contract(RectF rect
, float lessWidth
, float lessHeight
) {
28 float halfLessWidth
= lessWidth
/ 2.0f
;
29 float halfLessHeight
= lessHeight
/ 2.0f
;
30 return new RectF(rect
.left
+ halfLessWidth
,
31 rect
.top
+ halfLessHeight
,
32 rect
.right
- halfLessWidth
,
33 rect
.bottom
- halfLessHeight
);
36 public static RectF
intersect(RectF one
, RectF two
) {
37 float left
= Math
.max(one
.left
, two
.left
);
38 float top
= Math
.max(one
.top
, two
.top
);
39 float right
= Math
.min(one
.right
, two
.right
);
40 float bottom
= Math
.min(one
.bottom
, two
.bottom
);
41 return new RectF(left
, top
, Math
.max(right
, left
), Math
.max(bottom
, top
));
44 public static RectF
scale(RectF rect
, float scale
) {
45 float x
= rect
.left
* scale
;
46 float y
= rect
.top
* scale
;
47 return new RectF(x
, y
,
48 x
+ (rect
.width() * scale
),
49 y
+ (rect
.height() * scale
));
52 public static RectF
inverseScale(RectF rect
, float scale
) {
53 float x
= rect
.left
/ scale
;
54 float y
= rect
.top
/ scale
;
55 return new RectF(x
, y
,
56 x
+ (rect
.width() / scale
),
57 y
+ (rect
.height() / scale
));
60 /** Returns the nearest integer rect of the given rect. */
61 public static Rect
round(RectF rect
) {
67 public static void round(RectF rect
, Rect dest
) {
68 dest
.set(Math
.round(rect
.left
), Math
.round(rect
.top
),
69 Math
.round(rect
.right
), Math
.round(rect
.bottom
));
72 public static Rect
roundIn(RectF rect
) {
73 return new Rect((int)Math
.ceil(rect
.left
), (int)Math
.ceil(rect
.top
),
74 (int)Math
.floor(rect
.right
), (int)Math
.floor(rect
.bottom
));
77 public static IntSize
getSize(Rect rect
) {
78 return new IntSize(rect
.width(), rect
.height());
81 public static Point
getOrigin(Rect rect
) {
82 return new Point(rect
.left
, rect
.top
);
85 public static PointF
getOrigin(RectF rect
) {
86 return new PointF(rect
.left
, rect
.top
);
89 public static boolean fuzzyEquals(RectF a
, RectF b
) {
90 if (a
== null && b
== null)
93 return a
!= null && b
!= null
94 && FloatUtils
.fuzzyEquals(a
.top
, b
.top
)
95 && FloatUtils
.fuzzyEquals(a
.left
, b
.left
)
96 && FloatUtils
.fuzzyEquals(a
.right
, b
.right
)
97 && FloatUtils
.fuzzyEquals(a
.bottom
, b
.bottom
);
101 * Assign rectangle values from source to target.
103 public static void assign(final RectF target
, final RectF source
)
105 target
.left
= source
.left
;
106 target
.top
= source
.top
;
107 target
.right
= source
.right
;
108 target
.bottom
= source
.bottom
;