2017-04-11 19 views
0

2D空間では、いくつかの矩形があります。彼らは成長したり収縮したり、あるものは同時に成長/収縮したりします。矩形が大きくなると、その左上隅は決して動かず、すなわち底部または右端部だけが動くことができる。2D空間内の拡大/縮小矩形の整列

重複した四角形を再配置する必要があります。再配置された2D空間が以前の空間と視覚的に似ていることを確認したい(つまり、シャッフルしないでください。不要な空白を挿入しないでください)。

既知の値:スペースの

  • サイズ
  • 隣接する二つの矩形(変更後&前大きさ)の長方形の
  • サイズの長方形の
  • 位置との間の距離

どのようにして矩形を効率的に再配置できますか?衝突のグループが1つの操作で解決できるようにアルゴリズムを使用する必要があります(いくつかの矩形が整列して同じサイズを持つ可能性があるため)。座標に基づいて隣接する矩形間の関係を推測し、スマートに再配置するにはどうすればよいですか。

いくつかの例:

  • ケース1:縮小/成長垂直(シンプル)
  • ケース2:複雑(水平縮小/成長これは2長方形基、leftrightはなく、がある前提。以下の図に示すようにleft変更は、rightが再配置されtopbottom
  • ケース3:
0)、水平(単純縮小/成長

Examples

現在のソリューション

//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に失敗し、そのサイズは変更されません。下の2つの四角形の間の距離は0になりません。

+0

である - 私はあなたが本当に意味を明確にする必要があると思います有用な回答を得るためには、「相対的な位置」と「いくつかの場合」の2つがあります。 –

答えて

0

私はいくつかのアプローチを試みました(私はこの問題について約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()*「2つの矩形間の相対的な位置/距離は、いくつかのケースでは変更されていない場合、それは完璧になる」*

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) 
関連する問題