Debian Codegraph @

android/source/src/java/org/mozilla/gecko/gfx/PointUtils.java

raw
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/. */
5
6 package org.mozilla.gecko.gfx;
7
8 import android.graphics.Point;
9 import android.graphics.PointF;
10
11 import org.json.JSONException;
12 import org.json.JSONObject;
13
14 import java.lang.StrictMath;
15
16 public final class PointUtils {
17 public static PointF add(PointF one, PointF two) {
18 return new PointF(one.x + two.x, one.y + two.y);
19 }
20
21 public static PointF subtract(PointF one, PointF two) {
22 return new PointF(one.x - two.x, one.y - two.y);
23 }
24
25 public static PointF scale(PointF point, float factor) {
26 return new PointF(point.x * factor, point.y * factor);
27 }
28
29 public static Point round(PointF point) {
30 return new Point(Math.round(point.x), Math.round(point.y));
31 }
32
33 /* Computes the magnitude of the given vector. */
34 public static float distance(PointF point) {
35 return (float)StrictMath.hypot(point.x, point.y);
36 }
37
38 /** Computes the scalar distance between two points. */
39 public static float distance(PointF one, PointF two) {
40 return PointF.length(one.x - two.x, one.y - two.y);
41 }
42
43 public static JSONObject toJSON(PointF point) throws JSONException {
44 // Ensure we put ints, not longs, because Gecko message handlers call getInt().
45 int x = Math.round(point.x);
46 int y = Math.round(point.y);
47 JSONObject json = new JSONObject();
48 json.put("x", x);
49 json.put("y", y);
50 return json;
51 }
52 }
53