Finds the angle obtained by moving a distance of delta_x from x in a clockwise direction if delta_x is positive and anticlockwise direction is delta_x is negative. - Java java.lang

Java examples for java.lang:Math Geometry Distance

Description

Finds the angle obtained by moving a distance of delta_x from x in a clockwise direction if delta_x is positive and anticlockwise direction is delta_x is negative.

Demo Code

/*/*  w  ww  . j a va  2s . c  o  m*/
 * Copyright (C) 2011 apurv
 *
 * 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
 * (at your option) 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.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        double x = 2.45678;
        double delta_x = 2.45678;
        System.out.println(add(x, delta_x));
    }

    /**
     * Finds the angle obtained by moving a distance of delta_x from x in a clockwise direction
     * if delta_x is positive and anticlockwise direction is delta_x is negative.
     * @param x
     * @param delta_x
     * @return the transformed angle obtained after clockwise addition.
     */
    private static Double add(Double x, Double delta_x) {
        Double y = null;
        if (delta_x > 0) {
            y = addClockwise(x, delta_x);

        } else if (delta_x < 0) {
            y = addAntiClockwise(x, Math.abs(delta_x));

        } else {
            y = x;
        }
        return y;
    }

    /**
     * Finds the angle obtained by moving a distance of delta_x from x in a clockwise direction.
     * @param x
     * @param delta_x magnitude of the clockwise shift.
     * @return the transformed angle obtained after clockwise addition.
     */
    private static Double addClockwise(Double x, Double delta_x) {
        Double y = null;
        delta_x = delta_x % 360;

        if (x + delta_x > 180.0) {
            y = (x + delta_x) - 360;

        } else {
            y = x + delta_x;

        }
        return y;
    }

    /**
     * Finds the angle obtained by moving a distance of delta_x from x in a anti-clockwise direction.
     * @param x
     * @param delta_x magnitude of the anti-clockwise shift.
     * @return the transformed angle obtained after anti-clockwise addition.
     */
    private static Double addAntiClockwise(Double x, Double delta_x) {
        Double y = null;
        delta_x = delta_x % 360;

        if (x - delta_x < -180.0) {
            y = (x - delta_x) + 360;

        } else {
            y = x - delta_x;

        }
        return y;
    }
}

Related Tutorials