Drawing primitive

line() in graphics.h

line(int x1, int y1, int x2, int y2);

The line() function draws a straight line segment between two absolute screen coordinates. It is the basic building block behind grids, axes, polygons, diagrams, charts, houses, stars, and many Computer Graphics lab drawings. You give the start point and the end point, and graphics.h draws the visible pixels between them using the current color, line style, and thickness.

This function is different from lineto(). line() uses two complete points and does not depend on the current drawing position. That makes it predictable for beginner programs because each call contains all the information needed to draw one segment. If you are implementing DDA or Bresenham manually, you normally use putpixel(). If you simply need a built-in straight segment, line() is the right function.

Parameters

NameTypeDescription
x1intThe x-coordinate of the starting point.
y1intThe y-coordinate of the starting point.
x2intThe x-coordinate of the ending point.
y2intThe y-coordinate of the ending point.

The endpoints use the active graphics coordinate system. In a typical 640 by 480 graphics mode, (0, 0) is the top-left corner, (639, 0) is near the top-right corner, and (0, 479) is near the bottom-left corner. The line may be horizontal, vertical, diagonal, or slanted at any angle. If an endpoint is outside the screen, graphics.h clips the visible part.

Return Value

line() returns void. It does not return the slope, length, or clipping result. If you need mathematical values, calculate them yourself before calling the function. For example, horizontal distance is x2 - x1, vertical distance is y2 - y1, and the visual length can be estimated with the distance formula.

Code Examples

Draw two diagonals

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

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

    setcolor(WHITE);
    line(0, 0, 639, 479);
    line(639, 0, 0, 479);

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

The output is an X shape across the drawing area. It demonstrates that a line can travel in either direction; the function is not limited to left-to-right drawing.

Coordinate axes

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

int main() {
    int gd = DETECT, gm;
    int cx = 320, cy = 240;
    initgraph(&gd, &gm, "");

    setcolor(LIGHTGRAY);
    line(40, cy, 600, cy);
    line(cx, 40, cx, 440);

    setcolor(YELLOW);
    outtextxy(604, cy - 8, "X");
    outtextxy(cx + 8, 44, "Y");

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

This example draws horizontal and vertical axes through the screen center. It is useful for transformation assignments because objects can be positioned relative to the center intersection.

House outline with line()

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

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

    setcolor(CYAN);
    rectangle(220, 220, 420, 380);
    line(220, 220, 320, 120);
    line(420, 220, 320, 120);
    line(260, 380, 260, 300);
    line(260, 300, 320, 300);
    line(320, 300, 320, 380);

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

The output is a simple house: a rectangular wall, a triangular roof, and a door. This shows how multiple line segments can create larger objects.

Common Mistakes

Thinking y increases upward

In mathematics, y often increases upward. In graphics.h screen coordinates, y increases downward. A point with y = 400 appears lower than a point with y = 100.

Expecting line() to update the current position

line() does not update the current graphics cursor. Use lineto() if your drawing should continue from the current position.

Forgetting setcolor()

The line uses the current drawing color. If the current color matches the background, the line may be technically drawn but invisible.

Using line() inside manual algorithm assignments

If the assignment asks for DDA or Bresenham implementation, calling line() skips the algorithm. Use putpixel() and calculate intermediate points manually.

Try it online

Run the examples in the online compiler and adjust the endpoint coordinates. Small coordinate changes make the screen coordinate system much easier to remember.