Home Docs
Graphics.h Documentation
Compiler

Advanced

Advanced Functions in graphics.h

This page gives simple explanations of commonly used advanced graphics.h functions with small snippets and one full program at the end.

On This Page

setviewport()

Limits drawing to a rectangular area of the screen.

setviewport(50, 50, 300, 220, 1);

cleardevice()

Clears the active drawing area (full screen or current viewport).

cleardevice();

delay()

Pauses execution for given milliseconds. Useful for simple animation timing.

delay(200);  // wait 200 ms

moveto() / moverel()

Moves the current graphics cursor for relative line drawing.

moveto(100, 100);
linerel(80, 0);
moverel(0, 40);
linerel(-80, 0);

Full Program

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

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

    setviewport(80, 60, 540, 360, 1);
    rectangle(0, 0, getmaxx(), getmaxy());

    for (i = 0; i < 5; i++) {
        cleardevice();
        rectangle(0, 0, getmaxx(), getmaxy());
        setcolor(YELLOW);
        circle(60 + i * 70, 120, 30);
        delay(250);
    }

    setcolor(WHITE);
    moveto(80, 220);
    linerel(120, 0);
    moverel(0, 30);
    linerel(-120, 0);

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

Click Run (DOS) to test this program.