package com.yuqirong.screenlockview.view;

/**
 * Created by yuqirong on 2016/4/21.
 */
public class RotateDegrees {

    /**
     * 根据传入的point计算出它们之间的角度
     * @param a
     * @param b
     * @return
     */
    public static float getDegrees(Point a, Point b) {
        float degrees = 0;
        float aX = a.getX();
        float aY = a.getY();
        float bX = b.getX();
        float bY = b.getY();

        if (aX == bX) {
            if (aY < bY) {
                degrees = 90;
            } else {
                degrees = 270;
            }
        } else if (bY == aY) {
            if (aX < bX) {
                degrees = 0;
            } else {
                degrees = 180;
            }
        } else {
            if (aX > bX) {
                if (aY > bY) { // 第三象限
                    degrees = 180 + (float) (Math.atan2(aY - bY, aX - bX) * 180 / Math.PI);
                } else { // 第二象限
                    degrees = 180 - (float) (Math.atan2(bY - aY, aX - bX) * 180 / Math.PI);
                }
            } else {
                if (aY > bY) { // 第四象限
                    degrees = 360 - (float) (Math.atan2(aY - bY, bX - aX) * 180 / Math.PI);
                } else { // 第一象限
                    degrees = (float) (Math.atan2(bY - aY, bX - aX) * 180 / Math.PI);
                }
            }
        }
        return degrees;
    }

    /**
     * 根据point和(x,y)计算出它们之间的角度
     * @param a
     * @param bX
     * @param bY
     * @return
     */
    public static float getDegrees(Point a, float bX, float bY) {
        Point b = new Point(bX, bY, null);
        return getDegrees(a, b);
    }

}
