Given an input angle in radians, return an equivalent angle constrained to lie in the range (-pi, pi]. - Java 2D Graphics

Java examples for 2D Graphics:Angle

Description

Given an input angle in radians, return an equivalent angle constrained to lie in the range (-pi, pi].

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        double angle = 2.45678;
        System.out.println(constrainAngle(angle));
    }// ww  w .  ja  v a2s  .c o  m

    public static double PI = Math.PI;

    /**
     * Given an input angle in radians, return an equivalent angle constrained
     * to lie in the range (-pi, pi].
     */
    public static double constrainAngle(double angle) {
        while (angle > Math.PI)
            angle -= 2 * Math.PI;
        while (angle <= -Math.PI)
            angle += 2 * Math.PI;
        return angle;
    }
}

Related Tutorials