Determining the correct origin to rotate a QPolygonF around

流过昼夜 提交于 2019-12-08 04:51:16

问题


Edit: Added more information.

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

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

To rotate clockwise so that it ends up 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:

At which point should I be rotating around?


回答1:


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.




回答2:


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 ...



来源:https://stackoverflow.com/questions/10186102/determining-the-correct-origin-to-rotate-a-qpolygonf-around

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!