Android Open Source - getback_gps Coordinate






From Project

Back to project page getback_gps.

License

The source code is released under:

GNU General Public License

If you think the Android project getback_gps listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/**
 * 2D coordinate class, for converting between polar and Cartesian.
 *//  w w w  .  j a  v  a2 s .c om
 * Copyright (C) 2012-2015 Dieter Adriaenssens
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @package com.github.ruleant.getback_gps.lib
 * @author  Dieter Adriaenssens <ruleant@users.sourceforge.net>
 */
package com.github.ruleant.getback_gps.lib;

/**
 * 2D coordinate class, for converting between polar and Cartesian.
 *
 * @author  Dieter Adriaenssens <ruleant@users.sourceforge.net>
 */
public class Coordinate {
    /**
     * Polar angle coordinate in degrees.
     */
    private double mAngle;

    /**
     * Polar radius coordinate.
     */
    private double mRadius;

    /**
     * X coordinate.
     */
    public static final int X = 0;

    /**
     * Y coordinate.
     */
    public static final int Y = 1;

    /**
     * Polar radius coordinate.
     */
    public static final int RADIUS = 0;

    /**
     * Polar angle coordinate.
     */
    public static final int ANGLE = 1;

    /**
     * Constructor.
     *
     * @param radius Radius coordinate
     * @param angle Angle coordinate in degrees
     */
    public Coordinate(final double radius, final double angle) {
        setPolarCoordinate(radius, angle);
    }

    /**
     * Constructor.
     *
     * @param x X coordinate
     * @param y Y coordinate
     */
    public Coordinate(final long x, final long y) {
        setCartesianCoordinate(x, y);
    }

    /**
     * Constructor.
     *
     * @param coordinate New Coordinate
     */
    public Coordinate(final Coordinate coordinate) {
        setCoordinate(coordinate);
    }

    /**
     * Set coordinate in polar format.
     *
     * @param radius Radius coordinate
     * @param angle Angle coordinate in degrees
     */
    public final void setPolarCoordinate(final double radius,
                                         final double angle) {
        if (radius >= 0) {
            // if radius is a positive number
            mAngle = FormatUtils.normalizeAngle(angle);
            mRadius = radius;
        } else {
            // if radius is negative number
            // invert angle
            mAngle = FormatUtils.inverseAngle(angle);
            // use positive value for radius
            mRadius = Math.abs(radius);
        }
    }

    /**
     * Set coordinate in Cartesian format.
     *
     * @param x X coordinate
     * @param y Y coordinate
     */
    public final void setCartesianCoordinate(final long x, final long y) {
        // calculate radius
        mRadius = Math.sqrt(x * x + y * y);

        // calculate angle, atan2() returns the polar angle
        mAngle = FormatUtils.normalizeAngle(Math.toDegrees(Math.atan2(y, x)));
    }

    /**
     * Set coordinate with a Coordinate instance.
     *
     * @param coordinate New Coordinate
     */
    public final void setCoordinate(final Coordinate coordinate) {
        if (coordinate == null) {
            throw new IllegalArgumentException(
                    "Parameter coordinate should not be null");
        }

        setPolarCoordinate(coordinate.getPolarRadius(),
                coordinate.getPolarAngle());
    }

    /**
     * Get coordinate in polar format.
     *
     * @return array with radius and angle in degrees
     */
    public final double[] getPolarCoordinate() {
        double[] coordinate = new double[2];

        coordinate[RADIUS] = getPolarRadius();
        coordinate[ANGLE] = getPolarAngle();

        return coordinate;
    }

    /**
     * Get polar angle coordinate.
     *
     * @return angle coordinate in degrees (0-360)
     */
    public final double getPolarAngle() {
        if (mRadius > 0.0) {
            return mAngle;
        } else {
            return 0.0;
        }
    }

    /**
     * Get polar radius coordinate.
     *
     * @return radius coordinate
     */
    public final double getPolarRadius() {
        return mRadius;
    }

    /**
     * Get coordinate in Cartesian format.
     *
     * @return array with X and Y coordinate
     */
    public final long[] getCartesianCoordinate() {
        long[] coordinate = new long[2];

        coordinate[X] = getCartesianX();
        coordinate[Y] = getCartesianY();

        return coordinate;
    }

    /**
     * Get Cartesian X coordinate.
     *
     * @return X coordinate
     */
    public final long getCartesianX() {
        return Math.round(Math.cos(Math.toRadians(mAngle)) * mRadius);
    }

    /**
     * Get Cartesian Y coordinate.
     *
     * @return Y coordinate
     */
    public final long getCartesianY() {
        return Math.round(Math.sin(Math.toRadians(mAngle)) * mRadius);
    }
}




Java Source Code List

com.github.ruleant.getback_gps.AboutActivity.java
com.github.ruleant.getback_gps.AbstractGetBackGpsActivity.java
com.github.ruleant.getback_gps.DetailsActivity.java
com.github.ruleant.getback_gps.LocationService.java
com.github.ruleant.getback_gps.MainActivity.java
com.github.ruleant.getback_gps.NavigationView.java
com.github.ruleant.getback_gps.SettingsActivity.java
com.github.ruleant.getback_gps.lib.AbstractGeoCoordinate.java
com.github.ruleant.getback_gps.lib.AriadneLocation.java
com.github.ruleant.getback_gps.lib.CardinalDirection.java
com.github.ruleant.getback_gps.lib.CircularAverage.java
com.github.ruleant.getback_gps.lib.CoordinateConverterInterface.java
com.github.ruleant.getback_gps.lib.CoordinateRotation.java
com.github.ruleant.getback_gps.lib.Coordinate.java
com.github.ruleant.getback_gps.lib.Coordinates.java
com.github.ruleant.getback_gps.lib.DebugLevel.java
com.github.ruleant.getback_gps.lib.FormatUtils.java
com.github.ruleant.getback_gps.lib.Latitude.java
com.github.ruleant.getback_gps.lib.Longitude.java
com.github.ruleant.getback_gps.lib.LowPassFilter.java
com.github.ruleant.getback_gps.lib.Navigator.java
com.github.ruleant.getback_gps.lib.SensorOrientation.java
com.github.ruleant.getback_gps.lib.StoredDestination.java
com.github.ruleant.getback_gps.lib.StoredLocation.java
com.github.ruleant.getback_gps.lib.Tools.java
com.github.ruleant.getback_gps.lib.package-info.java
com.github.ruleant.getback_gps.package-info.java
com.github.ruleant.unitconversion.UnitConversionInterface.java
com.github.ruleant.unitconversion.package-info.java