std::complex 复数可以很方便的做旋转
以下为实现:
//待旋转的点坐标 (fRotationX,fRotationY)
//中心点坐标 (fCenterX, fCenterY)
//旋转角度
void Rotation(float &fRotationX, float &fRotationY,float fCenterX, float fCenterY,double fAngle)
{ // 注意标准坐标和屏幕坐标的区别:
// 标准坐标:y 轴正向向上;x 轴正向向右;角度逆时针增大
// 屏幕坐标:y 轴及角度方向与标准坐标相反!
// 转换为标准坐标
std::complex<double> c(fRotationX,-fRotationY);
std::complex<double> cCenter(fCenterX,-fCenterY);
// 先将坐标原点移动到旋转中心
c -= cCenter;
// 旋转(注意:标准坐标方向和屏幕相反)
//std::polar 提供一对表示复数幅度和角度的极坐标,并返回对应的复数
//(fAngle * a)/180.0f 角度转弧度
//std::arg(c) 返回复数的相角
// std::abs(c) 返回复数的模
std::complex<double> cNew = std::polar(std::abs(c),std::arg(c) - (fAngle * a)/180.0f);
恢复坐标原点
fRotationX = cNew.real() + fCenterX;
fRotationY = -cNew.imag() + fCenterY; // 注意:恢复纵坐标符号
}
获取更多帮主请关注小程序