已知旋转中心和旋转角度,获得旋转之后的一组点坐标

时间:2021-08-05 10:23:51
#define PI 3.1415926535897932384626433832795
//已知旋转中心和旋转角度,获得旋转之后的一组点坐标
bool rotPoint(CPoint center,float rotAngleNow,CPoint src_corners[],CPoint dst_corners[],int num)
{
	float reverseH[6];

	float x = (float) (cos (rotAngleNow * PI / 180.));
	float y = (float) (sin (rotAngleNow * PI / 180.));

 	reverseH[0] = x;
 	reverseH[1] = y;
	reverseH[2] = (1-x)* center.x - y * center.y ;
	reverseH[3] = -y;
	reverseH[4] = x;
	reverseH[5] = y * center.x +(1-x)*center.y ;

	for( int i = 0; i < num; i++ )
	{
		double x = src_corners[i].x, y = src_corners[i].y;
		double X = (reverseH[0]*x + reverseH[1]*y + reverseH[2]);
		double Y = (reverseH[3]*x + reverseH[4]*y + reverseH[5]);
		dst_corners[i] = CPoint(cvRound(X), cvRound(Y));
	}

	return true;
}


/************************************************************************
*函数名:        rotAngle
*
*函数作用:      已知2个坐标点,求从 0------->x 逆时针需旋转多少角度到该位置
*
*					|
*					|
*					|
*					|              
*------------------------------------> x
*					| 0            
*					|
*					|
*					|
*                   v
*					y
*					
*函数参数:
*CPoint pointO  - 起点
*CPoint pointA  - 终点
*
*函数返回值:
*double         向量OA,从 0------->x 逆时针需旋转多少角度到该位置
**************************************************************************/
double rotAngle(CPoint pointO,CPoint pointA)
{
	double angle = 0;
	CPoint point;
	double temp;

	point = pointA - pointO;// pointAdd(pointA,pointMultiply(pointO,-1));

	if ((0==point.x) && (0==point.y))
	{
		return 0;
	}

	if (0==point.x)
	{
		angle = 90;
		return angle;
	}

	if (0==point.y)
	{
		angle = 0;
		return angle;
	}

	temp = fabsf(float(point.y)/float(point.x));
	temp = atan(temp);
	temp = temp*180/PI ;

	if ((0<point.x)&&(0<point.y))
	{
		angle = 360 - temp;
		return angle;
	}

	if ((0>point.x)&&(0<point.y))
	{
		angle = 360 - (180 - temp);
		return angle;
	}

	if ((0<point.x)&&(0>point.y))
	{
		angle = temp;
		return angle;
	}

	if ((0>point.x)&&(0>point.y))
	{
		angle = 180 - temp;
		return angle;
	}

	printf("sceneDrawing :: getAngle error!");
	return -1;
}