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;
}