0

This question solves half of my problem because my sliding window can move outside the table, e.g. for 3x3 window, two columns of the window can be on the left end of the table and one column will be on the right end. These images show window moving to the left

enter image description here enter image description here enter image description here

I need algorithm for this sliding window, sliding window in the mentioned question doesn't move outside the table.

3
  • Can persons who down voted my question tell me why they did this, please? Commented Feb 15, 2012 at 23:56
  • I disagree with you Henk, circular sliding window is a separate problem/question. Commented Feb 16, 2012 at 0:38
  • So Stackoverflow "helpers" removed their comments, because, I presume, now this question is precise. Can you remove your down votes too, so other persons can find this solution, please? Commented Feb 16, 2012 at 22:07

2 Answers 2

2

You can use the modulo operation (%) in order to confine the indexes.

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]);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Olivier for the solution. There is one bug in your code, modulo should be done with array size not window size.
@robert_d: Thank you for the hint, of course, you are right. Fixed it.
2

I would create an adapter around your 2D object, which intercepts requested window position, consults the underlying 2D object, and returns an appropriately constructed result. This way you may use any underlying implementation (like the one you linked to) and get the desired result.

Think in terms of the following pseudo-code:

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())
}

If you end up using this code, make sure that negative indices modulo something gives a positive result. If not, use viewWidth - negative_modulo to get the right index.

3 Comments

Can you be more specific please?
I can't be more specific than this without writing the complete solution for you... Basically, underlying is the "underlying" implementation that implements sliding window without being circular. So you use that implementation directly if you can (location of window doesn't need to be wrapped), otherwise, you make your own view and populate it "manually" with "wrapped" data. View implements the "window" concept.
I just threw down some simple, working Python code that demonstrates the principle. It's not C#, but Python reads very well as pseudo-code. And if you can run it, it'll work. See if it helps further.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.