开发者

Translation problem while scaling an object

开发者 https://www.devze.com 2023-01-18 12:06 出处:网络
I have a QGraphicsPolygonItem defined as: myShape << QPointF(-50, -50) << QPointF(50, -50)

I have a QGraphicsPolygonItem defined as:

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

Its starting matrix is identity:

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

When I dilate the shape to double its size with a TransformationPivotVector = (-50,0开发者_开发百科) I get the following matrix:

Matrix after Scale:

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

This means that the center of the shape has been translated by 50 units along the X axis.

Now, Giving that the shape currently has the matrix after scale, When I intent to contract the shape using a TransformationPivotVector = (50,0) the translation automatically becomes negative, see for example when I contract just 0.01 of the shape:

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

I use the following function to get an overall transformation matrix:

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

The function basically gets a final matrix from: translate * rotate * scale * -translate.

I guess these function needs to include any previous translation of the object but I don’t know how.

Please help me!!

Many thanks in advance,

Carlos.


I fixed this by recalculating the pivot point:

QPointF pivot = PivotPoint-pos();

Also by calculating a new matrix and mutiply the previous * the new.

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

Carlos

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号