mobile/android/base/gfx/RectUtils.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
michael@0 2 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 package org.mozilla.gecko.gfx;
michael@0 7
michael@0 8 import org.mozilla.gecko.util.FloatUtils;
michael@0 9
michael@0 10 import org.json.JSONException;
michael@0 11 import org.json.JSONObject;
michael@0 12
michael@0 13 import android.graphics.Point;
michael@0 14 import android.graphics.PointF;
michael@0 15 import android.graphics.Rect;
michael@0 16 import android.graphics.RectF;
michael@0 17
michael@0 18 public final class RectUtils {
michael@0 19 private RectUtils() {}
michael@0 20
michael@0 21 public static Rect create(JSONObject json) {
michael@0 22 try {
michael@0 23 int x = json.getInt("x");
michael@0 24 int y = json.getInt("y");
michael@0 25 int width = json.getInt("width");
michael@0 26 int height = json.getInt("height");
michael@0 27 return new Rect(x, y, x + width, y + height);
michael@0 28 } catch (JSONException e) {
michael@0 29 throw new RuntimeException(e);
michael@0 30 }
michael@0 31 }
michael@0 32
michael@0 33 public static String toJSON(RectF rect) {
michael@0 34 StringBuilder sb = new StringBuilder(256);
michael@0 35 sb.append("{ \"left\": ").append(rect.left)
michael@0 36 .append(", \"top\": ").append(rect.top)
michael@0 37 .append(", \"right\": ").append(rect.right)
michael@0 38 .append(", \"bottom\": ").append(rect.bottom)
michael@0 39 .append('}');
michael@0 40 return sb.toString();
michael@0 41 }
michael@0 42
michael@0 43 public static RectF expand(RectF rect, float moreWidth, float moreHeight) {
michael@0 44 float halfMoreWidth = moreWidth / 2;
michael@0 45 float halfMoreHeight = moreHeight / 2;
michael@0 46 return new RectF(rect.left - halfMoreWidth,
michael@0 47 rect.top - halfMoreHeight,
michael@0 48 rect.right + halfMoreWidth,
michael@0 49 rect.bottom + halfMoreHeight);
michael@0 50 }
michael@0 51
michael@0 52 public static RectF contract(RectF rect, float lessWidth, float lessHeight) {
michael@0 53 float halfLessWidth = lessWidth / 2.0f;
michael@0 54 float halfLessHeight = lessHeight / 2.0f;
michael@0 55 return new RectF(rect.left + halfLessWidth,
michael@0 56 rect.top + halfLessHeight,
michael@0 57 rect.right - halfLessWidth,
michael@0 58 rect.bottom - halfLessHeight);
michael@0 59 }
michael@0 60
michael@0 61 public static RectF intersect(RectF one, RectF two) {
michael@0 62 float left = Math.max(one.left, two.left);
michael@0 63 float top = Math.max(one.top, two.top);
michael@0 64 float right = Math.min(one.right, two.right);
michael@0 65 float bottom = Math.min(one.bottom, two.bottom);
michael@0 66 return new RectF(left, top, Math.max(right, left), Math.max(bottom, top));
michael@0 67 }
michael@0 68
michael@0 69 public static RectF scale(RectF rect, float scale) {
michael@0 70 float x = rect.left * scale;
michael@0 71 float y = rect.top * scale;
michael@0 72 return new RectF(x, y,
michael@0 73 x + (rect.width() * scale),
michael@0 74 y + (rect.height() * scale));
michael@0 75 }
michael@0 76
michael@0 77 public static RectF scaleAndRound(RectF rect, float scale) {
michael@0 78 float left = rect.left * scale;
michael@0 79 float top = rect.top * scale;
michael@0 80 return new RectF(Math.round(left),
michael@0 81 Math.round(top),
michael@0 82 Math.round(left + (rect.width() * scale)),
michael@0 83 Math.round(top + (rect.height() * scale)));
michael@0 84 }
michael@0 85
michael@0 86 /** Returns the nearest integer rect of the given rect. */
michael@0 87 public static Rect round(RectF rect) {
michael@0 88 Rect r = new Rect();
michael@0 89 round(rect, r);
michael@0 90 return r;
michael@0 91 }
michael@0 92
michael@0 93 public static void round(RectF rect, Rect dest) {
michael@0 94 dest.set(Math.round(rect.left), Math.round(rect.top),
michael@0 95 Math.round(rect.right), Math.round(rect.bottom));
michael@0 96 }
michael@0 97
michael@0 98 public static Rect roundIn(RectF rect) {
michael@0 99 return new Rect((int)Math.ceil(rect.left), (int)Math.ceil(rect.top),
michael@0 100 (int)Math.floor(rect.right), (int)Math.floor(rect.bottom));
michael@0 101 }
michael@0 102
michael@0 103 public static IntSize getSize(Rect rect) {
michael@0 104 return new IntSize(rect.width(), rect.height());
michael@0 105 }
michael@0 106
michael@0 107 public static Point getOrigin(Rect rect) {
michael@0 108 return new Point(rect.left, rect.top);
michael@0 109 }
michael@0 110
michael@0 111 public static PointF getOrigin(RectF rect) {
michael@0 112 return new PointF(rect.left, rect.top);
michael@0 113 }
michael@0 114
michael@0 115 public static boolean fuzzyEquals(RectF a, RectF b) {
michael@0 116 if (a == null && b == null)
michael@0 117 return true;
michael@0 118 else if ((a == null && b != null) || (a != null && b == null))
michael@0 119 return false;
michael@0 120 else
michael@0 121 return FloatUtils.fuzzyEquals(a.top, b.top)
michael@0 122 && FloatUtils.fuzzyEquals(a.left, b.left)
michael@0 123 && FloatUtils.fuzzyEquals(a.right, b.right)
michael@0 124 && FloatUtils.fuzzyEquals(a.bottom, b.bottom);
michael@0 125 }
michael@0 126 }

mercurial