Increments current Angle by angle, truncating at desired Angle. - Java 2D Graphics

Java examples for 2D Graphics:Angle

Description

Increments current Angle by angle, truncating at desired Angle.

Demo Code


//package com.java2s;

public class Main {


    /**/*  w  w w  .j av a 2  s. c  om*/
     * Increments currentAngle by angle, truncating at desiredAngle.
     * 
     * @param angle
     *            The increment.
     * @param currentAngle
     *            The angle to be incremented by angle.
     * @param desiredAngle
     *            The limit angle, used to truncate the currentAngle increment.
     * @return currentAngle incremented by angle and limited by desiredAngle.
     */
    public static double calculateTruncatedNextAngle(double angle,
            double currentAngle, double desiredAngle) {
        double theta = currentAngle;

        double diffAngle = minimumDifference(currentAngle, desiredAngle);

        if (diffAngle > 0)
            // turn left
            theta = currentAngle + angle;
        else
            // turn right
            theta = currentAngle - angle;

        // truncate to desiredAngle
        if (desiredAngle > currentAngle && desiredAngle < theta)
            theta = desiredAngle;
        else if (desiredAngle < currentAngle && desiredAngle > theta)
            theta = desiredAngle;

        return theta;
    }

    /**
     * Returns the difference between two angles in degrees between -180 and 180
     */
    public static double minimumDifference(double a, double b) {
        double diffAngle = b - a;
        if (diffAngle > 180)
            diffAngle -= 360;
        else if (diffAngle < -180)
            diffAngle += 360;
        return diffAngle;
    }
}

Related Tutorials