缩放
qreal GraphicsObject::CalcuateScale(QPointF center, QPointF p1, QPointF p2)
{
//@功能:求缩放比例
//@参数:center图元中心点,p1按下的点,p2移动的点
qreal dx = p1.x() - center.x();
qreal dy = p1.y() - center.y();
//鼠标按下时距离中心点的距离
qreal d1 = sqrt(pow(dx, 2) + pow(dy, 2));
dx = p2.x() - center.x();
dy = p2.y() - center.y();
//鼠标移动时距离中心点的距离
qreal d2 = sqrt(pow(dx, 2) + pow(dy, 2));
//两个距离的比例可求出比例
return d2/d1*1.;
}
旋转角度
qreal GraphicsObject::CalcuateRotate(QPointF center, QPointF p1, QPointF p2)
{
//@功能:求旋转角度
//@参数:center图元中心点, p1鼠标按下点,p2鼠标移动点
//@有关数学公式:
//@1: 2Π弧度=360°==> 1弧度=180/Π
//@2: 三点形成的圆形总弧度为2Π
qreal x3 = center.x(), y3 = center.y();
qreal x1 = p1.x(), y1 = p1.y();
qreal x2 = p2.x(), y2 = p2.y();
qreal theta = atan2(x1 - x3, y1 - y3) - atan2(x2 - x3, y2 - y3);
if (theta > M_PI)
theta -= 2 * M_PI;
if (theta < -M_PI