Thread: Help required in image filling program.

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    5

    Question Help required in image filling program.

    Hello,
    The task is to print out a screen of pixels ' . ' and draw a circle using '*' and then fill it with '*' .
    My program does the first two task perfectly but there seems be a problem with the third part (filling) . Instead of filling only inside the figure it fills the whole screen.
    This program implements 8-connectivity algorithm , I have tried implementing 4-connectivity also but that too does'nt work. If I try printing a large screen then it simply then program simply exits with and error message.
    Please Help me with this . I am a beginner in C , so please be a little more descriptive while explaining solutions.

    THANKS

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <math.h>

    Code:
    struct screen
    {
    	int w;
    	int h;
    	char** mat;
    };
    
    void fill_2(struct screen* scr,int x,int y)
    {
    	int x1,xx,y1,yy ;
    	scr->mat[x][y] = '*' ;
    	for(xx = -1; xx <= 1; xx++)
    		for(yy = -1; yy<=1; yy++)
    		{
    			x1 = x + xx ;
    			y1 = y + yy ;
    			if((x1 >= 0) && (x1 < scr->w))
    				if((y1 >= 0) && (y1 < scr->h))
    					if(scr->mat[x1][y1] == '.')
    						fill_2(scr,x1,y1);
    		}
    }
    
    void draw_circle(struct screen* scr,int x,int y, int r)
    {
    	int deg = 0,i,j;
    	float th;
    	while(deg <= 360)
    	{
    		th = (3.14*deg)/180 ;
    		i  = x + r*cos(th) + 0.5 ;
    		j  = y + r*sin(th) + 0.5 ;
    		scr->mat[i][j] = '*' ;
    		deg = deg+5;
    	}
    }
    
    int main(void)
    {
    	int x,y,r,i,j;
    	struct screen scr;
    	printf("Enter screen width and height\n");
    	scanf("%d %d", &scr.w,&scr.h);
    	scr.mat = (char**)malloc(scr.h * sizeof(char*));
    	for(i=0; i < scr.h ; i++)
    		scr.mat[i] = (char*)malloc((scr.w + 1) * sizeof(char));
    	for(i=0 ; i < scr.h; i++)
    	{
    		for(j=0; j < scr.w; j++)
    			scr.mat[i][j] = '.' ;
    		scr.mat[i][scr.w] = '\0';
    	}
    	for(i=0; i < scr.h; i++)
    		printf("%s\n",scr.mat[i]);
    
    	printf("Enter circle centre and radius\n");
    	scanf("%d %d %d",&x,&y,&r);
    	draw_circle(&scr,x,y,r);
                    for(i=0; i < scr.h; i++)
    		printf("%s\n",scr.mat[i]);
    
    	printf("Enter cordinates\n");
    	scanf("%d %d",&x,&y);
    	fill_2(&scr,x,y);
                    for(i=0; i < scr.h; i++)
    		printf("%s\n",scr.mat[i]);
    	
    	return 0;
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    Code:
    **.
    ..*
    ..*
    When you get to the red point
    you'll start checking all nighbors - getting to the blue which is outside the circle - and still you call the fill function for this point, and thus - fill all the area

    Probably - you need to check only 4 nighbors (up-down, left-right, instead of 8)
    Last edited by vart; 03-04-2008 at 02:45 PM.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem reading tiff image files?
    By compz in forum C++ Programming
    Replies: 9
    Last Post: 10-30-2009, 04:17 AM
  2. image download program
    By yoda855 in forum Windows Programming
    Replies: 6
    Last Post: 06-30-2006, 11:37 AM
  3. Replies: 6
    Last Post: 03-03-2005, 03:52 AM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. Replies: 2
    Last Post: 05-10-2002, 04:16 PM