Parameters
| Name | Type | Description |
|---|---|---|
dx | int | The horizontal change from the current cursor position. Positive values move right, negative values move left. |
dy | int | The 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.