通過傳入的兩個點計算他們之間的角度
阿新 • • 發佈:2019-01-26
float getAngle(Point fromPoint, Point toPoint) { double len_y = toPoint.y - fromPoint.y; double len_x = toPoint.x - fromPoint.x; if( 0 == len_x && fromPoint.y <= toPoint.y ) { return 0; } else if( 0 == len_x && fromPoint.y > toPoint.y ) { return 180; } float angle = 0.f; double tan_yx = tan_yx = std::abs(len_y)/std::abs(len_x); if(len_y > 0 && len_x < 0) { angle = atan(tan_yx)*180/M_PI - 90; } else if (len_y > 0 && len_x > 0) { angle = 90 - atan(tan_yx)*180/M_PI; } else if(len_y < 0 && len_x < 0) { angle = -atan(tan_yx)*180/M_PI - 90; } else if(len_y < 0 && len_x > 0) { angle = atan(tan_yx)*180/M_PI + 90; } return angle; }