어제 오늘 내일

[Java Math] asin(), acos(), atan(), atan2() – 역삼각함수 계산 본문

IT/Java

[Java Math] asin(), acos(), atan(), atan2() – 역삼각함수 계산

hi.anna 2025. 12. 13. 07:11

 

자바 Math 클래스는 삼각함수의 역함수도 제공합니다.

  • asin(x) → 아크사인 (사인 역함수)
  • acos(x) → 아크코사인 (코사인 역함수)
  • atan(x) → 아크탄젠트 (탄젠트 역함수)
  • atan2(y, x) → 좌표 (x, y)의 각도 반환 (사분면 고려)

결과는 모두 라디안(radian) 단위입니다.
 
 

1. asin() – 아크사인

public class MathAsin {
    public static void main(String[] args) {
        double val = 0.5;
        double rad = Math.asin(val);

        System.out.println("asin(0.5) = " + rad);                 // 0.5235987755982989
        System.out.println("각도 = " + Math.toDegrees(rad) + "°"); // 30.0°
    }
}
  • 입력 범위: -1 ~ 1
  • 반환 범위: -π/2 ~ π/2

 

2. acos() – 아크코사인

public class MathAcos {
    public static void main(String[] args) {
        double val = 0.5;
        double rad = Math.acos(val);

        System.out.println("acos(0.5) = " + rad);                 // 1.0471975511965979
        System.out.println("각도 = " + Math.toDegrees(rad) + "°"); // 60.0°
    }
}
  • 입력 범위: -1 ~ 1
  • 반환 범위: 0 ~ π

 

3. atan() – 아크탄젠트

public class MathAtan {
    public static void main(String[] args) {
        double val = 1.0;
        double rad = Math.atan(val);

        System.out.println("atan(1.0) = " + rad);                 // 0.7853981633974483
        System.out.println("각도 = " + Math.toDegrees(rad) + "°"); // 45.0°
    }
}
  • 입력 범위: 모든 실수
  • 반환 범위: -π/2 ~ π/2

 

4. atan2(y, x) – 사분면 고려 아크탄젠트

public class MathAtan2 {
    public static void main(String[] args) {
        double x = -1.0;
        double y = 1.0;

        double rad = Math.atan2(y, x);

        System.out.println("atan2(1, -1) = " + rad);               // 2.356194490192345
        System.out.println("각도 = " + Math.toDegrees(rad) + "°"); // 135.0°
    }
}

atan2(y, x)는 사분면을 고려하여 정확한 각도를 반환합니다.

 

활용 예제: 좌표의 방향 각도 구하기

public class MathAtan2Direction {
    public static void main(String[] args) {
        int x1 = 0, y1 = 0;
        int x2 = 3, y2 = 4;

        double rad = Math.atan2(y2 - y1, x2 - x1);
        double deg = Math.toDegrees(rad);

        System.out.println("방향 각도 = " + deg + "°"); // 53.13010235415599°
    }
}

두 점의 상대적인 방향을 구할 때 atan2()가 자주 사용됩니다.

 

결론

  • asin() → 아크사인, [-π/2, π/2]
  • acos() → 아크코사인, [0, π]
  • atan() → 아크탄젠트, [-π/2, π/2]
  • atan2(y, x) → 사분면을 고려한 아크탄젠트
  • 그래픽스, 삼각 측량, 좌표 기하학, 물리 시뮬레이션 등에 활용

 

반응형
Comments