2011-11-21 93 views
2

聽起來對我來說我有一個非常基本的需求:提取組成矩陣的區域的座標。從矩陣獲取區域/區域邊界

讓我舉個例子。這裏是一些矩陣:

| A | B | D | E | F | G | H | I | J | 
| 1 | 0 | 0 | 0 | 2 | 2 | 2 | 4 | 4 | 4 | 
| 2 | 0 | 0 | 2 | 2 | 2 | 2 | 4 | 4 | 4 | 
| 3 | 0 | 0 | 2 | 2 | 2 | 3 | 3 | 4 | 4 | 
| 4 | 1 | 1 | 1 | 2 | 2 | 3 | 3 | 4 | 4 | 
| 5 | 1 | 1 | 1 | 2 | 2 | 3 | 3 | 4 | 4 | 
| 6 | 1 | 1 | 1 | 2 | 2 | 3 | 3 | 4 | 4 | 
| 7 | 1 | 0 | 0 | 0 | 1 | 1 | 3 | 0 | 4 | 
| 8 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 0 | 0 | 
| 9 | 0 | 0 | 0 | 1 | 1 | 1 | 1 | 0 | 0 | 
| 10| 0 | 0 | 0 | 1 | 1 | 1 | 1 | 0 | 0 | 

而且我想獲得一個數組的邊界和每個區域的值(不需要特定的順序)。

舉例左上區域:

  • 值:0
  • 界限:{A1,D1,B2,B3,13}

你知道一些庫回答這個需要還是應該我自己編碼?

+0

我不確定我瞭解您的示例。爲什麼A1是一個邊界,但不是B1或A2?如果邊不計爲邊界A1不應該是邊界。 –

+0

@Ron Warholic:當一條線存在時,我只給了極端。的確,你的建議也適合。 – apneadiving

回答

1

我自己編碼,不確定是否有庫。

我會考慮每個地區的每個點。然後(我認爲)這應該工作:

if (surrounding 8 squares has at least one with different region) 
{ 
    for each 3 squares, above, below, left and right 
    { 
     if (less than 3 are different, and the middle is different) 
     { 
      is a boundry 
     } 
    } 

    for each 3 squares, above, below 
    { 
     for each 3 squares, left, right 
     { 
      if(all 3 from outer loop and all 3 from inner loop are different) 
      { 
       is a boundry 
      } 
     } 
    } 

    not a boundry 
} 
else 
{ 
    not a boundry 
} 

對待邊界正方形不同。

+0

感謝您的回答和+1 – apneadiving