2012-02-15 44 views
0

This question solves half of my problem 因为我的滑动窗口可以在表外部移动, 例如对于3x3窗口,窗口的两列可以在表 的左端,一列将在右端。这些图像显示窗口移动到左边圆形滑动窗在2维阵列算法

enter image description hereenter image description hereenter image description here

我需要算法对于这个滑动窗口,在提到的问题滑动窗口外面的桌子不动。

+0

能人谁下来票选我的问题告诉我,他们为什么这样做,好吗? – 2012-02-15 23:56:39

+0

我不同意你Henk,圆形推拉窗是一个单独的问题/问题。 – 2012-02-16 00:38:34

+0

所以Stackoverflow“帮手”删除了他们的评论,因为,我认为,现在这个问题是确切的。你也可以删除你的倒票,所以其他人可以找到这个解决方案吗? – 2012-02-16 22:07:14

回答

2

您可以使用模运算(%)来限制索引。

Size arraySize = new Size(20, 15); 
Size windowSize = new Size(3, 3); 

double[,] array = new double[arraySize.Width, arraySize.Height]; 

// Set the location of the window 
Point windowLocation = new Point(18, 14); 

for (int x = 0; x < windowSize.Width; x++) { 
    for (int y = 0; y < windowSize.Height; y++) { 
     DoSomethingWith(array[(windowLocation.X + x) % arraySize.Width, 
           (windowLocation.Y + y) % arraySize.Height]); 
    } 
} 
+0

感谢Olivier提供的解决方案。在你的代码中有一个bug,应该用数组大小​​而不是窗口大小来完成模数。 – 2012-02-17 00:10:22

+0

@robert_d:谢谢你的提示,当然,你是对的。修复。 – 2012-02-17 14:27:23

2

我会在2D对象周围创建一个适配器,它可以拦截请求的窗口位置,查看底层的2D对象,并返回适当构造的结果。这样你可以使用任何底层实现(比如你链接的实现)并获得期望的结果。

想想下面的伪代码方面:

View getView(int leftX, int topY) { 
    if (leftX >= 0 and 
     topY >= 0 and 
     leftX <= underlying.width() - viewWidth and 
     topX <= underlying.height() - viewHeight) 
    { 
     return underlying.getView(leftX, topY); 
    } 
    // else make your own view and populate it 
    View view = new View() 
    for (int i = 0; i < viewWidth; ++i) 
     for (int j = 0; j < viewHeight; ++j) 
      view.set(i, j) = underlying.get((leftX + i) % underlying.width(), (topY + j) % underlying.height()) 
} 

如果你最终使用此代码,请确保负指数模东西给出了肯定的结果。如果不是,请使用viewWidth - negative_modulo以获得正确的索引。

+0

请问你能更具体吗? – 2012-02-15 23:58:04

+0

没有为你编写完整的解决方案,我不能更具体地说明......基本上,“底层”是实现滑动窗口而不是循环的“底层”实现。所以,如果你可以直接使用这个实现(不需要封装窗口的位置),否则,你可以创建你自己的视图并用“包装”数据“手动”填充它。 'View'实现了“窗口”的概念。 – Irfy 2012-02-16 00:00:49

+0

感谢您的代码Irfy – 2012-02-16 00:25:26