Create a sliding window over whole image by adjusting code

I have what is probably a simple problem to fix for anyone who know what they are doing a bit better than me.

I have this code which I have been using to analyse images by creating a square roi which moves by a whole square in the x direction and when it gets to the end of the image moves down by a whole square in the y direction and starts again from the left of the image. Now I would like the squares to overlap.

I have managed to get the grid squares to overlap by adding *0.5 to lines 13 and 14 but now it is only working over a quarter of the image. How would I tweak the code to get the roi squares to overlap the way they are doing now but over the whole image?

Any help would be greatly appreciated!

run("Set Measurements...", "area perimeter area_fraction centroid fit redirect=None  decimal=2");

A=200;   //Size of local area in pixels

w = getWidth();
h = getHeight();
setForegroundColor(0, 0, 0);
for (j=1;j<=nSlices;j++){
        setSlice(j);
        for (ii=0;ii<h/A;ii++){
                for (i=0;i<w/A;i++){
                        run("Clear Results");
                        x=i*A*0.5;
                        y=ii*A*0.5;
                        makeRectangle(x, y, A, A);
						roiManager ("add");
                        run("Analyze Particles...", "size=0-Infinity circularity=0.00-1.00  show=Nothing include");
                      
                }
        }
}




You need to account for the 0.5 factor also in the for loop, by replacing A by (A*0.5) there.

Here’s a version of your code having to separate variables to account for area (your A) and stepSize (your A*0.5):

run("Set Measurements...", "area perimeter area_fraction centroid fit redirect=None  decimal=2");

area=200;   //Size of local area in pixels
stepSize = area * 0.5;

w = getWidth();
h = getHeight();
setForegroundColor(0, 0, 0);
for (j=1;j<=nSlices;j++){
        setSlice(j);
        for (ii=0;ii<h/stepSize;ii++){
                for (i=0;i<w/stepSize;i++){
                        run("Clear Results");
                        x=i*stepSize;
                        y=ii*stepSize;
                        makeRectangle(x, y, area, area);
						roiManager ("add");
                        run("Analyze Particles...", "size=0-Infinity circularity=0.00-1.00  show=Nothing include");
                      
                }
        }
}

I recommend naming variables meaningfully, so that their name conveys what they’re used for.
See for example: https://blog.usejournal.com/naming-your-variables-f9477ba002e9