Saturday, March 4, 2017

C Program to explain moverel() and linerel()

Graphics in C - Program to Create a Hut Using moverel() and linerel()

Till now I have explained function line(), and function lineto(). These functions draw to a specific point in the viewport. Now I’ll explain function linerel(), which draws a line to a relative distance from the current position. Besides this, I’ll explain function moverel(), function getx(), function gety(), function  sprintf(). Please type this program in your editor.

#include<graphics.h>
#include<stdio.h>
void main()
{
          int gd = DETECT, gm, maxx, maxy, x, y;
          char str[100];
          initgraph(&gd, &gm, "c:\\tc\\bgi");
          maxx = getmaxx();
          maxy = getmaxy();
          outtextxy(120, 40, "Demo of moverel(), linerel(), getx(), gety()");
          setcolor(WHITE);
          moverel(maxx/2, maxy/4);
          x = getx();
          y = gety();
          sprintf(str, "(%d, %d)", x, y);
          outtextxy(x, y, str);
          setcolor(RED);
          linerel(100, 100);
          linerel(-200, 0);
          linerel(100, -100);
          setfillstyle(HATCH_FILL, BROWN);
          floodfill(maxx/2+5, maxy/4+10, RED);
          setcolor(WHITE);
          moverel(-80, 100);
          linerel(0, 100);
          linerel(60, 0);
          linerel(0, -50);
          linerel(50, 0);
          linerel(0, 50);
          linerel(50, 0);
          linerel(0, -100);
          getch();
          closegraph();
          restorecrtmode();
}

Compile and execute this program. The output would be a beautiful hut. In the topmost corner, coordinates of this point are being displayed.

First, I have obtained maximum width and height of the viewport using functions getmaxx() and getmaxy(), and stored the results in the variables ‘maxx’ and ‘maxy’. Then I have set the color of graphics ‘white’ by using the function setcolor(). Then I have used the function moverel(). Explanation of this function is given below.

Function moverel() moves the current position(C.P.) a relative distance as given in the function.

Declaration:

void far moverel(int dx, int dy);

where ‘dx’ is the distance in pixels in the x-direction, and ‘dy’ is the distance in pixels in the y direction from the current position(C.P.).

Syntax for calling this function is:

moverel(dx, dy);

Therefore, the statement:

moverel(maxx/2, maxy/4);

moves the current position(C.P.) maxx/2 pixels in the x-direction, and maxy/4 pixels in the y-direction. Initially the current position is (0, 0). If these values are given negative(-ve), distances will be in the opposite direction.

To find the coordinates of current position, I have used the functions getx(), and gety(). Function getx() returns the x-coordinate of the current position(C.P.), and function gety() returns the y-coordinate of the current position(C.P.). These values are viewport relative, dependent on your monitor’s viewport.

Declarations:

int far getx(void);
int far gety(void);

Syntax for calling these functions are:

getx();
gety();

Therefore, the statements:

x = getx();
y = gety();

get the x and y coordinates of current graphic position(C.P.), and store the results in the ‘x’ and ‘y’ variables.

Function sprintf() stores the values of x, and y in a string ‘str’ in the string format. Function sprintf() sends formatted output to a string.

Declaration:

int sprintf(char *buffer, const char *format[, argument, …]);

Therefore, the statement:

sprintf(str, "(%d, %d)", x, y);

formats the values of variables x, and y in the form ‘(x, y)’, and stores the formatted string in the string ‘str’. For using this function library file “stdio.h” is to be included.

Then, the statement:

outtextxy(x, y, str);

displays the string ‘str’ at the position (x, y). This way, I have shown the coordinates of topmost point of the hut.

Now, I have set the color of graphic ‘RED’ by the statement:

setcolor(RED);

Function linerel() draws a line from the current position(C.P.) to a point relative to the current position.

Declaration:

void far linerel(int dx, int dy);

where ‘dx’ is the relative distance in pixels in the x-direction from the current position, and ‘dy’ is the relative distance in pixels in y-direction from the current position. After drawing the line it moves the current position to this new point away by (dx, dy) from the old position. If any value is negative, line is drawn in opposite direction.

Syntax for calling this function is:

linerel(dx, dy);

Therefore, the statements:

linerel(100, 100);
linerel(-200, 0);
linerel(100, -100);

form the triangular top of hut. First statement draws the line from current position, which I have set (maxx/2, maxy/4) previously by the function moverel(), to 100 pixels in x-direction(right direction), and 100 pixels in y-direction(downwards). Then advances the current position to this new position. Second statement draws the line in the left direction of current position by 200 pixels and advances the current position to the new position(dy is 0). Same way, third line has been drawn, forming the triangular part. This triangular part has been flooded by fill pattern HATCH_FILL.

The remaining statements can be explained the same way.

For the explanation of remaining functions and terms, please refer my previous posts.

OUTPUT:







No comments:

Post a Comment

Please give your comment