您的位置:首页 > 移动开发 > Cocos引擎

cocos2d-x中求两坐标的夹角

2015-11-25 10:38 197 查看
以一个点为起始点,向周围其他点发出射线,这时候就要对射线图片进行旋转,旋转角度通过起始两点间夹角来计算。封装的函数如下:
float YourClassName::getAngle( Vec2& from, Vec2& to )
{
float len_y = to.y - from.y;
float len_x = to.x - from.x;
if( 0 == len_x && from.y < to.y )
{
return -90;
}else if( 0 == len_x && from.y > to.y )
{
return 90;
}else if (0 == len_y && from.x > to.x)
{
return 180;
}else if (0 == len_y && from.x < to.x)
{
return 0;
}
float tan_yx = std::abs(len_y)/std::abs(len_x);
float angle = 0;
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;
} 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 = 90 - atan(tan_yx)*180/M_PI;
}
return angle;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: