package net.micode.notes.tool;

public class Point {
    public static int BITMAP_NORMAL = 0; // 正常
    public static int BITMAP_ERROR = 1;  // 错误
    public static int BITMAP_PRESS = 2;  // 按下

    //九宫格中的点的下标（即每个点代表一个值）
    private String index;
    //点的状态
    private int state;
    //点的坐标
    private float x;
    private float y;

    public Point() {
        super();
    }

    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public String getIndex() {
        return index;
    }

    public int getState() {
        return state;
    }

    public float getX() {
        return x;
    }

    public float getY() {
        return y;
    }

    public void setIndex(String index) {
        this.index = index;
    }

    public void setState(int state) {
        this.state = state;
    }

    public void setX(float x) {
        this.x = x;
    }

    public void setY(float y) {
        this.y = y;
    }

    /**
     * 判断屏幕上的九宫格中的点能否可以进行连线
     *
     * @param a
     * @param moveX
     * @param moveY
     * @param radius 点bitmap的半径
     * @return 布尔型
     */
    public boolean isWith(Point a, float moveX, float moveY, float radius) {
        float result = (float) Math.sqrt((a.getX() - moveX)
                * (a.getX() - moveX) + (a.getY() - moveY)
                * (a.getY() - moveY));
        if (result < 5 * radius / 4) {
            return true;
        }
        return false;
    }

    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 (by > ay) {
                degrees = 90;
            } else {
                degrees = 270;
            }
        } else if (by == ay) {
            if (ax > bx) {
                degrees = 180;
            } else {
                degrees = 0;
            }
        } 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;
    }
}

