【问题标题】:Translation problem while scaling an object缩放对象时的平移问题
【发布时间】:2010-10-04 12:49:01
【问题描述】:

我有一个 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。

我猜这些函数需要包含对象的任何先前翻译,但我不知道如何。

请帮帮我!!

非常感谢,

卡洛斯。

【问题讨论】:

    标签: qt matrix translation transformation


    【解决方案1】:

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

    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
    

    卡洛斯

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-04
      • 2017-05-16
      • 2019-02-28
      • 2021-06-18
      • 2011-05-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多