Indicates whether r1 is nearest to r2 in the clockwise (>0) or anti-clockwise (<0) direction. - Java 2D Graphics

Java examples for 2D Graphics:Rectangle

Description

Indicates whether r1 is nearest to r2 in the clockwise (>0) or anti-clockwise (<0) direction.

Demo Code


//package com.java2s;

public class Main {
    public static final double FULL_CIRCLE = Math.PI * 2;

    /**//  www  . j  a v  a2  s .  c om
     * Indicates whether r1 is nearest to r2 in the clockwise (>0) or
     * anti-clockwise (<0) direction.  If the two rotations are the same
     * then 0 is returned.
     * 
     * @param r1 The first rotation
     * @param r2 The second rotation
     * @return int indicating the direction, clockwise or anti-clockwise, to travel for r2 to meet r1
     */
    public static int rotationDirection(double rotation1, double rotation2) {
        double r1 = rotation1;
        double r2 = rotation2;

        if (r1 == r2) {
            return 0;
        }
        if (r1 > Math.PI) {
            r1 = normaliseRadians(r1 - Math.PI);
            r2 = normaliseRadians(r2 - Math.PI);
        }
        if ((r2 - r1) > 0 && (r2 - r1) < Math.PI) {
            return 1;
        } else {
            return -1;
        }
    }

    public static double normaliseRadians(double radians) {
        while (radians > FULL_CIRCLE) {
            radians -= FULL_CIRCLE;
        }
        while (radians < -FULL_CIRCLE) {
            radians += FULL_CIRCLE;
        }
        if (radians < 0) {
            radians = Math.PI * 2 + radians;
        }

        return radians;
    }
}

Related Tutorials