1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/gfx/PointUtils.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,51 @@ 1.4 +/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- 1.5 + * This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +package org.mozilla.gecko.gfx; 1.10 + 1.11 +import org.json.JSONException; 1.12 +import org.json.JSONObject; 1.13 + 1.14 +import android.graphics.Point; 1.15 +import android.graphics.PointF; 1.16 + 1.17 +public final class PointUtils { 1.18 + public static PointF add(PointF one, PointF two) { 1.19 + return new PointF(one.x + two.x, one.y + two.y); 1.20 + } 1.21 + 1.22 + public static PointF subtract(PointF one, PointF two) { 1.23 + return new PointF(one.x - two.x, one.y - two.y); 1.24 + } 1.25 + 1.26 + public static PointF scale(PointF point, float factor) { 1.27 + return new PointF(point.x * factor, point.y * factor); 1.28 + } 1.29 + 1.30 + public static Point round(PointF point) { 1.31 + return new Point(Math.round(point.x), Math.round(point.y)); 1.32 + } 1.33 + 1.34 + /* Computes the magnitude of the given vector. */ 1.35 + public static float distance(PointF point) { 1.36 + return (float)Math.sqrt(point.x * point.x + point.y * point.y); 1.37 + } 1.38 + 1.39 + /** Computes the scalar distance between two points. */ 1.40 + public static float distance(PointF one, PointF two) { 1.41 + return PointF.length(one.x - two.x, one.y - two.y); 1.42 + } 1.43 + 1.44 + public static JSONObject toJSON(PointF point) throws JSONException { 1.45 + // Ensure we put ints, not longs, because Gecko message handlers call getInt(). 1.46 + int x = Math.round(point.x); 1.47 + int y = Math.round(point.y); 1.48 + JSONObject json = new JSONObject(); 1.49 + json.put("x", x); 1.50 + json.put("y", y); 1.51 + return json; 1.52 + } 1.53 +} 1.54 +