Computer Graphics Program source codes with full description.

Study of Various C Graphics Functions

Implementation and Using mouse in DOS

Implementation of DDA line algorithm with source code in C/C++.

Implementation of Bresenham Line algorithm with source code in C/C++.

Implementation of Midpoint Line algorithm with source code in C/C++.

Implementation of Bresenham Circle algorithm with source code in C/C++.

Implementation of Mid-point Circle algorithm with source code in C/C++.

Implementation of Mid-point Ellipse algorithm with source code in C/C++.

Implementation of Polygon Filling using Scan Fill with source code in C/C++.

Implementation of Polygon Filling using Flood Fill with source code in C/C++.

Implementation of Polygon Filling using Boundary Fill Algorithms.

Implementation of algorithm of 2D Transformation of an Object with source code in C/C++.

Implementation of Line Clipping using Cohen- Sutherland algorithm with source code in C/C++.

Implementation of Line Clipping using Liang-Barky algorithm with source code in C/C++.

Implementation of Polygon clipping using Sutherland-hodgeman algorithm with source code in C/C++.

Search

Friday 1 July 2011

Implementation of Flood Fill, Boundary Fill,Scanfill algorithm with source code in C/C++

Source code:


#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<dos.h>
#include<stdlib.h>

struct Node
{
    int x;
    int y;
    struct Node* next;
};

void fill (int pt[][2], int clr);
void floodfill4 (int x, int y, int oldclr, int newclr);
void insert (int x, int y, struct Node** last);

void main()
{
int x,y,xmax,ymax,q,i,j,clr,pt[3][2];
char str[20];
int gdriver = DETECT,gmode,errorcode;

 start: clrscr();
initgraph(&gdriver,&gmode,"C:\\TC\\BGI");
errorcode = graphresult();

if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}

xmax=getmaxx();
ymax=getmaxy();

//rectangle(0,100,xmax,ymax);
lineDDA(xmax/2,80,xmax/2,ymax);
lineDDA(0,ymax/2,xmax,ymax/2);

while(1)
{
moveto(xmax/2,ymax/2);
gotoxy(0,2);
printf("0:Exit,1:FloodFill,2:ScanLine,3:BoundaryFill");
gotoxy(1,3);
printf("What to do:   ");
gotoxy(12,4);
printf("Enter the x- and y-coordinates for three points: ");
for (i=0; i<3; i++)
{for (j=0; j<2; j++)
{scanf ("%d", &pt[i][j]);}}

printf ("Enter the fill-colour: (Any number from 1 to 14) ");
scanf ("%d", &clr);
// fill (pt, clr);
/*gotoxy(12,5);
printf("x1:      ");
gotoxy(12,6);
printf(" y1       ");
gotoxy(12,7);
printf("x2    ");
gotoxy(12,8);
printf("y2   ");
gotoxy(12,9);
printf("x3    ");
gotoxy(12,10);
printf("y3   ");*/
gotoxy(12,3);
scanf("%d",&q);
if(q==0) break;
if(q!=1&&q!=2&&q!=3)
{
gotoxy(20,6);
printf("Enter valid choice.");
getch();
continue;
}
/*gotoxy(14,5);
scanf("%d",&x1);
gotoxy(14,6);
scanf("%d",&y1);
gotoxy(14,7);
scanf("%d",&x2);
gotoxy(14,8);
scanf("%d",&y2);
gotoxy(14,9);
scanf("%d",&x3);
gotoxy(14,10);
scanf("%d",&y3);*/
if(q==1)  fill (pt, clr);
getch();
goto start;

}

}

void fill (int pt[][2], int clr)
{
    int gd = DETECT, gm;
    int seedx, seedy;

    initgraph (&gd, &gm, "..\\bgi");

    setcolor (WHITE);
    line (pt[0][0], pt[0][1], pt[1][0], pt[1][1]);
    line (pt[1][0], pt[1][1], pt[2][0], pt[2][1]);
    line (pt[2][0], pt[2][1], pt[0][0], pt[0][1]);
    getch();

    seedx = (pt[0][0] + pt[1][0] + pt[2][0]) / 3;
    seedy = (pt[0][1] + pt[1][1] + pt[2][1]) / 3;

    floodfill4 (seedx, seedy, BLACK, clr);
    getch();

    closegraph();
    return;
}

void floodfill4 (int x, int y, int oldclr, int newclr)
{
    struct Node* first, *last, *tmp;

    first = (struct Node*) malloc (sizeof (struct Node));
    if (first == NULL)
    {
closegraph();
fprintf (stderr, "floodfill4: Out of memory.\n");
exit (2);
    }
    if (oldclr == newclr)
    {
free (first);
return;
    }

    first->x = x;
    first->y = y;
    first->next = NULL;
    last = first;

    while (first != NULL)
    {
putpixel (x, y, newclr);

if (getpixel (x, y-1) == oldclr)
{
   putpixel (x, y-1, newclr);
   insert (x, y-1, &last);
}


if (getpixel (x, y+1) == oldclr)
{
   putpixel (x, y+1, newclr);
   insert (x, y+1, &last);
}

if (getpixel (x-1, y) == oldclr)
{
   putpixel (x-1, y, newclr);
   insert (x-1, y, &last);
}

if (getpixel (x+1, y) == oldclr)
{
   putpixel (x+1, y, newclr);
   insert (x+1, y, &last);
}

tmp = first;
first = first->next;
x = first->x;
y = first->y;
free (tmp);
    }
}

void insert (int x, int y, struct Node** last)
{
    struct Node* p;
    p = (struct Node*) malloc (sizeof (struct Node));
    if (p == NULL)
    {
closegraph();
fprintf (stderr, "\n insert: Out of memory.\n");
exit (2);
    }

    p->x = x;
    p->y = y;
    p->next = NULL;
    (*last)->next = p;
    *last = (*last)->next;
}


Click here to download the sorce code.

1 comment:

  1. Nice program. I came across an easy program of flood fill in C.
    http://jee-appy.blogspot.in/2011/10/flood-fill-program-in-c.html

    ReplyDelete

Copyright Text

Copyright @ LDRP Student Community, webmaster Rahul Bhadauriya