Graphics in C - Demonstration of Functions moveto() and lineto()
In the previous post, lines
have been drawn using function line(). The coordinates passed
to this function were with respect to the origin (0, 0), which was the top-left corner of the screen. While drawing
the line by using function line(), C.P.(Current Position) does not change. So far we have used the functions
that do not change the C.P. Coordinates passed to these functions are always
with respect to the point, named origin
(0, 0). However, there are a few functions, in which C.P. changes, and next graphic starts from C.P., not from origin. Function lineto()
draws a line from C.P. to the
specified location on the screen and moves the C.P. to its end point. Function moveto()
moves the C.P. to the specified
point. To clear the point, please type the program given
below in your editor. This program demonstrates functions moveto()
and lineto().
#include<graphics.h>
void main()
{
int gd = DETECT, gm, maxx, maxy;
initgraph(&gd, &gm, "c:\\tc\\bgi");
/* Sets the color of lines */
setcolor(WHITE);
/* Get the maximum screen coordinates */
maxx = getmaxx();
maxy = getmaxy();
/* Displays the message on screen */
outtextxy(maxx/2-150, maxy/4-70, "Demonstration of moveto() and lineto()");
/* Moves the C.P. to specified point */
moveto(maxx/2, maxy/4-35);
/* Draw the lines from C.P. to the specified
point and move the C.P. to that
specified
point */
lineto(maxx/4, maxy*3/4);
lineto(maxx*3/4, maxy*3/4);
lineto(maxx/2, maxy/4-35);
moveto(maxx/4, maxy/4+50);
lineto(maxx*3/4, maxy/4+50);
lineto(maxx/2, maxy*3/4+85);
lineto(maxx/4, maxy/4+50);
/* Sets the fill-style and color */
setfillstyle(SOLID_FILL, RED);
/* Fills the enclosed area with current
fill-style and color */
floodfill(maxx/2+1, maxy/4-30, WHITE);
setfillstyle(LINE_FILL, BLUE);
floodfill(maxx/4+4, maxy*3/4-2, WHITE);
setfillstyle(SLASH_FILL, GREEN);
floodfill(maxx*3/4-2, maxy*3/4-1, WHITE);
setfillstyle(HATCH_FILL, MAGENTA);
floodfill(maxx/4+3, maxy/4+53, WHITE);
setfillstyle(INTERLEAVE_FILL, YELLOW);
floodfill(maxx*3/4-2, maxy/4+51, WHITE);
setfillstyle(WIDE_DOT_FILL, CYAN);
floodfill(maxx/2, maxy*3/4+75, WHITE);
setfillstyle(XHATCH_FILL, LIGHTBLUE);
floodfill(maxx/2, maxy/2, WHITE);
getch();
closegraph();
restorecrtmode();
}
Compile and execute this
program. The output of this program will be a star filled with different
patterns in different colors in different bounding regions.
To draw the star, I have
used functions moveto() and lineto()
instead of function line() or function drawpoly().
Function moveto() moves the current position(C.P.) to a desired position (x, y).
Declaration:
void far moveto(int x, int y);
Where (x, y) is the coordinates of the desired location of the view port.
Syntax for calling this
function is given below.
moveto(x, y);
Function lineto() draws a line from the current position(C.P.) to the point (x, y). After drawing the line it moves the current position to the
location (x, y).
Declaration:
void far lineto(int x, int y);
Where (x, y) is the coordinates of the point, to which line has to be
drawn.
Syntax for calling this
function is given below.
lineto(x, y);
In the given program, I have
moved the current position(C.P.) to
the point (maxx/2, maxy/4-35) by the
statement given below.
moveto(maxx/2, maxy/4-35);
Then I have drawn three
lines by the statements given below, and formed a triangle.
lineto(maxx/4,
maxy*3/4);
lineto(maxx*3/4, maxy*3/4);
lineto(maxx/2, maxy/4-35);
The same way, I have drawn
another triangle by the following statements.
moveto(maxx/4, maxy/4+50);
lineto(maxx*3/4, maxy/4+50);
lineto(maxx/2, maxy*3/4+85);
lineto(maxx/4, maxy/4+50);
And finally a star has been
formed by combining these two triangles. As a result, seven bounded
regions are formed, and then I have filled them by using functions setfillstyle() and floodfill().
Now, the explanation of
function moveto() and function lineto() is complete. For the explanation of remaining
functions and terms, please refer my previous posts.
OUTPUT:
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
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