我在使用QuadTree计算颜色的函数时遇到问题。传入2维数组并基于节点偏移量,通过对所有值求和并找到平均值来计算颜色。如果平均值为0,则颜色= 0(白色)。如果平均值是1色= 1(黑色)。最后,如果平均值是任何不是0或1的值,则颜色= 2(灰色)。因此,如果节点的行偏移为0并且其行为64且cols为64,那么当它在imgAry [0] [64]结束时,总和应该总结为imgAry [0] [0] temp增加所以它是imgAry [1] [0]等等。然而,似乎元素的总和不起作用。在while和for循环中是否存在错误的逻辑?
int computeColor(QuadTreeNode* node, int** imgAry)
{
int sum;
int temp = node->rowOffset;
int temp2 = node->rowOffset + node->nrows;
int total;
int color;
while (temp <= temp2)
{
for (int i = 0; i < node->ncols; i++)
{
sum += imgAry[temp][i];
}
temp++;
}
cout << " Sum of the Array = " << sum << endl;
total = node->nrows * node->ncols;
cout << " Total elements in the Array = " << total << endl;
if (sum == 0)
{
color = 0;
}
else if (sum / total == 1)
{
color = 1;
}
else if (sum > 0)
{
color = 2;
}
cout << "Color is = " << color << endl;
return color;
}