PYQT5:填充Qpolygon的孔//增加Qpolygon的大小(高度)
我需要增加其高度的大小,才能获得关注的qpolygon:
< img src =“ https://i.sstatic.net/dun6k.png” alt =“在此处输入图像说明”> ,
而qpolygon的下边界则保持在同一位置,即x1,y1 in第一张图像是第二张图像中仍然是x1,y1,上面添加的黑色区域应该是qpolygon本身高度的一半(理想情况下是直线,但不太重要
) qpolygon
poly1_bounding_Rect = qpolygonf(poly_1_coords).boundingRect()
和x0,y0坐标以及宽度和高度,然后使多边形与原始的多边形合成,boundingRect
max_y, min_x, height_, width_ = poly1_bounding_rect.y(), poly1_bounding_rect.x(), poly1_bounding_rect.height(), poly1_bounding_rect.width()
poly1_addition = QPolygonF(
[QPointF(min_x, max_y), QPointF(min_x, max_y - height_ * .5),
QPointF(min_x + width_, max_y - height_ * .5),
QPointF(min_x + width_, max_y)])
()该解决方案使Qpolygon带有孔,我不确定我应该如何填充它们,或者首先创建一个没有这些批发的Qpolygon。
非常感谢任何指导,谢谢!
这是一个最小可重复的示例:
import PyQt5
from PyQt5 import QtCore
import sys
import PyQt5
from PyQt5.QtCore import *#QPointF, QRectF
from PyQt5.QtGui import *#QPainterPath, QPolygonF, QBrush,QPen,QFont,QColor, QTransform
from PyQt5.QtWidgets import *#QApplication, QGraphicsScene, QGraphicsView, QGraphicsSimpleTextItem
poly_1_coords= [PyQt5.QtCore.QPointF(412.01, 257.98), PyQt5.QtCore.QPointF(372.24, 197.66), PyQt5.QtCore.QPointF(358.88, 230.95), PyQt5.QtCore.QPointF(371.46, 278.4), PyQt5.QtCore.QPointF(381.16, 291.77), PyQt5.QtCore.QPointF(428.34, 308.81), PyQt5.QtCore.QPointF(483.66, 341.84), PyQt5.QtCore.QPointF(648.02, 373.29), PyQt5.QtCore.QPointF(945.05, 386.61), PyQt5.QtCore.QPointF(1087.66, 374.02), PyQt5.QtCore.QPointF(1285.26, 331.05), PyQt5.QtCore.QPointF(1317.34, 304.71), PyQt5.QtCore.QPointF(1345.85, 265.03), PyQt5.QtCore.QPointF(1340.6, 214.96), PyQt5.QtCore.QPointF(1326.19, 197.39), PyQt5.QtCore.QPointF(1303.38, 243.79), PyQt5.QtCore.QPointF(1134.3, 290.19), PyQt5.QtCore.QPointF(1091.57, 306.71), PyQt5.QtCore.QPointF(1067.45, 295.44), PyQt5.QtCore.QPointF(1017.38, 250.09), PyQt5.QtCore.QPointF(993.53, 218.63), PyQt5.QtCore.QPointF(925.89, 219.41), PyQt5.QtCore.QPointF(852.23, 233.31), PyQt5.QtCore.QPointF(723.52, 219.41), PyQt5.QtCore.QPointF(671.51, 309.58), PyQt5.QtCore.QPointF(638.88, 313.37), PyQt5.QtCore.QPointF(583.5, 299.71), PyQt5.QtCore.QPointF(485.61, 240.53), PyQt5.QtCore.QPointF(451.47, 270.88)]
def main():
app = QApplication(sys.argv)
scene = QGraphicsScene()
view = QGraphicsView(scene)
pen_ = QPen(QColor(0, 20, 255))
brush_ = QBrush(QColor(0,0,0))
poly_1 = QPolygonF(poly_1_coords)
#scene.addPolygon(poly_1,pen_,brush_)
poly1_bounding_rect = poly_1.boundingRect()
max_y, min_x, height_, width_ = poly1_bounding_rect.y(), poly1_bounding_rect.x(), poly1_bounding_rect.height(), poly1_bounding_rect.width()
poly1_addition = QPolygonF(
[QPointF(min_x, max_y), QPointF(min_x, max_y - height_ * .5),
QPointF(min_x + width_, max_y - height_ * .5),
QPointF(min_x + width_, max_y)])
complete_poly1 = poly_1.united(poly1_addition)
scene.addPolygon(complete_poly1,pen_,brush_)
view.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
I have the following QPolygon:
and I need to increase its size in height to get a following QPolygon:
whereas the lower boundary of the qpolygon stays in the same location, that is x1,y1 in the first image, is still x1,y1 in the second image and the black area that is added above should be half of the height of the qpolygon itself (ideally with a straight line, but not too important)
To do so, I took the bounding box of the QPolygon
poly1_bounding_rect=QPolygonF(poly_1_coords).boundingRect()
and the X0,Y0 coordinates, as well as width and height, and then made the polygon addition to be united with the original polygon
max_y, min_x, height_, width_ = poly1_bounding_rect.y(), poly1_bounding_rect.x(), poly1_bounding_rect.height(), poly1_bounding_rect.width()
poly1_addition = QPolygonF(
[QPointF(min_x, max_y), QPointF(min_x, max_y - height_ * .5),
QPointF(min_x + width_, max_y - height_ * .5),
QPointF(min_x + width_, max_y)])
However this solution leaves the QPolygon to be with holes and I am unsure how should I fill them in, or alternatively create a QPolygon without these wholes in the first place.
Any guidance is highly appreciated, thanks!
Here's a minimum reproducible example:
import PyQt5
from PyQt5 import QtCore
import sys
import PyQt5
from PyQt5.QtCore import *#QPointF, QRectF
from PyQt5.QtGui import *#QPainterPath, QPolygonF, QBrush,QPen,QFont,QColor, QTransform
from PyQt5.QtWidgets import *#QApplication, QGraphicsScene, QGraphicsView, QGraphicsSimpleTextItem
poly_1_coords= [PyQt5.QtCore.QPointF(412.01, 257.98), PyQt5.QtCore.QPointF(372.24, 197.66), PyQt5.QtCore.QPointF(358.88, 230.95), PyQt5.QtCore.QPointF(371.46, 278.4), PyQt5.QtCore.QPointF(381.16, 291.77), PyQt5.QtCore.QPointF(428.34, 308.81), PyQt5.QtCore.QPointF(483.66, 341.84), PyQt5.QtCore.QPointF(648.02, 373.29), PyQt5.QtCore.QPointF(945.05, 386.61), PyQt5.QtCore.QPointF(1087.66, 374.02), PyQt5.QtCore.QPointF(1285.26, 331.05), PyQt5.QtCore.QPointF(1317.34, 304.71), PyQt5.QtCore.QPointF(1345.85, 265.03), PyQt5.QtCore.QPointF(1340.6, 214.96), PyQt5.QtCore.QPointF(1326.19, 197.39), PyQt5.QtCore.QPointF(1303.38, 243.79), PyQt5.QtCore.QPointF(1134.3, 290.19), PyQt5.QtCore.QPointF(1091.57, 306.71), PyQt5.QtCore.QPointF(1067.45, 295.44), PyQt5.QtCore.QPointF(1017.38, 250.09), PyQt5.QtCore.QPointF(993.53, 218.63), PyQt5.QtCore.QPointF(925.89, 219.41), PyQt5.QtCore.QPointF(852.23, 233.31), PyQt5.QtCore.QPointF(723.52, 219.41), PyQt5.QtCore.QPointF(671.51, 309.58), PyQt5.QtCore.QPointF(638.88, 313.37), PyQt5.QtCore.QPointF(583.5, 299.71), PyQt5.QtCore.QPointF(485.61, 240.53), PyQt5.QtCore.QPointF(451.47, 270.88)]
def main():
app = QApplication(sys.argv)
scene = QGraphicsScene()
view = QGraphicsView(scene)
pen_ = QPen(QColor(0, 20, 255))
brush_ = QBrush(QColor(0,0,0))
poly_1 = QPolygonF(poly_1_coords)
#scene.addPolygon(poly_1,pen_,brush_)
poly1_bounding_rect = poly_1.boundingRect()
max_y, min_x, height_, width_ = poly1_bounding_rect.y(), poly1_bounding_rect.x(), poly1_bounding_rect.height(), poly1_bounding_rect.width()
poly1_addition = QPolygonF(
[QPointF(min_x, max_y), QPointF(min_x, max_y - height_ * .5),
QPointF(min_x + width_, max_y - height_ * .5),
QPointF(min_x + width_, max_y)])
complete_poly1 = poly_1.united(poly1_addition)
scene.addPolygon(complete_poly1,pen_,brush_)
view.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设多边形始终是一个简单的,而没有任何交叉点,则可以做以下操作:
另外,您可以根据需要翻译顶部:
Assuming that the polygon is always a simple one, without any intersection, you can do the following:
Alternatively, you can add the top side by translating it as required:
不是最佳或最优雅的答案,而是解决问题。
我在初始多边形轮廓点上循环,减少y坐标并将其与初始多边形团结起来,从而有效地产生更多的所需结果。
如果有人提出了一个更最佳的解决方案,将被接受!
Not the most optimal nor elegant answer, but solves the issue.
I loop through the initial polygon contour points, reduce the y coordinate and unite it with the initial polygon, thus effectively creating moreorless the desired result.
If anyone proposes a more optimal solution, will be accepted!