Boundary Fill Algorithm in C and C++
The Boundary Fill algorithm is used in computer graphics to fill a closed area with a specified color. It starts from a seed point and fills neighboring points until a boundary color is encountered. The algorithm is recursive in nature.
Boundary Fill and Flood Fill algorithm
The Boundary Fill and Flood Fill algorithm share several similarities:
- Objective: Both algorithms are used in computer graphics to fill enclosed areas with designated colors.
- Starting Point: They both start from a seed point within the region to be filled.
- Coloring Process: Both algorithms iteratively change pixel colors based on specified conditions.
- Neighbor Exploration: They examine neighboring pixels to determine whether they should be filled or stopped.
- Recursive Approach: Both algorithms often employ a recursive approach to propagate color changes.
- Boundary Constraint: Both algorithms stop when encountering a boundary color, preventing them from overflowing into neighboring regions.
- Connected Regions: They effectively handle areas with gaps or inner boundaries by treating the enclosed region as a single entity.
- Used in Graphics: Both algorithms play crucial roles in image editing, area coloring, and graphics rendering tasks.
While they serve similar purposes, the Boundary Fill algorithm focuses on filling a region up to its boundary, while the Flood Fill algorithm aims to fill connected regions until encountering a boundary color.
Boundary Fill Algorithm Program in C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #include<stdio.h> #include<graphics.h> #include<conio.h> void boundaryfill(int x,int y,int fillcolor,int boundrycolor) { if(getpixel(x,y)!=boundrycolor && getpixel(x,y)!=fillcolor) { putpixel(x,y,fillcolor); boundaryfill(x+1,y,fillcolor,boundrycolor); boundaryfill(x,y+1,fillcolor,boundrycolor); boundaryfill(x-1,y,fillcolor,boundrycolor); boundaryfill(x,y-1,fillcolor,boundrycolor); } } int main() { int gm,gd=DETECT,radius=30; int x=100,y=100; initgraph(&gd,&gm,"c:\\turboc3\\bgi"); circle(x,y,radius); boundaryfill(x,y,6,15); getch(); closegraph(); return 0; } |
Program Explanation
• The program uses the graphics library to create a graphics window.
• initgraph(&gd, &gm, “C:\\Turboc3\\BGI”); initializes the graphics mode.
• circle(x,y,radius); draws a circle on the screen.
• boundaryFill(x, y, 6, 15); initiates the boundary fill algorithm at the point (100, 100) with fill color 6 and boundary color 15.
• The boundaryFill function is a recursive function:
• It checks if the current pixel is not the boundary color or the fill color.
• If the condition is met, it changes the pixel color to the fill color and then recursively calls itself on neighboring pixels.
• getch(); waits for a key press to exit the program.
• closegraph(); closes the graphics mode.
Output
Boundary Fill Algorithm Program in C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #include<graphics.h> #include<conio.h> #include<iostream.h> void boundaryfill(int x,int y,int fillcolor,int boundrycolor) { if(getpixel(x,y)!=boundrycolor && getpixel(x,y)!=fillcolor) { putpixel(x,y,fillcolor); boundaryfill(x+1,y,fillcolor,boundrycolor); boundaryfill(x,y+1,fillcolor,boundrycolor); boundaryfill(x-1,y,fillcolor,boundrycolor); boundaryfill(x,y-1,fillcolor,boundrycolor); } } int main() { int gm,gd=DETECT,radius=30; int x=100,y=100; initgraph(&gd,&gm,"c:\\turboc3\\bgi"); circle(x,y,radius); boundaryfill(x,y,6,15); getch(); closegraph(); return 0; } |