Indicates whether an angle is within a range or not. - Android java.lang

Android examples for java.lang:Math Trigonometric Function

Description

Indicates whether an angle is within a range or not.

Demo Code


//package com.java2s;

public class Main {
    /**//  www  . java2  s.  co  m
     * Indicates whether an angle is within a range or not.
     *
     * @param angle The angle to check in degrees.
     * Can be in the range [-180, 180] or [0, 360].
     * @param bisectorAngle The bisector of the range.
     * @param range the range in degrees.
     * @return True if the angle is in the specified range. False otherwise.
     */
    public static boolean angleInRange(double angle, double bisectorAngle,
            double range) {
        if (angle < 0) {
            angle += 360.0;
        }

        final double lowerBound = bisectorAngle - range / 2.0;
        final double upperBound = bisectorAngle + range / 2.0;

        if (lowerBound < 0) {
            return angle > lowerBound + 360 || angle < upperBound;
        }

        return angle > lowerBound && angle < upperBound;
    }
}

Related Tutorials