Monday, October 8, 2018

Program to fill any shape using Half-filling method in C/C++ Graphics.

                                          

                                        SOURCE CODE

#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<iostream.h>

void rboundaryfill(int x,int y,int fcolor,int bcolor)

{
int current=getpixel(x,y);
if((current!=fcolor)&&(current!=bcolor))
   {
   putpixel(x,y,fcolor);
   rboundaryfill(x+1,y,fcolor,bcolor);
   rboundaryfill(x,y+1,fcolor,bcolor);
   rboundaryfill(x,y-1,fcolor,bcolor);
   }
}

void lboundaryfill(int x,int y,int fcolor,int bcolor)

{
int current=getpixel(x,y);
if((current!=fcolor)&&(current!=bcolor))
   {
   putpixel(x,y,fcolor);
   lboundaryfill(x-1,y,fcolor,bcolor);
   lboundaryfill(x,y+1,fcolor,bcolor);
   lboundaryfill(x,y-1,fcolor,bcolor);
   }
}
void main()
{
int gd=DETECT;
int gm;
initgraph(&gd,&gm,"");
int x,y,r;
int a[10][2];
int n;
printf("Enter the no. of edges in a polygon");
scanf("%d",&n);
printf("Enter the co-ordinates of shape:-")  ;
for(int i=0;i<n;i++)
{
scanf("%d%d",&a[i][0],&a[i][1]) ;          //to take co-ordinates
}

a[n][0]=a[0][0];

a[n][1]=a[0][1];
for(int p=0;p<n;p++)
{
line(a[p][0],a[p][1],a[p+1][0],a[p+1][1]);           // to draw polygon
}

int ylargest,ysmallest,ymid;

ylargest=a[0][1];
for(i=0;i<n;i++)
{
  if(a[i+1][1]>=ylargest)
  ylargest=a[i+1][1];
}
    //to calculate ymid
ysmallest=a[0][1];
for(i=0;i<n;i++)
{
  if(a[i+1][1]<=ysmallest)
  ysmallest=a[i+1][1];
}

//printf("ylargst:-%d",ylargest);

//printf("ysmallest:-%d",ysmallest);
ymid=(ysmallest)+((ylargest-ysmallest)/2);



int xlargest,xsmallest,xmid;

xlargest=a[0][0];
for(i=0;i<n;i++)
{
  if(a[i+1][0]>=xlargest)
  xlargest=a[i+1][0];
}
     //to calculate xmid
xsmallest=a[0][0];
for(i=0;i<n;i++)
{
  if(a[i+1][0]<=xsmallest)
  xsmallest=a[i+1][0];
}
//printf("xlargst:-%d",xlargest);
//printf("xsmallest:-%d",xsmallest);
xmid=(xsmallest)+((xlargest-xsmallest)/2);


rboundaryfill(xmid+1,ymid,5,15);

lboundaryfill(xmid-1,ymid,3,15);
getch();
closegraph();
}

                                                                 OUTPUT




No comments:

Post a Comment

Source code for Happy Diwali Wishing program in C Graphics.

                                           SOURCE CODE #include<graphics.h> #include<stdio.h> #include<conio.h> ...