编写嵌套for循环的正确方法?

时间:2016-09-05 01:15:19

标签: for-loop nested gml

我有一个程序将图像分成50 x 50像素的块进行编辑,我已经尝试了3天没有成功写一个嵌套的for循环来做我需要的东西,在此之前我尝试获取宽度段和高度细分,然后试图得到每个细分区域,但这根本不起作用,所以我尝试了几种方法来使用嵌套的for循环但是我有一些东西遗失或不正确,任何帮助将非常感激,因为我没有想法!这是我目前的方面,首先列出并解释变量:

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];
        }
    }
}

这是一张图片,其中包含更多信息以及我的嵌套forloop与我尝试获取的结果的结果 - NESTED FOR LOOP RESULTS

我要查找的输出是每个段和每个段的区域的起始x,y位置。

2 个答案:

答案 0 :(得分:2)

你的想法太复杂了。你只需要两个for循环。找到段的x和y位置非常容易。找到该区域也会使其复杂化。

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;
    }
}

我最初用C ++编写代码并尽力转换为GML,所以如果第一次尝试不编译,请不要开枪。无论如何,这应该给你:

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

答案 1 :(得分:0)

再次感谢@Kake_Fisk解决了我的问题,并教我一些关于FOR循环的东西,我还找到了一种方法来解决它,使用while循环,所以对于需要这个解决方案的人来说,这是一个不同的方法:

a=0;
b=0;
bwatch=0;
c=0;
d=0;
j=(picture1.bbox_right+1)-(picture1.bbox_left);
k=(picture1.bbox_bottom+1)-(picture1.bbox_top);
g=picture1.bbox_left;
l=picture1.bbox_top-1;
h=(j*k);
i=0;
ii=0;
s=j mod 50;
t=k mod 50;
c=(ceil(j/50));
d=(ceil(k/50));
brk=c*d;
for(i=0;i<h+1;i+=1)
{
colsmake[i]=0;
}
for(i=0;i<=brk+1;i+=1)
{
v[i]=0;
w[i]=0;
z[i]=0;
drwatx[i]=0;
drwaty[i]=0;
colorize[i]=0;
}
if(h<=2500)
{drwatx[bwatch]=picture1.bbox_left;
drwaty[bwatch]=picture1.bbox_top-1;
z[bwatch]=h;
}
if(h>2500)
{
if(d>c)
{
    while(a<c && b<d-1)
    {
    if(a<c)
        {
        drwatx[ii]=picture1.bbox_left+(a*50);
    if s=0 v[ii]=50;
    if s !=0 && a<c-1 v[ii]=50;
    if s !=0 && a=c-1 v[ii]=s;
        drwaty[ii]=picture1.bbox_top-1+(b*50);
        w[ii]=50;
        z[ii]=v[ii]*w[ii];
        a+=1;
        ii+=1;
        }
    if(a=c)
        {
        b+=1;
        a=0;
    }
}
while(a<c && b=d-1)
    {
        drwatx[ii]=picture1.bbox_left+(a*50);
    if s=0 v[ii]=50;
    if s !=0 && a<c-1 v[ii]=50;
    if s !=0 && a=c-1 v[ii]=s;
        drwaty[ii]=picture1.bbox_top-1+(b*50);
    if t=0 w[ii]=50;
    if t !=0 w[ii]=t;
        z[ii]=v[ii]*w[ii];
        a+=1;
        ii+=1;
    }
}
}