cocos2d-x中求两坐标的夹角

以一个点为起始点,向周围其他点发出射线,这时候就要对射线图片进行旋转,旋转角度通过起始两点间夹角来计算。封装的函数如下:
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;
}

你可能感兴趣的:(cocos2d-x,cocos2d-x,夹角)