Convert polar coordinates to cartesian coordinates. - Android java.lang

Android examples for java.lang:Math

Description

Convert polar coordinates to cartesian coordinates.

Demo Code


//package com.java2s;
import android.graphics.PointF;

public class Main {
    /**/*from   www .j  a v  a2  s  .c o m*/
     * Convert polar coordinates to cartesian coordinates.
     *
     * @param angle The angle in degrees.
     * @param radius The distance from the pole.
     * @param reverse True if the ordinates goes down instead of up. False otherwise.
     * @return The cartesian coordinates.
     */
    public static PointF polarToCartesian(double angle, float radius,
            boolean reverse) {
        double angleInRadians = Math.toRadians(angle);
        float x = Math.round(radius * Math.cos(angleInRadians) * 100f) / 100f;
        float y = Math.round(radius * Math.sin(angleInRadians) * 100f) / 100f;
        if (reverse) {
            return new PointF(x, y * -1f);
        }
        // Standard formula
        return new PointF(x, y);
    }

    /**
     * Convert polar coordinates to cartesian coordinates.
     *
     * @param angle The angle
     * @param radius The distance from the pole (called the radial coordinate or radius
     * @return The cartesian coordinates.
     */
    public static PointF polarToCartesian(double angle, float radius) {
        return polarToCartesian(angle, radius, false);
    }
}

Related Tutorials