【问题标题】:Find bounding set of rectangles查找矩形的边界集
【发布时间】:2023-05-12 11:56:01
【问题描述】:

我有一个std::vectorrectangle。类定义很简单:

class rectangle
{
public:
    rectangle();
    rectangle(int leftX, int topY, int width, int height);
    rectangle(const rectangle &other);

    int x() const;
    int y() const;

    int width() const;
    int height() const;

    // Returns the bounding rectangle of this rectangle and the given rectangle.
    rectangle united(const rectangle &r) const;
    rectangle intersected(const rectangle &r) const;

    // Returns true if rectangle intersects with given rectagle else false.
    bool intersects(const rectangle &r) const;
};

对于每个矩形,我想看看是否与向量中的另一个矩形相交,然后将它们“合并”(找到边界矩形)如果它们相交。

我很想看看是否有办法使用<algorithm> 中的函数在一系列矩形上执行搜索/组合。任何人都可以就可能的解决方案提出建议吗?寻找简洁而无需重新发明*的东西。

[编辑:] 我应该提到我已经实现了 'intersects()' 和 'united'。

我的最终目标是实现一个适用于我的矩形范围的函数,如下所示:

/// For each rectangle in the vector it test if it intersects with another and return the set of united rectangles. 
/// \param v A set of rectangles
/// \return A united set of rectangles.
std::vector<rectangle> test_intersect_and_unite(const std::vector<rectangle> &v)
{
    std::vector<rectangle> united;

    // ...

    return united;
}

我可能会处理少于 20 个矩形。

【问题讨论】:

  • 我看到表达的愿望,但没有实际问题。甚至是要求。你试过什么吗?比如,检查每一对?它奏效了吗?如果没有,出了什么问题?
  • @Sembei 我不认为这是重复的,因为我已经实现了“相交”和“联合”。我正在寻找 STL 中的一个函数来处理一系列矩形。
  • For each rectangle, I'd like to see if intersects another rectangle in the vector and then 'unite' them (find the bounding rectangle) if they intersect.,不,你不想要那个,你已经有了它,所以你不应该在问题中要求它。
  • 再一次,完全不清楚你想做什么;请尽量自己实现。有十几个或更多的决定,我不知道你在解决方案中想要哪个(结果去哪里?这个联合过程是否通勤?破坏性好吗?我们谈论多少元素?如果 A B C 成对相交,做我们得到 1 2 或 3 个输出矩形?),一个简单的解决方案(连同为什么它不是你想要的)回答了其中的一半以上。我知道这对你来说可能很明显,但我们无法读懂你的想法。

标签: c++ stl-algorithm


【解决方案1】:

我的问题似乎引起的困惑多于答案。也许我不太擅长解释自己。尽管如此,这是我的(天真的)解决方案。

///////////////////////////////////////////////////////////////////////////
template<template <typename, typename = std::allocator<rectangle>> class Container>
Container<rectangle> test_intersect_and_unite(const Container<rectangle> &v)
{
    Container<rectangle> vTemp{v};

    for (std::size_t i = 0; i < vTemp.size(); ++i)
    {
        for (std::size_t j = 0; j < vTemp.size(); ++j)
        {
            if (i == j) { continue; }

            if (vTemp[i].intersects(vTemp[j]))
            {
                vTemp[i]  = vTemp[i].united(vTemp[j]);
                vTemp.erase(vTemp.begin() + j);
                if (j < i) { --i; }
                --j;
                continue;
            }
        }
    }

    return vTemp;
}

一些简单的单元测试:

////////////////////////////////////////////////////////////////////////////////
class rectangle_utils_test : public testing::Test
{
public:

    rectangle_utils_test() = default;

    ~rectangle_utils_test() override = default;
};


//////////////////////////////////////////////////////////////////////////
TEST_F(rectangle_utils_test, test_intersect_and_unite)
{
    // TODO: This unit test makes some naive assumptions about ordering.
    // TODO: Test with negative values.

    {
        std::vector<rectangle> vArg = {{10, 10, 10, 10}, {15, 15, 10, 10}};

        std::vector<rectangle> v = test_intersect_and_unite(vArg);

        ASSERT_EQ(v.size(), 1);
        ASSERT_EQ(v[0], rectangle(10, 10, 15, 15));
    }

    {
        std::vector<rectangle> vArg = {{10, 10, 10, 10}, {21, 21, 10, 10}};

        std::vector<rectangle> v = test_intersect_and_unite(vArg);

        ASSERT_EQ(v.size(), 2);
        ASSERT_EQ(v[0], rectangle(10, 10, 10, 10));
        ASSERT_EQ(v[1], rectangle(21, 21, 10, 10));
    }

    {
        std::vector<rectangle> vArg = {{10, 10, 10, 10},
                                       {15, 15, 10, 10},
                                       {60, 60, 10, 10},
                                       {5,  5,  10, 10},
                                       {0,  0,  10, 10},
                                       {40, 40, 10, 10}};

        std::vector<rectangle> v = test_intersect_and_unite(vArg);

        ASSERT_EQ(v.size(), 3);
        ASSERT_EQ(v[0], rectangle(0, 0, 25, 25));
        ASSERT_EQ(v[1], rectangle(60, 60, 10, 10));
        ASSERT_EQ(v[2], rectangle(40, 40, 10, 10));
    }

    // Most interesting test case.
    {
        std::vector<rectangle> vArg = {{0,  0,  10, 10},
                                      {20, 20, 10, 10},
                                      {10, 10, 10, 10},
                                      {5,  5,  10, 10},
                                      {15, 15, 10, 10},
                                      {40, 40, 10, 10}};

        std::vector<rectangle> v = test_intersect_and_unite(vArg);

        ASSERT_EQ(v.size(), 2);
        ASSERT_EQ(v[0], rectangle(0, 0, 30, 30));
        ASSERT_EQ(v[1], rectangle(40, 40, 10, 10));
    }
}

【讨论】:

  • 我相信这里的结果将取决于订单;很容易看出,单个i=0 传递的结果是。假设 A 与 B 相交,B 与 C 相交。如果按顺序有 {A,B,C},则我们得到 {A U B, C},然后是 {A U B U C}。如果我们有 {A, C, B} 的顺序,那么在 i=0 传递之后我们有 {A U B, C}。我没有明确地构造它,但我很确定像 A B C 这样足够长的链会根据顺序产生不同的输出。也许不吧;可能 n^2 次通过可以防止这种情况。但我会担心并想要一个证据。
  • 最后一个测试用例旨在测试您所描述的内容,尽管使用的是一小部分数据。尽管如此,为了更安心,在rectangle 上覆盖operator&lt; 并使用std::next_permutation 应该相当容易。
  • 我担心一条长长的链条可能不会崩溃;没有单元测试可以排除它。你需要一个证明。