找到矩形的边界集
Find bounding set of rectangles
我有 std::vector
个 rectangle
个。 class 的定义很简单:
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;
};
对于每个矩形,我想看看是否与向量中的另一个矩形相交,然后 'unite' 它们(找到边界矩形)如果 它们相交。
我很想知道是否有办法使用 <algorithm>
中的函数对一系列矩形执行 search/combining。谁能就可能的解决方案提出建议?寻找简洁而不需要重新发明轮子的东西。
[编辑:] 我应该提到我已经实施了 '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 个矩形。
我的问题似乎比答案引起了更多的困惑。也许我不太善于解释自己。尽管如此,这是我(天真的)解决方案。
///////////////////////////////////////////////////////////////////////////
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));
}
}
我有 std::vector
个 rectangle
个。 class 的定义很简单:
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;
};
对于每个矩形,我想看看是否与向量中的另一个矩形相交,然后 'unite' 它们(找到边界矩形)如果 它们相交。
我很想知道是否有办法使用 <algorithm>
中的函数对一系列矩形执行 search/combining。谁能就可能的解决方案提出建议?寻找简洁而不需要重新发明轮子的东西。
[编辑:] 我应该提到我已经实施了 '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 个矩形。
我的问题似乎比答案引起了更多的困惑。也许我不太善于解释自己。尽管如此,这是我(天真的)解决方案。
///////////////////////////////////////////////////////////////////////////
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));
}
}