public float getDegrees(Point a, Point b) {
float ax = a.x;
float ay = a.y;
float bx = b.x;
float by = b.y;
float degrees = 0;
if (bx == ax)
{
if (by > ay)
{
degrees = 90;
} else if (by < ay)
{
degrees = 270;
}
}
else if (by == ay)
{
if (bx > ax)
{
degrees = 0;
} else if (bx < ax)
{
degrees = 180;
}
}
else {
if (bx > ax)
{
if (by > ay)
{
degrees = 0;
degrees = degrees
+ switchDegrees(Math.abs(by - ay),
Math.abs(bx - ax));
} else if (by < ay)
{
degrees = 360;
degrees = degrees
- switchDegrees(Math.abs(by - ay),
Math.abs(bx - ax));
}
} else if (bx < ax)
{
if (by > ay)
{
degrees = 90;
degrees = degrees
+ switchDegrees(Math.abs(bx - ax),
Math.abs(by - ay));
} else if (by < ay)
{
degrees = 270;
degrees = degrees
- switchDegrees(Math.abs(bx - ax),
Math.abs(by - ay));
}
}
}
return degrees;
}
/**
* 1=30度 2=45度 4=60度
*
* @param tan
* @return
*/
private float switchDegrees(float x, float y) {
return (float) MathUtil.pointTotoDegrees(x, y);
}
/**
* 两点间的距离
* @param x1
* @param y1
* @param x2
* @param y2
* @return
*/
public static double distance(double x1, double y1, double x2, double y2) {
return Math.sqrt(Math.abs(x1 - x2) * Math.abs(x1 - x2)
+ Math.abs(y1 - y2) * Math.abs(y1 - y2));
}
/**
* 计算点a(x,y)的角度
*
* @param x
* @param y
* @return
*/
public static double pointTotoDegrees(double x, double y) {
return Math.toDegrees(Math.atan2(x, y));
}
/**
* 鼠标的坐标是否和九宫格中的点重合
* @param pointX 点的x坐标
* @param pointY 点的y坐标
* @param r 九宫格中的点的半径
* @param movingX 鼠标的x坐标
* @param movingY 鼠标的y坐标
*/
public static boolean with(float pointX,float pointY,float r,float movingX,float movingY) {
return Math.sqrt((pointX - movingX) * (pointX - movingX) + (pointY - movingY) * (pointY - movingY)) < r;
}