0% found this document useful (0 votes)
89 views8 pages

Pratiksha D.Laldas SE Computer A Roll No. 53 Exiperiment No: 05 Aim: Implementation of Polygon Filling Algorithm. 1. Boundary-Fill Algorithm Code

The document describes experiments implementing two polygon filling algorithms: boundary fill and flood fill. For boundary fill, a function recursively calls itself to fill pixels adjacent to the initial pixel. For flood fill, a flood function recursively fills pixels of the same original color as the initial pixel. Both algorithms are implemented in C code and tested by filling shapes on a graphics screen. The conclusion states that both polygon filling algorithms were successfully studied and implemented.

Uploaded by

piyush
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views8 pages

Pratiksha D.Laldas SE Computer A Roll No. 53 Exiperiment No: 05 Aim: Implementation of Polygon Filling Algorithm. 1. Boundary-Fill Algorithm Code

The document describes experiments implementing two polygon filling algorithms: boundary fill and flood fill. For boundary fill, a function recursively calls itself to fill pixels adjacent to the initial pixel. For flood fill, a flood function recursively fills pixels of the same original color as the initial pixel. Both algorithms are implemented in C code and tested by filling shapes on a graphics screen. The conclusion states that both polygon filling algorithms were successfully studied and implemented.

Uploaded by

piyush
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Pratiksha D.

Laldas
SE Computer A
Roll No. 53
Exiperiment No : 05
Aim: Implementation of Polygon Filling Algorithm.
1. Boundary-Fill Algorithm
Code:
#include <graphics.h>
void boundaryFill4(int x, int y, int fill_color,int boundary_color)
{
if(getpixel(x, y) != boundary_color &&
getpixel(x, y) != fill_color)
{
putpixel(x, y, fill_color);
boundaryFill4(x + 1, y, fill_color, boundary_color);
boundaryFill4(x, y + 1, fill_color, boundary_color);
boundaryFill4(x - 1, y, fill_color, boundary_color);
boundaryFill4(x, y - 1, fill_color, boundary_color);
}
}

int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
int x = 250, y = 200, radius = 50;
circle(x, y, radius);
boundaryFill4(x, y, 6, 15);
delay(10000);
getch();
closegraph();
return 0;
}
Output:

2. Flood-Fill Algorithm
Code:
// program to fill polygon using floodfill
// algorithm
#include <graphics.h>
#include <stdio.h>

  
// flood fill algorithm

void flood(int x, int y, int new_col, int old_col)


{

    // check current pixel is old_color or not

    if (getpixel(x, y) == old_col) {

  

        // put new pixel with new color

        putpixel(x, y, new_col);

  

        // recursive call for bottom pixel fill

        flood(x + 1, y, new_col, old_col);


  

        // recursive call for top pixel fill

        flood(x - 1, y, new_col, old_col);

  

        // recursive call for right pixel fill

        flood(x, y + 1, new_col, old_col);

  

        // recursive call for left pixel fill

        flood(x, y - 1, new_col, old_col);

    }
}

  

int main()
{

    int gd, gm = DETECT;

  

    // initialize graph

    initgraph(&gd, &gm, "");

  

    // rectangle coordinate

    int top, left, bottom, right;

  

    top = left = 50;

    bottom = right = 300;


  

    // rectangle for print rectangle

    rectangle(left, top, right, bottom);

  

    // filling start cordinate

    int x = 51;

    int y = 51;

  

    // new color to fill

    int newcolor = 12;

  

    // new color which you want to fill

    int oldcolor = 0;

  

    // call for fill rectangle

    flood(x, y, newcolor, oldcolor);

    getch();

  

    return 0;
// program to fill polygon using floodfill
// algorithm
#include <graphics.h>
#include <stdio.h>

  
// flood fill algorithm
void flood(int x, int y, int new_col, int old_col)
{

    // check current pixel is old_color or not

    if (getpixel(x, y) == old_col) {

  

        // put new pixel with new color

        putpixel(x, y, new_col);

  

        // recursive call for bottom pixel fill

        flood(x + 1, y, new_col, old_col);

  

        // recursive call for top pixel fill

        flood(x - 1, y, new_col, old_col);

  

        // recursive call for right pixel fill

        flood(x, y + 1, new_col, old_col);

  

        // recursive call for left pixel fill

        flood(x, y - 1, new_col, old_col);

    }
}

  

int main()
{

    int gd, gm = DETECT;


  

    // initialize graph

    initgraph(&gd, &gm, "");

  

    // rectangle coordinate

    int top, left, bottom, right;

  

    top = left = 50;

    bottom = right = 300;

  

    // rectangle for print rectangle

    rectangle(left, top, right, bottom);

  

    // filling start cordinate

    int x = 51;

    int y = 51;

  

    // new color to fill

    int newcolor = 12;

  

    // new color which you want to fill

    int oldcolor = 0;

  
    // call for fill rectangle

    flood(x, y, newcolor, oldcolor);

    getch();

  

    return 0;
#include <graphics.h>
#include <stdio.h>
void flood(int x, int y, int new_col, int old_col)
{
if (getpixel(x, y) == old_col) {
putpixel(x, y, new_col);
flood(x + 1, y, new_col, old_col);
flood(x - 1, y, new_col, old_col);
flood(x, y + 1, new_col, old_col);
flood(x, y - 1, new_col, old_col);
}
}
int main()
{
int gd, gm = DETECT;
initgraph(&gd, &gm, "");
int top, left, bottom, right;
top = left = 50;
bottom = right = 300;
rectangle(left, top, right, bottom);
int x = 51;
int y = 51;
int newcolor = 12;
int oldcolor = 0;

flood(x, y, newcolor, oldcolor);


getch();
return 0;
}
Output:

Conclusion:
Hence we have successfully studied and implemented Polygon Filling Algorithm.

You might also like