2014년 6월 30일 월요일

Bearing Calculation Tip


2차원 평면그래프는 
X축이 우측으로 갈수록 + 좌측으로 갈수록 -이고
Y축은 위로 갈수록 +, 아래로 갈수록 -이다

하지만 픽셀 좌표에서는
left는 2차원 평면 그래프와 같지만,
top은 반대로 위로 갈수록 마이너스 아래로 갈수록 +이다.

atan을 이용한 bearing 구하기

getBearing

private double getBearing(float curX, float curY, float tarX, float tarY) {
        
        double degree = 0;
        double radians = 0;
        
        double width = tarX-curX; 
        double height = -(tarY-curY);
        
        if(height == 0) {
            height = 1;
        }
        
        try {
            radians = Math.atan(width / height);
            degree = Math.toDegrees(radians);
            degree = (degree + 360) % 360;
            
        } catch(Exception e) {
            ;
        }
        
        return (double) degree;
    }

댓글 없음:

댓글 쓰기