2011-01-25 36 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