Mortie
Mortie

Reputation: 316

How can I draw line diagrams on a tty?

I'm writing a command line (non-graphical) Linux program in C++, and in some places I need it to draw a chart or plot a function right into the terminal.

Looking at some programs like ntsysv, mc, alsa etc., I believe it's possible but I was wondering how.

Upvotes: 3

Views: 11824

Answers (5)

user3748950
user3748950

Reputation: 19

Probably that's not needed anymore but maybe that's helping somebody

void drawfunc(int ymin, int ymax, int xmin, int xmax, float h){
int y = 0;
int x = 0;
double func;

for(y = ymax; y >= ymin; y--)
{
    printf("\n");
    for(x = xmin; x <= xmax; x++)
    {
        func = cos(x);
        
        if(x == 0)
        {
            printf("|");
        }
        
        if(func > (y - 1) * h && func < y * h)
        {
            printf("*");
        }

        else if(y == 0)
        {
            if(x >= xmin || x <= xmax)
            {
                printf("-");
            }
        }
        else
        {
            printf(" ");
        }
    }
}
printf("\n\n");}

Upvotes: 0

Debbie
Debbie

Reputation: 17

try with the old borland library for turbo c conio.h

Upvotes: 0

Tom A
Tom A

Reputation: 964

Seconding the ncurses recommendation, there also exists a library called libcaca, a graphics library that outputs ASCII text instead of pixels. If you want to create this graphs with other software and then push their output to the terminal you could consider using libcaca with ncurses.

Upvotes: 5

austin1howard
austin1howard

Reputation: 4955

Things like alsamixer use ncurses. http://www.gnu.org/software/ncurses/

This will let you make diagrams and whatnot directly in the terminal.

Upvotes: 2

You probably want to use ncurses and to do some ASCII art.

Upvotes: 4

Related Questions