Relative line function

linerel() in graphics.h

void far linerel(int dx, int dy);

The linerel() function draws a line from the current graphics cursor position using a relative x and y offset. Instead of giving the full destination point, you tell graphics.h how far to move horizontally and vertically from the current position. After drawing, the cursor moves to the new endpoint.

This is useful when your shape is naturally described by steps rather than by fixed absolute coordinates. Relative drawing can make repeated box edges, zigzags, staircases, and small symbol outlines easier to write and easier to update later.

Parameters

NameTypeDescription
dxintThe horizontal change from the current cursor position. Positive values move right, negative values move left.
dyintThe vertical change from the current cursor position. Positive values move downward, negative values move upward.

The key idea is that the numbers are offsets, not final screen coordinates. If the current position is (200, 150) and you call linerel(60, 0), the new endpoint becomes (260, 150).

Return Value

linerel() returns void. It changes two things: it draws a visible segment, and it updates the current graphics position to the new relative endpoint. That updated position becomes the base for the next linerel() or lineto() call.

Code Examples

Draw a box using relative edges

#include <graphics.h>
#include <conio.h>

int main() {
    int gd = DETECT, gm;
    initgraph(&gd, &gm, "");

    setcolor(WHITE);
    moveto(180, 140);
    linerel(180, 0);
    linerel(0, 120);
    linerel(-180, 0);
    linerel(0, -120);

    getch();
    closegraph();
    return 0;
}

This is a good beginner example because each side is described by how far it moves, not by recalculating every endpoint manually.

Simple staircase pattern

#include <graphics.h>
#include <conio.h>

int main() {
    int gd = DETECT, gm;
    initgraph(&gd, &gm, "");

    setcolor(LIGHTCYAN);
    moveto(100, 100);
    linerel(60, 0);
    linerel(0, 40);
    linerel(60, 0);
    linerel(0, 40);
    linerel(60, 0);
    linerel(0, 40);

    getch();
    closegraph();
    return 0;
}

The output is a staircase-like polyline. Relative drawing is very natural for patterns built from repeated steps.

Arrow shape from one anchor point

#include <graphics.h>
#include <conio.h>

int main() {
    int gd = DETECT, gm;
    initgraph(&gd, &gm, "");

    setcolor(YELLOW);
    moveto(180, 220);
    linerel(160, 0);
    linerel(-30, -25);
    moveto(340, 220);
    linerel(-30, 25);

    getch();
    closegraph();
    return 0;
}

This uses relative offsets to build a directional symbol from one base position. Small changes to the offsets reshape the arrow quickly.

Common Mistakes

Treating dx and dy like absolute coordinates

linerel(300, 200) does not mean "go to 300,200". It means "move 300 pixels right and 200 pixels down from wherever the current position already is."

Forgetting that negative values are useful

To draw leftward or upward, negative offsets are required. This is common when closing a box or returning along a shape edge.

Not keeping track of the updated cursor

Every relative line changes the current graphics position. If you lose track of that state, later segments can go in surprising directions.

Try it online

Run the box example and replace one positive offset with a negative one to see how quickly the path direction changes.