C 仅在第一次调用递归函数时初始化值

C 仅在第一次调用递归函数时初始化值,c,image,function,recursion,constants,C,Image,Function,Recursion,Constants,我的任务是创建一个函数,该函数将使用与给定像素(x,y)相同的颜色对所有直接和间接邻域进行着色。基本上,它应该像油漆中的填充工具一样工作。 这就是我目前的代码: void fill(struct Image *image, int x, int y, uint8_t newGrayValue) { int grayValue = image->data[y * image->width + x]; if (image->data[y * image->w

我的任务是创建一个函数,该函数将使用与给定像素(x,y)相同的颜色对所有直接和间接邻域进行着色。基本上,它应该像油漆中的填充工具一样工作。 这就是我目前的代码:

void fill(struct Image *image, int x, int y, uint8_t newGrayValue) {
    int grayValue = image->data[y * image->width + x];

    if (image->data[y * image->width + x] == grayValue) {
        image->data[y * image->width + x] = newGrayValue;


        fill(image, x + -1, y + -1, newGrayValue);
        fill(image, x +  0, y + -1, newGrayValue);
        fill(image, x +  1, y + -1, newGrayValue);
        fill(image, x + -1, y +  0, newGrayValue);
        fill(image, x +  1, y +  0, newGrayValue);
        fill(image, x + -1, y +  1, newGrayValue);
        fill(image, x +  0, y +  1, newGrayValue);
        fill(image, x +  1, y +  1, newGrayValue);
    }
}
问题是每次函数调用都会重置变量
greyValue
。是否可以在第一次函数调用时根据输入像素(x,y)定义
greyValue

编辑:问题解决了,这是我的最终代码:

void fillRecursive(struct Image *image, int x, int y, uint8_t grayValue, uint8_t newGrayValue) {
    if (image->data[y * image->width + x] == grayValue) {
        image->data[y * image->width + x] = newGrayValue;


        fillRecursive(image, x + -1, y + -1, grayValue, newGrayValue);
        fillRecursive(image, x +  0, y + -1, grayValue, newGrayValue);
        fillRecursive(image, x +  1, y + -1, grayValue, newGrayValue);
        fillRecursive(image, x + -1, y +  0, grayValue, newGrayValue);
        fillRecursive(image, x +  1, y +  0, grayValue, newGrayValue);
        fillRecursive(image, x + -1, y +  1, grayValue, newGrayValue);
        fillRecursive(image, x +  0, y +  1, grayValue, newGrayValue);
        fillRecursive(image, x +  1, y +  1, grayValue, newGrayValue);
    }
}

void fill(struct Image *image, int x, int y, uint8_t newGrayValue) {
    int grayValue = image->data[y * image->width + x];
    printf("%d\n", grayValue);
    if (grayValue != newGrayValue) {
        fillRecursive(image, x, y, grayValue, newGrayValue);
    }
}
将其设置为静态:

static int grayValue = 0;
if(grayValue == 0)
grayValue=image->data[y * image->width + x];
将其设置为静态:

static int grayValue = 0;
if(grayValue == 0)
grayValue=image->data[y * image->width + x];

使
grayValue
成为递归函数的参数,以便 可以将其传递到下一个呼叫:

void recursive_fill(struct Image *image, int x, int y, uint8_t grayValue, uint8_t newGrayValue)
{
    if (image->data[y * image->width + x] == grayValue) {
        image->data[y * image->width + x] = newGrayValue;
        recursive_fill(image, x + -1, y + -1, grayValue, newGrayValue);
        // ...
    }
}
然后用

void fill(struct Image *image, int x, int y, uint8_t newGrayValue)
{
    recursive_fill(image, x, y, image->data[y * image->width + x], newGrayValue);
}

使
grayValue
成为递归函数的参数,以便 可以将其传递到下一个呼叫:

void recursive_fill(struct Image *image, int x, int y, uint8_t grayValue, uint8_t newGrayValue)
{
    if (image->data[y * image->width + x] == grayValue) {
        image->data[y * image->width + x] = newGrayValue;
        recursive_fill(image, x + -1, y + -1, grayValue, newGrayValue);
        // ...
    }
}
然后用

void fill(struct Image *image, int x, int y, uint8_t newGrayValue)
{
    recursive_fill(image, x, y, image->data[y * image->width + x], newGrayValue);
}

根据赋值,函数调用必须是
void fill(struct Image*Image,int x,int y,uint8\u t newGrayValue)
。所以这不起作用。@user3000387:请看编辑后的答案,fill函数看起来与此完全相同,但使用了一个带有附加参数的递归帮助函数。-请注意,如果您仅在第一次函数调用时(使用静态变量)才真正分配grayValue,那么您以后将无法再进行第二次填充。谢谢。创建助手函数非常有用。根据赋值,函数调用必须是
void fill(struct Image*Image,int x,int y,uint8\t newGrayValue)
。所以这不起作用。@user3000387:请看编辑后的答案,fill函数看起来与此完全相同,但使用了一个带有附加参数的递归帮助函数。-请注意,如果您仅在第一次函数调用时(使用静态变量)才真正分配grayValue,那么您以后将无法再进行第二次填充。谢谢。创建一个助手函数非常有用。现在我的输出图像看起来像。当我只是检查
0
而不是
greyValue
时,它看起来像。现在我的输出图像看起来像。当我只是检查
0
而不是
greyValue
时,它看起来像。