Home Docs
Graphics.h Documentation
Compiler

Drawing Basics

Line & Cursor Movement

graphics.h provides functions to draw straight lines and control the current drawing position (graphics cursor). These are core tools for building complex shapes step by step.

On This Page

Understanding the Coordinate System

  • Origin (0, 0) is top-left.
  • X increases left to right.
  • Y increases top to bottom.

For a 640 x 480 screen, bottom-right is approximately (639, 479).

line()

Syntax

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

Draws a straight line between two points.

line(100, 100, 300, 100);

This creates a horizontal line from left to right.

moveto()

Syntax

void moveto(int x, int y);

Moves current drawing position without drawing.

moveto(100, 200);

lineto()

Syntax

void lineto(int x, int y);

Draws from current position to a target point, then updates current position.

moveto(100, 200);
lineto(300, 200);

linerel()

Syntax

void linerel(int dx, int dy);

Draws relative to current position using offsets.

moveto(100, 200);
linerel(200, 0);

moverel()

Syntax

void moverel(int dx, int dy);

Moves current position relative to current point without drawing.

moveto(100, 200);
moverel(50, 50);

New position becomes (150, 250).

Example Program: Rectangle Using Movement

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

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

    moveto(100, 100);
    linerel(200, 0);
    linerel(0, 100);
    linerel(-200, 0);
    linerel(0, -100);

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

This draws a rectangle by repeatedly drawing relative lines from current position.

When to Use Each Function

Use line()

When both start and end points are known directly.

Use moveto() + lineto()

When building connected segments step by step.

Use linerel()

When drawing based on relative offsets and patterns.

Use moverel()

When repositioning cursor without drawing.

Common Mistakes

  • Forgetting to call initgraph() before drawing.
  • Ignoring that lineto() depends on current position.
  • Using negative offsets incorrectly in linerel().
  • Confusing moveto() (absolute) and moverel() (relative).

These five functions are the foundation for all drawing logic in graphics.h. Master cursor movement first, then continue to shapes and fills.

Next: Basic Shapes, Polygons & Fill, and Advanced drawing techniques.