确定正确的原点以旋转QPolygonF [英] Determining the correct origin to rotate a QPolygonF around

查看:2752
本文介绍了确定正确的原点以旋转QPolygonF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:添加了更多信息。

Added more information.

我想在下面描述的原点周围旋转 QPolygonF

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

我想要这个多边形(图像中心是原点 - 0,0 ):

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

旋转顺时针,以便结束这里:

To rotate clockwise so that it ends up here:

QPolygonF 具有与上图中第一个块相同的位置:

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);

然后我围绕我认为是正确的起源( 2,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;

输出:

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



当我想要的输出是: p>

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:

我应该在哪里转动?

推荐答案

编辑

正确的代码实现这一点:

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;

这将给出矩形:

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

这是你期望的(虽然排序是由于旋转而改变的)。值得注意的是, rotate(90) rotate(-90)

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.

关于矩形的边缘,而不是中间 strong>

Conceptualising QT tranforms

由于qt的方向,可能会产生一些混乱 rotate 功能。虽然这将执行逆时针旋转,因为我们看到y轴反转(至少在这种情况下)它顺时针出现。

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.

作为一个类比,如果我在一个房间,观察者一盏灯掉在左边,
从某人挂在cieling它已经落在右边。

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.

这篇关于确定正确的原点以旋转QPolygonF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆