Normalize an angle so that it belongs to the [0, 360[ range. - Java 2D Graphics

Java examples for 2D Graphics:Angle

Description

Normalize an angle so that it belongs to the [0, 360[ range.

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        float angle = 2.45678f;
        System.out.println(normalize(angle));
    }//from   w ww.  ja  v  a 2  s. c o m

    /**
     * Normalize an angle so that it belongs to the [0, 360[ range.
     * @param angle the angle in degrees
     * @return the same angle in the [0, 360[ range
     */
    public static float normalize(final float angle) {
        return (angle >= 0 ? angle : (360 - ((-angle) % 360))) % 360;
    }
}

Related Tutorials