角度计算有问题
计算角度这个没有讲 真的有好大问题
计算角度这个没有讲 真的有好大问题
2015-06-10
/**
* 获取角度
* @param a
* @param b
* @return 角度
*/
public float getDegress(Point a,Point b){
float ax=a.x;
float ay=a.y;
float bx=b.x;
float by=b.y;
float degress=0;
if(bx==ax){
if(by>ay){
degress=90;
}else if(by<ay){
degress=270;
}
}else if(by==ay){
if(bx>ax){
degress=0;
}else if(bx<ax){
degress=180;
}
}else if(bx>ax){//-90~90
if(by>ay){ //-90~0
degress=WithPoint(Math.abs(ax-bx),Math.abs(ay-by));
}else if(by<ay){//0~90
degress=-WithPoint(Math.abs(ax-bx),Math.abs(ay-by));
}
}else if(bx<ax){//90~270
if(by>ay){//180~270
degress=-WithPoint(Math.abs(ax-bx),Math.abs(ay-by))+180;
}else if(by<ay){//90~180
degress=WithPoint(Math.abs(ax-bx),Math.abs(ay-by))+180;
}
}
return degress;
}
//求角度
public float WithPoint(float x,float y){
float dushu=Math.round(Math.atan(y/x)/Math.PI*180);
return dushu;
}
/**
* 获取角度
* @param a
* @param b
* @return 角度
* */
public float getDegrees(Point a,Point b){
float aX = a.x;
float aY = a.y;
float bX = b.x;
float bY = b.y;
float tan;
float degrees = 0;
if (aX == bX){ //90度或270度
if(bY > aY){ //在y轴下边
degrees = 90;
} else if(bY < aY){
degrees = 270;
}
}else if(aY == bY){ // 0 度或180度
if (aX>bX){
degrees = 180;
}else if(aX<bX){
degrees = 0 ;
}
}else {
tan=(b.y-a.y)/(b.x-a.x);
degrees= (float) ((float)Math.atan(tan)/Math.PI*180);
if (a.y<b.y&&a.x>b.x){
degrees=180+degrees; //270°-360°
}
if (a.x>b.x&&a.y > b.y) { //180°-270°
degrees=180+degrees;
}
}
return degrees;
}
举报