编写嵌套 for 循环的正确方法?
Correct way to write a nested for loop?
我有一个程序可以将图像分成 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 的结果与我试图获得的结果 -
我正在寻找的输出是每个线段的起始 x,y 位置和每个线段的面积。
你想的太复杂了。你只需要两个 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
再次感谢 @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;
}
}
}
我有一个程序可以将图像分成 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 的结果与我试图获得的结果 -
我正在寻找的输出是每个线段的起始 x,y 位置和每个线段的面积。
你想的太复杂了。你只需要两个 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
再次感谢 @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;
}
}
}