How to use setfillstyle() and floodfill() in C Language

setfillstyle() and floodfill() Function in C language

Introduction :

C or C++ graphics programming language allows you to create visual elements and shapes that brings visual creativity to life on the screen. The graphics.h library of Turbo C++ IDE provides functions like setfillstyle() and floodfil() which allow you to set fill patterns or style and fill colors of a closed area shape.

To bring your designs to life through colors and patterns, setfillstyle() and floodfill() are very important functions in C or C++ programming language. The setfillstyle() and floodfill() functions are generally used to draw and fill different closed shapes in graphics mode. In this article, I shall explore you how to use setfillstyle() and floodfill() functions using C or C++ graphics programming language.

What are setfillstyle() and floodfill() functions :

The setfillstyle() and floodfill() are functions used in graphics programming, particularly in languages like C and C++ with graphics.h library. The setfillstyle() and floodfill() functions are commonly used for simple 2D graphics in C or C++ programming language. These functions are commonly used to manipulate and fill shapes with pattern and color in a graphical environment. The setfillstyle() and floodfill() functions are not part of the standard C or C++ library. These functions are specific to certain compilers like Turbo C++.

setfillstyle() function:

The setfillstyle() function is used to set the fill pattern or style and color for shapes that you want to draw on your graphics window or canvas. The setfillstyle() function allows you to specify how a shape will be filled with patterns and colors.

syntax of setfillstyle() function :

The following code is the syntax of setfillstyle() function.

void setfillstyle(int fill_pattern, int fill_color);

The setfillstyle() function takes two int type arguments such as “fill_pattern” and “fill_color”. The first argument or parameter is an integer that represents the type of fill pattern or style you want to use. The fill pattern determines how the pattern or style of a shape should be filled which can be predefined constants like SOLID_FILL, LINE_FILL, HATCH_FILL, SLASH_FILL, BKSLASH_FILL, INTERLEAVE_FILL and more.

Another argument or parameter is also an integer which represents the fill color of the shape. The fill color determines which color will fill the shape which you have drawn. You can use predefined color constants like WHITE, RED, BLUE, GREEN etc. as you like. After calling setfillstyle(), any subsequent shapes you have drawn on the canvas will be filled according to the specified pattern or style and color until you change the fill style again.

floodfill() function :

The floodfill () function used to fill a closed area or shape within a graphics window with a specified fill style or pattern and color you have set using setfillstyle(). For use floodfill() function, You need to specify the seed point (a point within the closed area or shape) and the boundary color. After that, floodfill() function determines the boundaries of the shape and filling shapes with the specified pattern and color. The floodfill() is particularly useful for filling closed shapes like rectangle, circle, etc.

syntax of floodfill() function :

The following code is the syntax of floodfill() function.

void floodfill(int x_coordinate, int y_coordinate, int boundary_color);

The floodfill() function takes three int type arguments such as “x_coordinate”, “y_coordinate” and “boundary_color”. The x and y coordinate represent the starting point inside the closed shape from where the flood filling will begin. The boundary color is an integer specifying the boundary color of the closed shape. The filling will stop when it encounters the boundary color.

Note that, before using floodfill(), you need to draw a closed shape on the graphics window using graphics functions like rectangle(), circle(), line() etc.

About the program :

In this program, you see circles with different fill styles and colors one by one. For each fill style (from 0 to 11), it displays each circle with different fill style and color. When you press any key, you can see the next circle with different fill pattern or style and color.

Explanation of the program :

First, you have to include necessary header files in the program. The graphics.h library use for graphics functions for and conio.h library use for getch() functions. In the main() function, declare require int variables such as “mid_x”, “mid_y”, “fill_style” and “fill_color”. You have to also declare the “graphic_driver” and “graphic_mode” in the program.

To use graphics functions, you can initialize the graphics mode using the initgraph() function. Using getmaxx() and getmaxy() functions, calculate the coordinates of the center of the graphics window and store in “mid_x” and “mid_y” variables. In the for loop, call the setfillstyle(), circle() and floodfill() functions. The setfillstyle() is used to set the current fill style and fill color for the shapes. It takes the fill_style and fill_color variables as arguments.

The circle() function is used to draw a circle at the center of the screen with a radius of 200 pixels. Then, floodfill() fills the circle with the current fill pattern or style and color. The floodfill() starts filling from the point (mid_x, mid_y) and stops when it encounters the color WHITE as the boundary. Next, you have to increment the “fill_color” variable and use getch() for see the next circle with different fill pattern or style and color. After completing the for loop, close the graphics window using closegraph() function.

How run the program :

First, you have to install the Turbo C++ IDE on your pc. Then open the Turbo C++ IDE and create a C or C++ file with .c or .cpp extension. Now, copy the below code and paste in your C or C++ file of the Turbo C++ IDE. If you do not know how to copy paste in the Turbo C++ IDE, follow the link. You can also see how to use graphics.h in Turbo C++ IDE in my other post. Now run the program on your pc to see how use setfillstyle() and floodfill() functions using C or C++ programming language.

Code for setfillstyle() and floodfill() functions :

The following code is used for setfillstyle() and floodfill() functions in C or C++ graphics programming language.

/*Developed by Puskar Jasu*/
#include <graphics.h>
#include <conio.h>
int main()
{
    int mid_x, mid_y, fill_style, fill_color = 0;
    int graphic_driver = DETECT, graphic_mode;
    initgraph(&graphic_driver, &graphic_mode, "//turboc3/bgi");
    mid_x = getmaxx() / 2;
    mid_y = getmaxy() / 2;
    for (fill_style = 0; fill_style < 12; fill_style++)
    {
        setfillstyle(fill_style, fill_color);
        circle(mid_x, mid_y, 200);
        floodfill(mid_x, mid_y, WHITE);
        fill_color++;
        getch();
    }
    closegraph();
    return 0;
}

Output :

When you run the above program on your pc, you can see the below image as output.

output of setfillstyle() and floodfill() function in c language

Conclusion :

At last, you have learned how to use setfillstyle() and floodfill() functions of graphics.h library by C or C++ programming language. This is a basic program of how to use setfillstyle() and floodfill() in C or C++ programming language to fill and color shapes in a graphics window. You can combine these functions with other graphics functions to create more complex graphics applications. Thank you for visiting my site.

Scroll to Top