实例: 以点(100,100) 围绕点(200,150)旋转45°后的坐标, 采用QTransform 类方法实现移动变换.
Test1
采用一个QTransform 对象,通过连续的变换后,发现最后的结果与预先的不一致.
原因:
当trans1.translate(-200., -150.); 后,坐标系的原点变成了-200,-150.
之后trans1.rotate(45.); 将以(-200,-150)为原点,旋转坐标系45°.
trans1.translate(200., 150.); ,将在旋转后的坐标系下面, X轴方向偏移200,Y轴偏置150.
Test2
正确的方式:
采用多个QTransform 对象,保证每个变换对象的初始原点坐标始终是(0,0).
最后通过了将每个变换* 得到一个总的变换矩阵.
也可以单独一步一步的变换后,将上一步的结果作为下一步的输入点.
#include <QtCore/QCoreApplication>
#include <QTransform>
#include <QRectF>
#include <QDebug>
#include <QRegion>void Test1()
{qInfo() << "Test1";QPointF point(100., 100.);QTransform trans1;qInfo() << "trans1:" << trans1;trans1.translate(-200., -150.);qInfo() << "trans1:" << trans1;QPointF point1 = trans1.map(point);qInfo() << point1;trans1.rotate(45.);qInfo() << "trans1:" << trans1;QPointF point2 = trans1.map(point);qInfo() << point2;trans1.translate(200., 150.);qInfo() << "trans1:" << trans1;QPointF point3 = trans1.map(point);qInfo() << "Test2" << point3;
}void Test2()
{qInfo() << "Test2";QPointF point(100., 100.);QTransform trans1;qInfo() << "trans1:" << trans1;trans1.translate(-200., -150.);qInfo() << "trans1:" << trans1;QPointF point1 = trans1.map(point);qInfo() << point1;QTransform trans2;qInfo() << "trans2:" << trans2;trans2.rotate(45.);qInfo() << "trans2:" << trans2;QPointF point2 = trans2.map(point1);qInfo() << point2;QTransform trans3;qInfo() << "trans3:" << trans3;trans3.translate(200., 150.);qInfo() << "trans3:" << trans3;QPointF point3 = trans3.map(point2);qInfo() << "Test2" << point3;//方式二// Combine the transformations using chainingQTransform finalTransform = trans1 * trans2 * trans3;qInfo() << "finalTransform:" << finalTransform;QPointF point4 = finalTransform.map(point);qInfo() << "Test2" << point4;}int main(int argc, char *argv[])
{QCoreApplication a(argc, argv);Test1();Test2();return a.exec();
}
结果
Test1
trans1: QTransform(type=TxNone, 11=1 12=0 13=0 21=0 22=1 23=0 31=0 32=0 33=1)
trans1: QTransform(type=TxTranslate, 11=1 12=0 13=0 21=0 22=1 23=0 31=-200 32=-150 33=1)
QPointF(-100,-50)
trans1: QTransform(type=TxRotate, 11=0.707107 12=0.707107 13=0 21=-0.707107 22=0.707107 23=0 31=-200 32=-150 33=1)
QPointF(-200,-8.57864)
trans1: QTransform(type=TxRotate, 11=0.707107 12=0.707107 13=0 21=-0.707107 22=0.707107 23=0 31=-164.645 32=97.4874 33=1)
Test2 QPointF(-164.645,238.909)
Test2
trans1: QTransform(type=TxNone, 11=1 12=0 13=0 21=0 22=1 23=0 31=0 32=0 33=1)
trans1: QTransform(type=TxTranslate, 11=1 12=0 13=0 21=0 22=1 23=0 31=-200 32=-150 33=1)
QPointF(-100,-50)
trans2: QTransform(type=TxNone, 11=1 12=0 13=0 21=0 22=1 23=0 31=0 32=0 33=1)
trans2: QTransform(type=TxRotate, 11=0.707107 12=0.707107 13=0 21=-0.707107 22=0.707107 23=0 31=0 32=0 33=1)
QPointF(-35.3553,-106.066)
trans3: QTransform(type=TxNone, 11=1 12=0 13=0 21=0 22=1 23=0 31=0 32=0 33=1)
trans3: QTransform(type=TxTranslate, 11=1 12=0 13=0 21=0 22=1 23=0 31=200 32=150 33=1)
Test2 QPointF(164.645,43.934)
finalTransform: QTransform(type=TxRotate, 11=0.707107 12=0.707107 13=0 21=-0.707107 22=0.707107 23=0 31=164.645 32=-97.4874 33=1)
Test2 QPointF(164.645,43.934)