如何计算 QPolygon 面积

Posted

技术标签:

【中文标题】如何计算 QPolygon 面积【英文标题】:How calculate QPolygon area 【发布时间】:2021-05-16 16:16:12 【问题描述】:

我正在尝试获取蓝色和绿色 QPolygon 的交集,以及它由蓝色 QPolygon 组成的百分比(见下图)。

我创建的多边形如下:

import PyQt5
from PyQt5 import QtWidgets,QtGui,QtCore

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

import sys, math


class MyWidget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        QtWidgets.QWidget.__init__(self, parent)
        self.pen1 = QtGui.QPen(QtGui.QColor(20,0,250))                      # set lineColor
        self.pen = QtGui.QPen(QtGui.QColor(0,0,0))                      # set lineColor
        self.pen2 = QtGui.QPen(QtGui.QColor(0,250,0))                      # set lineColor

        self.pen.setWidth(3)                                            # set lineWidth
        self.brush = QtGui.QBrush(QtGui.QColor(255,255,255,10))        # set fillColor
        
        coords = [PyQt5.QtCore.QPointF(640.0, 334.0), PyQt5.QtCore.QPointF(626.0, 462.0), PyQt5.QtCore.QPointF(782.0, 509.0), PyQt5.QtCore.QPointF(807.0, 373.0), PyQt5.QtCore.QPointF(807.0, 333.0)]
        greenpolygoncoords = [PyQt5.QtCore.QPointF(698.0, 373.0), PyQt5.QtCore.QPointF(690.0, 433.0), PyQt5.QtCore.QPointF(757.0, 376.0), PyQt5.QtCore.QPointF(713.0, 351.0), PyQt5.QtCore.QPointF(713.0, 351.0), PyQt5.QtCore.QPointF(698.0, 373.0)]
        self.bluepolygon = QPolygonF(coords)
        self.greenpolygon =QPolygonF(greenpolygoncoords)
        self.intersection = self.bluepolygon.intersected(self.greenpolygon)


        print("overlap = "+str(self.intersection.size()/self.bluepolygon.size()))


    def paintEvent(self, event):
        painter = QtGui.QPainter(self)
        painter.setPen(self.pen1)
        painter.setBrush(self.brush)
        painter.drawPolygon(self.bluepolygon) #bluepolygon = Blue
        painter.setPen(self.pen)
        painter.drawPolygon(self.intersection) #Intersection = black
        painter.setPen(self.pen2)
        painter.drawPolygon(self.greenpolygon) # greenpolygon = Green
app = QtWidgets.QApplication(sys.argv)

widget = MyWidget()
widget.show()

sys.exit(app.exec_())

但是,当我尝试计算两个多边形之间的重叠时,作为蓝色多边形的百分比,我得到了错误的答案(它说重叠等于 1,但是,绿色多边形肯定不会100% 的蓝色多边形)。

overlap = self.intersection.size()/self.bluepolygon.size()

查看由 QPolygon.size() 计算的大小时,很明显 QPolygon.size() 返回的多边形大小不正确。

    print("size of blue polygon = "+str(self.bluepolygon.size()))
    print("size of greenpolygon = "+str(self.greenpolygon.size()))
    print("size of intersection = "+str(self.intersection.size()))

返回:

size of blue polygon = 5
size of greenpolygon = 6
size of intersection = 5

显然出于 PyQt5 的某些原因,蓝色多边形小于绿色多边形。怎么会?

我做错了什么?

【问题讨论】:

【参考方案1】:

size() 方法不返回多边形的面积,而是返回边数或顶点数。如果你想计算面积,那么你必须使用the standard method来实现它:

def calculate_area(qpolygon):
    area = 0
    for i in range(qpolygon.size()):
        p1 = qpolygon[i]
        p2 = qpolygon[(i + 1) % qpolygon.size()]
        d = p1.x() * p2.y() - p2.x() * p1.y()
        area += d
    return abs(area) / 2

然后你评估你想要什么:

print(
    "overlap = "
    + str(calculate_area(self.intersection) / calculate_area(self.bluepolygon))
)

输出:

overlap = 0.0962288941619438

【讨论】:

非常感谢 eyllanesc! 为了完整性:QPolygonF.size() 返回多个边(或顶点)的原因是它实际上是一个QVector<QPointF> 加上一些方法。

以上是关于如何计算 QPolygon 面积的主要内容,如果未能解决你的问题,请参考以下文章

如何获得翻译后的 QPolygon 的终点

arcgis人均面积怎么算

Qt画图以及坐标问题

QPolygon:如何获得多边形的边缘

计算多边形的最小面积矩形

如何减去 QPainterPaths,QPolygon?