
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Flood fill algorithm using C graphics
Concept
With respect of a given rectangle, our task is to fill this rectangle applying flood fill algorithm.
Input
rectangle(left = 50, top = 50, right= 100, bottom = 100) floodFill( a = 55, b = 55, NewColor = 12, OldColor = 0)
Output
Method
// A recursive function to replace previous color 'OldColor' at '(a, b)' and all surrounding pixels of (a, b) with new color 'NewColor' and floodFill(a, b, NewColor, OldColor)
If a or b is outside the screen, thenreturn.
If color of getpixel(a, b) is same asOldColor, then
Recur for top, bottom, right and left.
floodFill(a+1, b, NewColor, OldColor);<
floodFill(a-1, b, NewColor, OldColor);
floodFill(a, b+1, NewColor, OldColor);
floodFill(a, b-1, NewColor, OldColor);
Example
// Shows program to fill polygon using floodfill // algorithm #include <graphics.h> #include <stdio.h> // Describes flood fill algorithm void flood(int x1, int y1, int new_col, int old_col){ // Checking current pixel is old_color or not if (getpixel(x1, y1) == old_col) { // Putting new pixel with new color putpixel(x1, y1, new_col); // Shows recursive call for bottom pixel fill flood(x1 + 1, y1, new_col, old_col); //Shows recursive call for top pixel fill flood(x1 - 1, y1, new_col, old_col); // Shows recursive call for right pixel fill flood(x1, y1 + 1, new_col, old_col); // Shows recursive call for left pixel fill flood(x1, y1 - 1, new_col, old_col); } } int main(){ int gd1, gm1 = DETECT; // Initializing graph initgraph(&gd1, &gm1, ""); //Shows rectangle coordinate int top1, left1, bottom1, right1; top1 = left1 = 50; bottom1 = right1 = 300; // Shows rectangle for print rectangle rectangle(left1, top1, right1, bottom1); // Fills start cordinate int x1 = 51; int y1 = 51; // Shows new color to fill int newcolor = 12; // Shows old color which you want to replace int oldcolor = 0; // Calling for fill rectangle flood(x1, y1, newcolor, oldcolor); getch(); return 0; }
Output
- Related Articles
- Flood fill Algorithm
- Difference Between Flood-fill and Boundary-fill Algorithm
- Flood fill Algorithm – how to implement fill() in paint in C++
- Point Clipping Algorithm in Computer Graphics in C++
- Flood fill to specific color in PHP using imagefilltoborder() (GD) function.
- Turtle graphics using Python
- bar() function in C graphics
- How to create SVG graphics using JavaScript?
- Draw a line in C++ graphics
- Reversal Algorithm for Array Rotation using C++
- What is a Ping Flood Attack or ICMP Flood Attack?
- C++ Program to Implement Dijkstra’s Algorithm Using Set
- C++ Program to Construct Transitive Closure Using Warshall’s Algorithm
- Convex Hull using Divide and Conquer Algorithm in C++
- What is an IP Flood?

Advertisements