Home Docs
Graphics.h Documentation
Compiler

Basic Shapes

line() in graphics.h

The line() function draws a straight line between two points. It is one of the most fundamental drawing functions in graphics.h.

On This Page

Syntax

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

Draws a line from (x1, y1) to (x2, y2).

Parameters

  • x1, y1: starting point
  • x2, y2: ending point

Coordinates are absolute screen coordinates (not relative movement).

Because line() uses absolute points, it is ideal when you already know exact coordinates for edges, axes, or static layout boundaries.

Simple Example

line(100, 100, 300, 100);

This draws a horizontal line 200 pixels long.

Full Program

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

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

    line(100, 100, 300, 100);
    line(100, 100, 100, 250);
    line(100, 250, 300, 250);
    line(300, 100, 300, 250);

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

Click Run (DOS) to quickly test this program.

Common Mistakes

  • Calling line() before initgraph().
  • Using coordinates outside visible screen bounds.
  • Forgetting closegraph() before program exit.

Next: continue with circle() and rectangle().