mobile/android/base/gfx/PointUtils.java

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial