【问题标题】:Arrange expanded/shrunk rectangles in a 2D space在 2D 空间中排列扩展/缩小的矩形
【发布时间】:2017-04-11 13:12:23
【问题描述】:

在二维空间中,有一些矩形。它们会增长或收缩,有些会同时增长/收缩。当一个矩形变大时,它的左上角永远不会移动,即只有底部或右边缘可以移动。

我们需要重新定位重叠的矩形。我们要确保重新定位的 2D 空间在视觉上与之前的空间相似(即,我们不应该打乱它们,不应该引入不必要的空白,(如果可能的话)不应该改变每个矩形最近的矩形)。

已知值:

  • 空间大小
  • 两个相邻矩形之间的距离
  • 矩形的大小(改变前后的大小)
  • 矩形的位置

如何有效地重新定位矩形?应该使用什么算法才能通过一次操作解决一组碰撞(因为某些矩形可能对齐良好并且具有相同的大小)?我们如何根据坐标猜测相邻矩形之间的关系,并巧妙地重新定位它们。

一些例子:

  • 案例 1:垂直增长/收缩(简单)
  • 案例2:水平增长/收缩(复杂。假设有2个矩形组,leftright,而不是topbottom。当left发生变化时,right重新- 位置如下图所示。)
  • 案例 3:水平扩展/收缩(简单)

当前解决方案

 //required values: distance between two adjacent rectangles, sizes of rectangles
 sort(rectangles) // from top-left to bottom-right
 foreach (rect in rectangles)
     if (distance between two adjacent rectangles changed)
         get number of grown/shrunk rows/columns
         foreach (rect2 in rectangles below/to the right of rect)
             move rect2 according to the grown/shrunk rows
             if collision is solved after vertical movement
                 continue
             else
                 move horizontally

此解决方案在情况 2 中失败,因为小蓝色矩形的相对距离已更改,但其大小未更改。底部两个矩形之间的距离不会为 0。

【问题讨论】:

  • “如果两个矩形之间的相对位置/距离在某些情况下不改变就完美了” - 我认为你真的需要澄清'相对位置的含义' 和 'some case' 以获得有用的答案。

标签: java algorithm


【解决方案1】:

我尝试了几种方法(我已经考虑了这个问题大约一个多月),我发现以下方法可能更好。 我想我们必须知道矩形之间的关系(它们是如何分组的)才能使重新定位的矩形在视觉上与以前的版本相似。

// Use 2D array to represent the 2D space
ConstructRectangleGroups()
foreach (item in array) // item is the pixel in 2D space
    if (item contains more than 1 rectangles) // has collision
        foreach (rect in item's rectangles)
            get number of rect's grown/shrunk rows/columns
            foreach (rect2 in rectangleGroups[rectangleGroups.FindIndex(rect)])
                move rect2 according to the grown/shrunk rows
                if collision is solved after vertical movement
                    continue
                else
                    move horizontally

ConstructRectangleGroups() 是

rectangleGroups = [][]
foreach (item in array)
    if (item has rectangle)
        closestRectangles = rectangle.GetClosestRectangles()
        // A rectangle is isolated if the distance between adjacent rectangle > MaxDistance
        // In this case, closestRectangles will be empty.
        if (closestRectangles.Count > 0)
            foreach (closestRectangle in closestRectangles)
                if (closestRectangle.ClosestRectangle != rectangle)
                    rectangle.ClosestRectangle = closestRectangle
            if (rectangle.closestRectangle == null)
                rectangle.ClosestRectangle = closestRectangles[0]
foreach (rect in rectangles)
    if (rect.ClosestRectangle == null)
        rectangleGroups.Add(new [] { rect })
    else
        index = rectangle.FindIndex(rect)
        if (index < 0)
            rectangleGroups.Add(new [] { rect })
        else
            rectangleGroups[index].Add(rect.ClosestRectangle)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-08
    • 2021-06-03
    • 1970-01-01
    • 1970-01-01
    • 2016-10-11
    • 1970-01-01
    • 2022-06-12
    • 2021-10-02
    相关资源
    最近更新 更多