michael@0: /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.gfx; michael@0: michael@0: import org.json.JSONException; michael@0: import org.json.JSONObject; michael@0: michael@0: import android.graphics.Point; michael@0: import android.graphics.PointF; michael@0: michael@0: public final class PointUtils { michael@0: public static PointF add(PointF one, PointF two) { michael@0: return new PointF(one.x + two.x, one.y + two.y); michael@0: } michael@0: michael@0: public static PointF subtract(PointF one, PointF two) { michael@0: return new PointF(one.x - two.x, one.y - two.y); michael@0: } michael@0: michael@0: public static PointF scale(PointF point, float factor) { michael@0: return new PointF(point.x * factor, point.y * factor); michael@0: } michael@0: michael@0: public static Point round(PointF point) { michael@0: return new Point(Math.round(point.x), Math.round(point.y)); michael@0: } michael@0: michael@0: /* Computes the magnitude of the given vector. */ michael@0: public static float distance(PointF point) { michael@0: return (float)Math.sqrt(point.x * point.x + point.y * point.y); michael@0: } michael@0: michael@0: /** Computes the scalar distance between two points. */ michael@0: public static float distance(PointF one, PointF two) { michael@0: return PointF.length(one.x - two.x, one.y - two.y); michael@0: } michael@0: michael@0: public static JSONObject toJSON(PointF point) throws JSONException { michael@0: // Ensure we put ints, not longs, because Gecko message handlers call getInt(). michael@0: int x = Math.round(point.x); michael@0: int y = Math.round(point.y); michael@0: JSONObject json = new JSONObject(); michael@0: json.put("x", x); michael@0: json.put("y", y); michael@0: return json; michael@0: } michael@0: } michael@0: