# Thread: Filling a shape

1. ## Filling a shape

Hi guys, once more I am completely stumped on a C problem. The goal is to read in the perimeter of a shape that will be marked off by the user, then read in a point inside the shape that is given by the user. I then have to write a recursive function that will "fill" the shape until it reaches a boundary, starting from the point given.

I got as far as writing a function to read in the boundaries of the shape into a multidimensional array before I got stumped =/ Here's the code for reading in the array:

Code:
```void initArray(char array[size][size])
{
char current_char;
int i=0;
int j=0;

printf("Enter a line of text:\n");
while (true)
{
current_char=getchar();
switch(current_char)
{
case 'e': break;
case ' ':break;
case '\n':j++; i=0; break;
default: array[i][j]=current_char; i++; break;
}
if (current_char=='e') break;
}
}```
Once again, thanks for the help.

2. Know what you are doing before you start, draw a diagram, write a design document, something! You have given us too little code and not even a clue about how you want the user to make the shape and mark the inside of it to start the filling from. More information please.

3. Well, I took a full fledged try at it and this is what I managed to come up with

Code:
```/*
*File:fill.c
*The program gets the perimeter of a shape from the user, and then fills in that shape
*/

#include "stdafx.h"
#include <stdio.h>
#include "simpio.h"
#define size 15

int initArray(char array[size][size]);
bool fill(int row,int column,char arr[size][size]);
void displayArray(char ne[size][size]);

main()
{
int numlines,x,y;
char array[size][size];
numlines=initArray(array);
printf("Enter x coordinate of the point from which to begin filling\n");
x=GetInteger();
printf("Enter y coordinate of the point\n");
y=GetInteger();
fill(x,y,array);
displayArray(array);
}

int initArray(char array[size][size])
{
char current_char;
int i=0;
int j=0;

for (i=1;i<size;i++)
{
for (j=1;j<size;j++)
{
array[i][j]=' ';
}
printf("\n");
}
printf("Enter a line of text:\n");
while (true)
{
current_char=getchar();
switch(current_char)
{
case 'e': break;
case ' ':break;
case '\n':j++; i=0; break;
default: array[i][j]=current_char; i++; break;
}
if (current_char=='e') break;
}
return i;
}

bool fill(int row,int column,char arr[size][size])
{
if ((arr[row+1][column]!='*')||(arr[row][column+1])) return true;
else
{
if ((arr[row+1][column]=='*')||(arr[row][column+1])) return false;
if (fill(row+1,column,arr)) arr[row+1][column]='*';
if (fill(row,column+1,arr)) arr[row][column+1]='*';
}
}

void displayArray(char ne[size][size])
{
int i,j;
for (i=1;i<size;i++)
{
for (j=1;j<size;j++)
{
if (ne[i][j]!=' ') printf("%4d",(ne[i][j]));
}
printf("\n");
}
}```
Let me clarify the purpose of the program. The purpose is to have the user basically fill out the perimeter of a shape using asterisks.
The then give a point inside the shape, and the recursive proceedure, fill, should fill outwards from that point untill it reaches a boundary.

4. Well if you are looking for a floodfill they are all over the net. Beware though - using a recursive floodfill will result in a stack overflow on large shapes.

Code:
```void FloodFill(DWORD *Buffer,DWORD offset,DWORD width,RGB color,RGB bordercolor)
{
if (Buffer[offset]==bordercolor) return;

Buffer[offset]=color;

FloodFill(Buffer,offset-width,width,color,bordercolor);
FloodFill(Buffer,offset-1,width,color,bordercolor);
FloodFill(Buffer,offset+1,width,color,bordercolor);
FloodFill(Buffer,offset+width,width,color,bordercolor);
}```

5. Ok, well I got the entire program working except for the heart of it, the recurtsion. Unfortunately, I'm not looking for a floodfill, but rather, the function should fill outwards from a given point. The function itself should take three arguments: row and column of the current point checked, and the multidimensional array representing the array. The only hint I was given was "Think of a simple end condition, and of the neighboring cells." Right now, I have the following, but, unfortunately, it doesn't work.

Code:
```bool fill(int row,int column,char arr[size][size])
{
if ((arr[row][column])==('*')) return false;
else
{
if ((arr[row][column])!=('*')) return true;
if (fill(row,column,arr)!=false) arr[row][column]='*';
if (fill(row+1,column,arr)) arr[row+1][column]='*';
if (fill(row,column+1,arr)) arr[row][column+1]='*';
if (fill(row-1,column,arr)) arr[row-1][column]='*';
if (fill(row,column-1,arr)) arr[row][column-1]='*';
}

}```

6. Unfortunately, I'm not looking for a floodfill, but rather, the function should fill outwards from a given point.

And how does this differ from a floodfill?

Popular pages Recent additions