Search code examples
for-loopnestedgml

Correct way to write a nested for loop?


I have a program that splits an image into 50 x 50 pixel chunks for editing, I have been trying unsuccessfully for 3 days to write a nested for loop to do what I need, before that I have tried getting the width segments and height segments seperatly then trying to get each segment's area, but that didn't work at all, so I have tried several approaches to using a nested for loop but there's something I'm missing or not doing correctly, any help would be greatly appreciated as I'm out of ideas! Here is my current equasion with the variables listed and explained first:

j = (picture1.bbox_right + 1) - picture1.bbox_left; //gets image width in pixels
k = (picture1.bbox_bottom + 1) - picture1.bbox_top; //gets image height in pixels
h = j*k; //total number of pixels in image
s = j mod 50; //remaining pixels in image x plane
t = k mod 50; //remaining pixels in y plane
c = ceil(j/50); //number of segments in x plane, segments can be a maximum of 50 pixels wide
d = ceil(k/50); //number of segments in y plane segments can be a maximum of 50 pixels tall
brk = c*d; //total number of segments in image
for (i=0; i<h+1; i+=1) z[i] = 0;
for (i=0; i<=brk+1; i+=1) {v[i] = 0; w[i] = 0; z[i] = 0;} //v[] is the segment width, w[] is the segment height, z[] is the area of the segment
//drwatx[] and m[] are the starting x position of each segment
//drwaty[] and n[] are the starting y position of each segment

if(h > 2500)
{
    if(d > c)
    {
        for(i=0; i<brk; i+=1)
        {
            for(a=0; a<d-1; a+=1)
            {
                if(a < d-1)
                {
                    for(b=0; b<c-1; b+=1)
                    {
                        if(b < c-1)
                        {
                            m[b + i] = picture1.bbox_left + b*50;
                            drwatx[b + i] = picture1.bbox_left + b*50;
                            v[b + i] = 50;
                        }
                        if (b == c-1 && s == 0) v[b + i] = 50;
                        if (b == c-1 && s > 0) v[b + i] = s;
                    }
                    if(a < d-1)
                    {
                        n[a + i] = picture1.bbox_top - 1 + a*50;
                        drwaty[a + i] = picture1.bbox_top - 1 + a*50;
                        w[a + i] = 50;
                    }
                    if(a == d-1 && s == 0)
                    {
                        w[a + i] = 50;
                    }
                    if(a == d-1 && s > 0)
                    {
                        v[a + i] = s;
                        w[a + i] = t;
                    }
                }
            }
            z[i] = v[i]*w[i];
        }
    }
}

Here is a drawing with more info and the results of my nested forloop vs the results I'm trying to get- NESTED FOR LOOP RESULTS

the output I'm looking for is the starting x,y position of each segment and each segment's area.


Solution

  • You are thinking way too complicated. You only need two for loops. Finding the x and y position of the segments is quite easy. Finding the area as well, complicates it a bit more.

    segment_width = 50;
    segment_height = 50;
    image_width = 73;
    image_height = 183;
    x = 12; // x position of image
    y = 148; // y position of image
    
    // Looping through the segment rows by incrementing the current
    // y-coordinate value j by segment_height
    for (j = 0; j <= image_height; j += segment_height)
    {
        // Segment size of the current segment
        current_width = 0;
        current_height = 0;
        if (image_height - j < segment_height)
        {
            // If we are on the last row, calculate the segment height
            // by subtracting the image_height by the current pixel
            current_height = image_height - j;
        }
        else
        {
            // Else, we know that the segment height is 50
            current_height = segment_height;
        }
    
        for (i = 0; i <= image_width; i += segment_width)
        {
            if (image_width - i < segment_width)
            {
                current_width = image_width - i;
            }
            else
            {
                current_width = segment_width;
            }
    
            // Calculate the segment area
            z[i*j] = current_width*current_height;
    
            // Calculate the segment position
            drawx[floor(i/segment_width)] = x + i;
            drawy[floor(j/segment_height)] = y + j;
        }
    }
    

    I originally wrote my code in C++ and tried my best to convert to GML, so don't shoot me if it doesn't compile at first attempt. Anyway, this should give you:

    drawx[0] = 12
    drawx[1] = 62
    drawx[2] = 12
    drawx[3] = 62
    drawx[4] = 12
    drawx[5] = 62
    drawx[6] = 12
    drawx[7] = 62
    
    drawy[0] = 148
    drawy[1] = 148
    drawy[2] = 198
    drawy[3] = 198
    drawy[4] = 248
    drawy[5] = 248
    drawy[6] = 298
    drawy[7] = 298
    
    z[0] = 2500
    z[1] = 1150
    z[2] = 2500
    z[3] = 1150
    z[4] = 2500
    z[5] = 1150
    z[6] = 1650
    z[7] = 759