Connected line function

lineto() in graphics.h

void far lineto(int x, int y);

The lineto() function draws a line from the current graphics cursor position to a new absolute point. After drawing that segment, the current position moves to the new endpoint. This makes lineto() useful for connected path drawing where each new segment should continue from the previous one.

It is different from line(), which takes two full endpoints every time. With lineto(), the start point is implicit and comes from the current cursor state. In practice, you often call moveto() first, then one or more lineto() calls to build a polyline or outline.

Parameters

NameTypeDescription
xintThe x-coordinate of the endpoint.
yintThe y-coordinate of the endpoint.

The starting point is not passed as an argument because it comes from the current graphics position. That position may have been created by moveto(), by a previous lineto(), or by another cursor-aware function. This is why lineto() behaves like a path continuation tool.

Return Value

lineto() returns void. It does not return the old position, the new position, or the line length. Its visible effect is the drawn segment, and its internal effect is updating the current cursor point to the endpoint you supplied.

Code Examples

Single segment from a chosen start point

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

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

    setcolor(WHITE);
    moveto(140, 160);
    lineto(420, 160);

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

The line begins at the point set by moveto() and ends at the coordinates passed to lineto().

Draw a zigzag path

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

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

    setcolor(LIGHTCYAN);
    moveto(100, 120);
    lineto(180, 200);
    lineto(260, 120);
    lineto(340, 200);
    lineto(420, 120);

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

Each segment starts exactly where the previous one ended. That is the key advantage of cursor-based connected drawing.

Outline a rectangle with path drawing

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

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

    setcolor(YELLOW);
    moveto(180, 140);
    lineto(420, 140);
    lineto(420, 300);
    lineto(180, 300);
    lineto(180, 140);

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

This example shows how a closed outline can be created by continuing from one edge to the next without repeatedly writing the starting point for every segment.

Common Mistakes

Forgetting to set the starting position first

If you do not know where the current cursor already is, the line may start somewhere unexpected. A clear moveto() call often makes the code easier to reason about.

Confusing absolute coordinates with relative offsets

lineto() expects a full endpoint. If you want to draw by offset, use linerel() instead.

Expecting it to behave like line()

line() works well when every segment should be independent. lineto() is better when you want continuity from one segment to the next.

Try it online

Run the examples and replace one of the lineto() calls with line() to feel the difference between cursor-based and independent segments.