Graphics in C - Demonstration of User-defined fill-pattern
In C programming language we can give fill-patterns by two methods.
One is standard system defined, and other is user-defined. I have described
function setfillstyle() previously. Function setfillstyle() fills the standard predefined fill patterns.
If you want to give user-defined fill patterns, you can use function setfillpattern(). Here is a
simple program given below. Please type this program in your editor.
#include<graphics.h>
void main()
{
/* Declares and initializes array with
user-defined bit-pattern */
char patt[] = {0x0F, 0xF0,
0x0F, 0xF0, 0x0F, 0xF0, 0x0F, 0xF0};
int gd = DETECT, gm;
initgraph(&gd, &gm,
"c:\\tc\\bgi");
/* Sets the user-defined fill-pattern and color */
setfillpattern(patt, RED);
/* Draws a rectangle */
rectangle(100, 100, 400, 400);
/* Fills the rectangle with user-defined
fill-pattern and color */
floodfill(101, 101, WHITE);
getch();
closegraph();
restorecrtmode();
}
Compile and execute this
program. The output will be a rectangle filled with nice checker red pattern.
To create user-defined pattern, I have used the function setfillpattern().
Declaration:
void far setfillpattern(char far
*upattern, int color);
Where ‘upattern’ is a pointer to a sequence of ‘8’ bytes, each byte consists of 8 pixels. You can store this 8 byte
sequence of pattern in an array. In every byte, if a bit is set to ‘1’, a pixel will be drawn, and if a bit
is set to ‘0’, pixel will not be
drawn. ‘colour’ parameter specifies
the colour of user-defined pattern.
Syntax for calling this
function is given below-
setfillpattern(upattern, color);
In the given program, an
array ‘patt[8]’ has been declared
and initialized with 2 hexadecimal numbers ‘0x0F’ and ‘0xF0’ in
alternating fashion (0x0Fà0000 1111 and 0xF0à 1111 0000). Number of array elements is 8, which denotes to 8 bytes. Therefore, wherever a bit is ‘1’, a pixel will be drawn in given
colour, which is ‘RED’ in the given
program. Therefore, a pattern of bits will be like this-
0000 1111
1111 0000
0000 1111
1111 0000
0000 1111
1111 0000
0000 1111
1111 0000
The statement given below will
set a checker like pattern in red color. ‘patt’ is the address of array ‘patt[8]’.
setfillpattern(patt, RED);
Since, function rectangle() is not automatically filled with given fill
pattern, I have used the function floodfill().
floodfill(101, 101, WHITE);
This is the description of
using user-defined fill patterns. For the description of other functions, and
constants, please refer my previous posts.
You would also like these programs given below:
No comments:
Post a Comment
Please give your comment