Qt 5.5 如何围绕位置旋转多边形
Qt 5.5 how to rotate polygon around position
我有一个多边形:
QPolygon plyNeedle;
plyNeedle << QPoint(ptOrigin.x() - intNeedleHalfWidth, ptOrigin.y())
<< QPoint(ptOrigin.x(), ptOrigin.y() + intNeedleHalfWidth)
<< QPoint(ptOrigin.x() + intRadius - intNeedleHalfWidth, ptOrigin.y())
<< QPoint(ptOrigin.x(), ptOrigin.y() - intNeedleHalfWidth);
ptOrigin是QPoint类型,定义了旋转点。 intNeedleHalfWidth 是一个整数,值为 4。intRadius 是一个整数,定义针的长度。
我想围绕多边形的第二个索引旋转多边形,但是怎么做?
(编辑)...
在尝试 Anton Savin 建议的答案之前和转换:
ptOrigin (40, 250)
plyNeedle (36,250),(40,254),(287,250),(40,246)
执行后:
plyNeedle = QTransform().translate(-ptOrigin.x(), -ptOrigin.y())
.rotate(45)
.translate(ptOrigin.x(), ptOrigin.y())
.map(plyNeedle);
然后将 plyNeedle 添加到 QPainter:
QBrush brshArrow;
brshArrow.setColor(mcpszARGBneedle);
brshArrow.setStyle(Qt::SolidPattern);
objOffscrPainter.setPen(mcpszARGBneedle);
QPainterPath path;
path.addPolygon(plyNeedle);
objOffscrPainter.drawPolygon(plyNeedle);
objOffscrPainter.fillPath(path, brshArrow);
未显示任何内容,在调查 plyNeedle 时包含:
plyNeedle (-340,157),(-340,163),(-162,335),(-334,157)
像这样:
QPoint origin = ...;
plyNeedle = QTransform()
.translate(-origin.x, -origin.y)
.rotate(angle)
.translate(origin.x, origin.y)
.map(plyNeedle);
感谢 "Anton Savin",他让我走上了正确的道路,经过一番尝试后,实际的解决方案是:
plyNeedle << QPoint(ptOrigin.x() - intNeedleHalfWidth, ptOrigin.y())
<< QPoint(ptOrigin.x(), ptOrigin.y() + intNeedleHalfWidth)
<< QPoint(ptOrigin.x() + intRadius - intNeedleHalfWidth, ptOrigin.y())
<< QPoint(ptOrigin.x(), ptOrigin.y() - intNeedleHalfWidth);
plyNeedle = QTransform().translate(ptOrigin.x(), ptOrigin.y())
.rotate(-mfltElevation)
.translate(-ptOrigin.x(), -ptOrigin.y())
.map(plyNeedle);
现在可以完美运行了。
我有一个多边形:
QPolygon plyNeedle;
plyNeedle << QPoint(ptOrigin.x() - intNeedleHalfWidth, ptOrigin.y())
<< QPoint(ptOrigin.x(), ptOrigin.y() + intNeedleHalfWidth)
<< QPoint(ptOrigin.x() + intRadius - intNeedleHalfWidth, ptOrigin.y())
<< QPoint(ptOrigin.x(), ptOrigin.y() - intNeedleHalfWidth);
ptOrigin是QPoint类型,定义了旋转点。 intNeedleHalfWidth 是一个整数,值为 4。intRadius 是一个整数,定义针的长度。
我想围绕多边形的第二个索引旋转多边形,但是怎么做?
(编辑)... 在尝试 Anton Savin 建议的答案之前和转换:
ptOrigin (40, 250)
plyNeedle (36,250),(40,254),(287,250),(40,246)
执行后:
plyNeedle = QTransform().translate(-ptOrigin.x(), -ptOrigin.y())
.rotate(45)
.translate(ptOrigin.x(), ptOrigin.y())
.map(plyNeedle);
然后将 plyNeedle 添加到 QPainter:
QBrush brshArrow;
brshArrow.setColor(mcpszARGBneedle);
brshArrow.setStyle(Qt::SolidPattern);
objOffscrPainter.setPen(mcpszARGBneedle);
QPainterPath path;
path.addPolygon(plyNeedle);
objOffscrPainter.drawPolygon(plyNeedle);
objOffscrPainter.fillPath(path, brshArrow);
未显示任何内容,在调查 plyNeedle 时包含:
plyNeedle (-340,157),(-340,163),(-162,335),(-334,157)
像这样:
QPoint origin = ...;
plyNeedle = QTransform()
.translate(-origin.x, -origin.y)
.rotate(angle)
.translate(origin.x, origin.y)
.map(plyNeedle);
感谢 "Anton Savin",他让我走上了正确的道路,经过一番尝试后,实际的解决方案是:
plyNeedle << QPoint(ptOrigin.x() - intNeedleHalfWidth, ptOrigin.y())
<< QPoint(ptOrigin.x(), ptOrigin.y() + intNeedleHalfWidth)
<< QPoint(ptOrigin.x() + intRadius - intNeedleHalfWidth, ptOrigin.y())
<< QPoint(ptOrigin.x(), ptOrigin.y() - intNeedleHalfWidth);
plyNeedle = QTransform().translate(ptOrigin.x(), ptOrigin.y())
.rotate(-mfltElevation)
.translate(-ptOrigin.x(), -ptOrigin.y())
.map(plyNeedle);
现在可以完美运行了。