2010-10-04 65 views
0

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

我想這些功能需要包括任何以前的對象翻譯,但我不知道如何。

請幫助我!

非常感謝提前,

卡洛斯。

回答

0

我通過重新計算樞軸點固定此:

QPointF樞軸= PivotPoint-POS();

另外通過計算一個新的矩陣和mutiply前面*新的。

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 

卡洛斯