2011-01-25 79 views
0

我在vb.net中將可變大小的窗口滑過二維數組時出現問題。當我將數組的第一個元素設置爲0,0時,我的問題是,窗口的大小需要變小,因爲所討論的元素必須是滑動窗口的中心。例如:arrar大小(40,43)窗口大小5x5(窗口大小是NxN N = 3勝大小是3x3)所以數組(0,0)與勝利大小5所以2 col和2行需要cout和一個新的窗口大小爲3x3。任何幫助,將great.`vb.net中的2d數組滑動窗口

Public Function getPIXELSinWINDOW(ByVal Wsize As Integer, ByVal x As Integer, ByVal y As Integer) 

     Dim tempARRAY As New ArrayList() 
     Dim Xwidth As Integer = Wsize 
     Dim Yheight As Integer = Wsize 
     Dim Xvalue As Integer = x - Wsize/2 
     Dim Yvalue As Integer = y - Wsize/2 
     Dim imgHEIGHT As Integer = Me.mysize.Height 
     Dim imgWIDTH As Integer = Me.mysize.Width 
     Dim i, j As Integer 


     While Xvalue < 0 
      Xvalue += 1 
      Xwidth -= 1 
     End While 
     While Xvalue > imgWIDTH 
      Xvalue -= 1 
      Xwidth -= 1 
     End While 
     While Xwidth >= imgWIDTH 
      Xwidth -= 1 
     End While 
     While Xvalue + Xwidth > imgWIDTH 
      Xwidth -= 1 
     End While 

     While Yvalue < 0 
      Yvalue += 1 
      Yheight -= 1 
     End While 
     While Yvalue > imgHEIGHT 
      Yvalue -= 1 
      Yheight -= 1 
     End While 
     While Yheight >= imgHEIGHT 
      Yheight -= 1 
     End While 
     While Yvalue + Yheight > imgHEIGHT 
      Yheight -= 1 
     End While 

     For i = Xvalue To Xvalue + Xwidth 
      For j = Yvalue To Yvalue + Yheight 
       tempARRAY.Add(pixels(j, i)) 
      Next 
     Next 

     Return tempARRAY 
    End Function 

了var像素是二維數組

回答

0

像這樣的事情?

public class MainClass 
{ 
    public static void Main (string[] args) 
    { 
     MainClass mc = new MainClass(); 
     List<int> pix = mc.GetPixelsInWindow(5, 40, 43);    
    } 

    private int[,] data = new int[40, 43]; 

    public List<int> GetPixelsInWindow(int windowSize, int xPoint, int yPoint) 
    { 
     // As we are dealing with integers, this will result in a truncated value, 
     // so a windowSize of 5 will yield a windowDelta of 2. 
     int windowDelta = windowSize/2; 
     List<int> pixels = new List<int>(); 

     int xMin = Math.Max(0, xPoint - windowDelta); 
     int xMax = Math.Min(data.GetLength(0) - 1, xPoint + windowDelta); 

     int yMin = Math.Max(0, yPoint - windowDelta); 
     int yMax = Math.Min(data.GetLength(1) - 1, yPoint + windowDelta); 

     for (int x = xMin; x <= xMax; x++) 
     { 
      for (int y = yMin; y <= yMax; y++) 
      { 
       Console.WriteLine("Getting pixel (" + x.ToString() + ", " + y.ToString() + ")..."); 
       pixels.Add(data[x, y]); 
      } 
     } 

     return pixels; 
    } 
} 
+0

噢,抱歉在C#中的例子,但我希望你能理解它! :) – ManiSto

+0

即時通訊不知道如何閱讀這個權利,但如果在數據陣列20,10即時通訊將會給我25個值的完整窗口? – bob

+0

是的。您可以創建一個控制檯項目,粘貼此類並將其作爲主類運行。這樣你可以玩不同的值來確保它按預期工作。另外,如果您願意,我可以添加更多代碼評論的版本。 – ManiSto