c++ - 在 QPolygon 中查找完全包含的正方形?

标签 c++ qt polygon intersection

我正在寻找一种方法来找到一组完全包含在 QPolygon 中的正方形,它不一定是凸的。到目前为止,我天真的方法是这样的:

QRectF boundingRect(mShape->boundingRect());
for (int x = boundingRect.x() - 1; x < boundingRect.width(); x++)
{
    for (int y = boundingRect.y() - 1; y < boundingRect.height(); y++)
    {
        QRectF rect(x, y, 1, 1);
        QPolygonF cell(rect);
        QPolygonF intersection = mShape->polygon().intersected(cell);
        if (!intersection.empty())
        {
            // Cell is fully contained
        }
    }
}

当我可视化结果时,它看起来像这样:

Cells intersecting with the polygon are not left out

这几乎就是我想要的,除了与多边形“轮廓”相交的单元格不应该在那里。有谁知道如何构建一组完全位于多边形“内部”的正方形?

最佳答案

假设较大的多边形是凸的(在您的示例中),检查正方形的所有四个角是否都在较大的多边形内就足够了。在较大的多边形上使用 containsPoint 方法。

关于c++ - 在 QPolygon 中查找完全包含的正方形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13228045/

相关文章:

c# - 如何在 C# 中映射 HALF_PTR

c++ - 内存泄漏(OpenCV + QML)

algorithm - 找到相邻多边形的轮廓

php - 多边形算法中的点有时会给出错误的结果

graphics - 这段代码的复杂度是多少?

c++ - 为什么我在 Apple Silicon 上编译的 native 应用程序有时构建为 arm64,有时构建为 x86_64?

c++ - 以下符号是什么意思?

c++ - Qt debian/ubuntu : Can't compile, 错误:找不到-lGL

c++ - 点积作为 Armadillo 的乘法

c++ - QT:图片作为窗口