Graphics in C - Predefined and User-defined Line Styles
In the previous posts, we
have drawn only solid lines.
Sometimes we may want to draw lines in other styles. In C programming language, different types of lines can be drawn. Besides
solid lines, you can draw dotted lines,
center lines, dashed lines, and user-defined
lines. I have described these line styles in this post. Please type this
program in your editor.
#include<graphics.h>
void main()
{
/* Declares and initializes array of pointers
to string */
char *style[5]={
"SOLID_LINE style(value = 0)",
"DOTTED_LINE style(value = 1)",
"CENTER_LINE style(value = 2)",
"DASHED_LINE style(value = 3)",
"USERBIT_LINE style(value = 4)"
};
int i, x1=100, y1=125, x2=250, y2=100;
int upattern = 0xAA; /* bit pattern: 1010 1010 */
int gd = DETECT, gm;
initgraph(&gd, &gm, "c:\\tc\\bgi");
/* Displays the message */
outtextxy(200, 50, "Line Styles");
for(i=0; i<=4; i++)
{
if(i==4)
{
setlinestyle(i,
upattern, 1);
}
else
{
setlinestyle(i,
0, 1);
}
line(x1,
y1, x1+100, y1);
rectangle(x2,
y2, x2+100, y2+50);
outtextxy(x2+150,
y2+25, style[i]);
y1+=60;
y2+=60;
}
getch();
closegraph();
restorecrtmode();
}
Compile and execute this
program. In the output, you will see one set of five lines and another set of
five rectangles in different line styles. For different line styles, I have
used the function setlinestyle(). Function setlinestyle() sets the current line style, line pattern,
and line width. Line pattern is the
user-defined pattern. This function sets the style for all lines drawn by
functions line(), rectangle(), drawpoly(), etc.
Declaration:
void far setlinestyle(int linestyle,
unsigned upattern, int thickness);
Syntax for calling this
function is given below-
setlinestyle(linestyle, upattern,
thickness);
Where’ linestyle’ is either system-defined or user-defined, depending on
the value of ‘linestyle’. Its values
can be an integer from 0 to 4. You can give the name of constants instead of
integer values. The names of constants and their corresponding integer values
are given below-
Name of constant Integer value
SOLID_LINE 0
DOTTED_LINE 1
CENTER_LINE 2
DASHED_LINE 3
USERBIT_LINE 4
Value
‘4’ is for user-defined line style.
‘upattern’ is user-defined pattern,
which is given, when you give value ‘4’
for line-style, otherwise it is set to ‘0’. ‘thickness’
is the width of line, and its value can be given from 1 to 3. If you give the
invalid values to this function, the current line style remains unchanged.
In the above program, I have
used a ‘for’ loop, which iterates through loop
variable i = 0 to 4. In the ‘for’ loop, an ‘if’ construct selects a ‘setlinestyle’
statement on the basis of the value of ‘i’.
If the value of ‘i’ is 4, then line-style is USERBIT_LINE(means user-defined line-style),
the statement setlinestyle(i, upattern, 1); will be executed. ‘upattern’ has been given the hexadecimal value ‘0xAA’ (bit pattern 1010 1010), and
thickness of line is ‘1’. If the
value of ‘i’ is from 0 to 3, statement setlinestyle(i, 0, 1);
will be executed. This time, value of ‘upattern’
has been set to ‘0’, and ‘thickness’ of line is again ‘1’. On the basis of settings of
function ‘setlinestyle(), lines and rectangles
have been drawn. Function outtextxy() has been used to
describe the line-styles.
Thus,
this simple program describes the use of function setlinestyle().
For the remaining functions and other things, please refer my previous posts.
You would also like these programs given below:
C Program to draw a line
C Program to draw a circle and a point
C Program to draw different types of ellipses
C Program to draw arc, pie-slice, and rectangle
C Program to draw a polygon
C Program to draw filled polygon, 2-D bar, and 3-D bar
C Program to create the user-defined fill-pattern
C Program to explain different line-styles
C Progarm to explain functions moveto() and lineto()
C Program to explain moverel() and linerel()
C Program to draw an elliptical pie-slice using sector()
C Program to write text in different font styles using settextstyle()
C Program to justify text using settextjustify()
C Program to explain function setusercharsize()
C Program to define palette of colors
C Program to generate captcha code
C Program for moving a ball on screen
C Program for creating animated circles
Playing with mouse using C language
C Program to restrict mouse pointer in a window
C Program for getting the position of mouse pointer
C Program to create a clock
Interrupt Handling With C
No comments:
Post a Comment
Please give your comment