2017-03-02 23 views
0

我有一個列表類型爲MCvPoint3D32f的列表。 MCvPoint3D32f點是包含(x,y,z)浮點值的EmguCV類型3D點。該列表存儲一個正方形的4個角點。例如。 Square 0將有4個角點Square [0] [0],Square [0] [1]。Square [0] [3]等。如何在列表清單上執行矩陣轉換函數c#

存儲的角點順序與順序不一致我想要。例如,存儲在方塊1中的角點包含方塊3的角點,方塊6的方塊2等等。矩陣變換就是我想要在列表中做的事情。

我想這樣做,但因爲這不是一個正常的數組,但列表的列表。我沒有以正確的方式訪問或設置值。我得到了一個超出範圍數組範圍的錯誤。有沒有更好的方法來實現我想要做的事情?

The matrix

List<List<MCvPoint3D32f>> tempList = new List<List<MCvPoint3D32f>>(); 
SortMatrixIndex(Matrix); 
private void SortMatrixIndex(List<List<MCvPoint3D32f>> matrix) 
{ 
    for (int i = 0; i < matrix.Count; i++) 
     { 
      if (i == 0 || i == 3 || i == 4 || i == 8) 
      { 
       tempList[i] = matrix[i]; 
      } 
      else if (i == 5) 
      { 
       tempList[i] = matrix[i]; 
       matrix[i] = matrix[i + 2]; 
       matrix[i + 2] = tempList[i]; 
      } 
      else 
      { 
       tempList[i] = matrix[i]; 
       matrix[i] = matrix[i * 3]; 
       matrix[i * 3] = tempList[i]; 
      } 
     } 
    } 
+1

不是http://stackoverflow.com/questions/6950495/linq-swap-columns-into-rows回答你的問題? – rkrahl

+0

我不這麼認爲。我甚至不理解那些代碼在做什麼。但我想交換我的方塊的內部元素。即。存儲在Square [1]到Square [3]等中的4個角點。 –

+0

基本上我想交換內部元素 –

回答

0

這未必是解決上述問題的最佳方法,因爲它幾乎是硬編碼爲3x3矩陣,但工程現在。

private void TransposeMatrix(List<List<MCvPoint3D32f>> matrix) 
{ 
    //This case applies to both N and W, however N needs column swapping too 
    for (int i = 0; i < matrix.Count; i++) 
     { 
      tempList.Add(new List<MCvPoint3D32f>()); 

      if (i == 1 || i == 2) 
      { 
       tempList[i] = matrix[i]; 
       matrix[i] = matrix[i * 3]; 
       matrix[i * 3] = tempList[i]; 
      } 
      else if (i == 5) 
      { 
       tempList[i] = matrix[i]; 
       matrix[i] = matrix[i + 2]; 
       matrix[i + 2] = tempList[i]; 
      } 
      else 
      { 
       tempList[i] = matrix[i]; 
      } 
     } 

     tempList.Clear(); 
     this.squareMatt = matrix; 
    }