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

For loop 编写嵌套for循环的正确方法?,for-loop,nested,gml,For Loop,Nested,Gml,我有一个程序,可以将图像分割成50 x 50像素块进行编辑,我已经尝试了3天没有成功地编写了一个嵌套的for循环来满足我的需要,在此之前,我尝试分别获取宽度段和高度段,然后尝试获取每个段的面积,但这根本不起作用,因此,我尝试了几种使用嵌套for循环的方法,但有一些我没有做到或做得不正确,任何帮助都将不胜感激,因为我没有想法!以下是我当前的等式,首先列出并解释了变量: j = (picture1.bbox_right + 1) - picture1.bbox_left; //gets image

我有一个程序,可以将图像分割成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];
        }
    }
}
j=(picture1.bbox\u right+1)-picture1.bbox\u left//获取以像素为单位的图像宽度
k=(picture1.bbox_底部+1)-picture1.bbox_顶部//获取以像素为单位的图像高度
h=j*k//图像中的像素总数
s=j模50//图像x平面中的剩余像素
t=k模50//y平面中的剩余像素
c=ceil(j/50)//x平面中的分段数,分段最大宽度为50像素
d=ceil(k/50)//y平面分段中的分段数最多可以为50像素高
brk=c*d//图像中的段总数
对于(i=0;i c)
{

对于(i=0;i你的想法太复杂了。你只需要两个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;
    }
}

你的想法太复杂了。你只需要两个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;
    }
}

再次感谢@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;
    }
}
}
a=0;
b=0;
bwatch=0;
c=0;
d=0;
j=(图1.bbox\u右+1)-(图1.bbox\u左);
k=(图1.bbox\u底部+1)-(图1.bbox\u顶部);
g=图1.bbox_左;
l=图1.bbox\u top-1;
h=(j*k);
i=0;
ii=0;
s=j模50;
t=k模50;
c=(ceil(j/50));
d=(ceil(k/50));
brk=c*d;

对于(i=0;i再次感谢@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;
    }
}
}
a=0;
b=0;
bwatch=0;
c=0;
d=0;
j=(图1.bbox\u右+1)-(图1.bbox\u左);
k=(图1.bbox\u底部+1)-(图1.bbox\u顶部);
g=图1.bbox_左;
l=图1.bbox\u top-1;
h=(j*k);
i=0;
ii=0;
s=j模50;
t=k模50;
c=(ceil(j/50));
d=(ceil(k/50));
brk=c*d;

对于(i=0;iI建议使用更好的变量名。您的代码几乎不可读。我建议使用更好的变量名。您的代码几乎不可读。啊,非常感谢!我确实让它变得比应该的复杂,我最终创建了一个WHILE循环并使其工作,但这也相当复杂,您的方式更复杂aight Forward,再次感谢您的帮助!啊,非常感谢!我确实让它变得比它应该的更复杂,我最终创建了一个WHILE循环并使其工作,但这也相当复杂,您的方式更直接,再次感谢您的帮助!