缩放对象时的翻译问题

时间:2010-10-04 12:49:02

标签: qt matrix translation transformation

我将QGraphicsPolygonItem定义为:

myShape << QPointF(-50, -50) << QPointF(50, -50)
                           << QPointF(50, 50) << QPointF(-50, 50)
                           << QPointF(-50, -50);
mypolygon.setPolygon(myShape);

它的起始矩阵是身份:

|---|---|---|
| 1 | 0 | 0 |
| 0 | 1 | 0 |
| 0 | 0 | 1 |
|---|---|---|

当我使用TransformationPivotVector =( - 50,0)扩大形状以使其大小加倍时,我得到以下矩阵:

比例后的矩阵:

|----|---|---|
| 2 | 0 | 0 |
| 0 | 1 | 0 |
| 50 | 0 | 1 |
|----|---|---|

这意味着形状的中心沿X轴平移了50个单位。

现在,给出形状当前具有缩放后的矩阵,当我打算使用TransformationPivotVector =(50,0)收缩形状时,平移自动变为负数,例如,当我收缩0.01的形状时:

|-------|---|---|
| 1.99 | 0 | 0 |
| 0 | 1 | 0 |
| -49.5 | 0 | 1 |
|-------|---|---|

我使用以下函数来获得整体转换矩阵:

myShape->setTransform(QTransform().translate(transPivotVector.x(), transPivotVector.y()).rotate(rotation).scale(xFactor, yFactor).translate(-transPivotVector.x(),-transPivotVector.y()));

该函数基本上得到一个最终矩阵:translate * rotate * scale * -translate。

我想这些功能需要包含对象的任何先前翻译,但我不知道如何。

请帮助我!!

非常感谢,

卡洛斯。

1 个答案:

答案 0 :(得分:0)

我通过重新计算轴心点来解决这个问题:

QPointF pivot = PivotPoint-pos();

同样通过计算一个新矩阵并多次使用前一个*新的。

QTransform tmpTransform = QTransform().translate(pivot.x(), pivot.y())
                .rotate(rotation)
                .scale(xFactor, yFactor)
                .translate(-pivot.x(), -pivot.y());


    tmpPolygon->setTransform(this->transform() * tmpTransform); //Multiplies the previous matrix * the temporary

卡洛斯