Mitch
Mitch

Reputation: 24406

Determining the correct origin to rotate a QPolygonF around

Edit: Added more information.

I'd like to rotate a QPolygonF around the origin illustrated below:

block rotations

I'd like this polygon (centre of image is origin - 0, 0):

first polygon

To rotate clockwise so that it ends up here:

enter image description here

The QPolygonF has points in the same position as the first block in the image above:

QPolygonF p1 =  QPolygonF() << QPointF(0, 1) << QPointF(4, 1) << QPointF(4, 2) << QPointF(0, 2);

I then rotate around what I think is the correct origin (2, 2):

QTransform t;
t.translate(2, 2);
t.rotate(-90);
t.translate(-2, -2);
QPolygonF p2 = t.map(p1);
qDebug() << p1 << "rotated = " << p2;

Output:

QPolygonF(QPointF(0, 1) QPointF(4, 1) QPointF(4, 2) QPointF(0, 2) ) rotated =  QPolygonF(QPointF(1, 4) QPointF(1, 0) QPointF(2, 0) QPointF(2, 4) )

When the output that I want is:

QPolygonF(QPointF(0, 1) QPointF(4, 1) QPointF(4, 2) QPointF(0, 2) ) rotated =  QPolygonF(QPointF(2, 0) QPointF(3, 0) QPointF(3, 4) QPointF(2, 4) )

But, according to the output above, the polygon would end up looking like this:

wrong rotation

At which point should I be rotating around?

Upvotes: 1

Views: 3445

Answers (2)

cmh
cmh

Reputation: 10937

Edit

After working out what you are after I now know the right code to achieve this:

QTransform t; 
t.translate(2, 2); 
t.rotate(90); 
t.translate(-2, -2); 
QPolygonF p2 = t.map(p1); 
qDebug() << p1 << "rotated = " << p2;

Which will give the rectangle:

QPolygonF(QPointF(3, 0) QPointF(3, 4) QPointF(2, 4) QPointF(2, 0) )

Which is what you expect (although the ordering is change due the rotation). It's worth noting that rotate(90) and rotate(-90) will not give the same result as you are rotating about the edge of the rectangle, not the middle of it.

Conceptualising QT tranforms

Some confusion may arise because of the direction of the qt rotate function. Although this will enact a counter clockwise rotation, because we "see" the y-axis inverted (at least in this case) it appears clockwise to us.

As an analogy if I'm in a room and observer a lamp falling over to the left, from the point of view of someone hanging on the cieling it has "fallen over" to the right.

Upvotes: 2

nutrina
nutrina

Reputation: 1042

I had just a short look, but I think this is wrong:

t.translate(2, 2);

To obtain your transformation should be:

t.translate(2, 1);

The lower bound of the rectangle is at y = 1, as far as I understand ...

Upvotes: 0

Related Questions