Filling a shape

This is a discussion on Filling a shape within the C Programming forums, part of the General Programming Boards category; Hi guys, once more I am completely stumped on a C problem. The goal is to read in the perimeter ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    14

    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. #2
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    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.
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    14
    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. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    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. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    14
    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. #6
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    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 subscribe to a feed

Similar Threads

  1. Finding the outside of arbitrary shape
    By JackR in forum Game Programming
    Replies: 17
    Last Post: 08-21-2008, 02:49 PM
  2. Help required in image filling program.
    By co123ol in forum C Programming
    Replies: 1
    Last Post: 03-04-2008, 01:42 PM
  3. Newbie- having trouble with functions
    By bnmwad in forum C Programming
    Replies: 7
    Last Post: 02-22-2005, 03:41 PM
  4. Area of irregular shape
    By Brian in forum Game Programming
    Replies: 4
    Last Post: 06-23-2004, 10:54 AM
  5. circle filling
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 06-08-2002, 05:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21