Tutorialspoint.dev

linerel() function in C

The header file graphics.h contains linerel() function which draws a line from the current position (x_pos1, y_pos1) to a point that is at a relative distance (x, y) from the Current Position, then advances the Current Position by (x, y).
Note : Use getx() and gety() to find the current position.

Syntax :

linerel(int x, int y);

The end position is calculated as :
x_pos2 = x_pos1 + x;
y_pos2 = y_pos1 + y;

Examples :

Input : x_pos1 = 0, y_pos1 =0, x = 200, y = 100
Output : 

Input : x_pos1 = 100, y_pos1 = 150, x = 150, y = 60
Output : 

CASE 1 : When the current position is at origin i.e, (0,0)
Below is the implementation of linerel() function in C:

// C Implementation for linerel()
#include <graphics.h>
#include<stdio.h>
  
// driver code
int main()
{
    // gm is Graphics mode which is
    // a computer display mode that
    // generates image using pixels.
    // DETECT is a macro defined in
    // "graphics.h" header file
    int gd = DETECT, gm;
    char arr[100];
  
    // initgraph initializes the
    // graphics system by loading a
    // graphics driver from disk
    initgraph(&gd, &gm, "");
  
    // linerel fuction
    linerel(200, 100);
  
    // sprintf stands for “String print”.
    // Instead of printing on console,
    // it store output on char buffer
    // which are specified in sprintf
    sprintf(arr, "Current x position = %d and "
             "y position = %d", getx(), gety());
  
    // outtext function displays
    // text at current position.
    outtextxy(100, 150, arr);
  
    getch();
  
    // closegraph function closes the
    // graphics mode and deallocates
    // all memory allocated by
    // graphics system .
    closegraph();
  
    return 0;
}

Output :


CASE 2 : When the current position is other than origin, (100, 150).
Below is the implementation of linerel() function in C:

// C Implementation for linerel()
#include <graphics.h>
#include<stdio.h>
  
// driver code
int main()
{
    // gm is Graphics mode which is
    // a computer display mode that
    // generates image using pixels.
    // DETECT is a macro defined in
    // "graphics.h" header file
    int gd = DETECT, gm;
    char arr[100];
  
    // initgraph initializes the
    // graphics system by loading a
    // graphics driver from disk
    initgraph(&gd, &gm, "");
  
    // Change current position using
    // moveto function
    moveto(100, 150);
  
    // linerel fuction
    linerel(150, 60);
  
    // sprintf stands for “String print”.
    // Instead of printing on console,
    // it store output on char buffer
    // which are specified in sprintf
    sprintf(arr, "Current x position = %d and "
             "y position = %d", getx(), gety());
  
    // outtext function displays
    // text at current position.
    outtextxy(100, 150, arr);
  
    getch();
  
    // closegraph function closes the
    // graphics mode and deallocates
    // all memory allocated by
    // graphics system .
    closegraph();
  
    return 0;
}

Output :




This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter