Thread: Help with a C program (linux) to do graphics using characters

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    11

    Exclamation Help with a C program (linux) to do graphics using characters

    Hi everyone i'm not good talking english so, if you don't understand something tell me.

    Well, i have to make a program in C (Linux) that make graphics using the linux terminal.. Like this.

    Help with a C program (linux) to do graphics using characters-2ronf3q-png

    I don't know how to draw that in the linux terminal... And the functions like x*x The program does not interpret the functions, these functions have to be predefined within the source code.

    And... It have to... graph.. in a limit... in X and Y.. for example

    I can only plot up to X = 40, that means that I can plot up to 30 or even 20. The X = 40 is a constant that is defined in the code of the program.

    Please! Someone can help me?

  2. #2
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Create an array large enough to contain the entire graph. Add one extra column to the right, so you can add a '\0', as an end-of-string character:
    Code:
    #include <stdio.h>
    
    #define ROWS  25
    #define COLUMNS 60
    
    int main()
    {
        char graph[ROWS][COLUMNS + 1];
        int r, c;
    
        /* Clear the graph to spaces */
        for (r = 0; r < ROWS; r++)
            for (c = 0; c < COLUMNS; c++)
                graph[r][c] = ' ';
    
        /* Add end-of-string mark at end of each row. */
        for (r = 0; r < ROWS; r++)
            graph[r][COLUMNS] = '\0';
    
        /*
         * Draw to graph[row][col] cells, each one character large,
         * with 0 <= row < ROWS and 0 <= col < COLUMNS.
        */
    
        /* Print each row. puts() adds a newline automatically. */
        for (r = 0; r < rows; r++)
            puts(graph[r]);
    
        /* Done. */
        return 0;
    }
    In C, multi-dimensional arrays are stored in row-major order. It means that graph[r][c] and graph[r][c+1] are consecutive in memory. So, if you have the column as the rightmost index, each row will be consecutive in memory, and adding an extra '\0' at the end, you can treat it as a string. puts() is a standard function that prints that string (the "row string") to standard output, plus adds a newline, so each "row string" will be output on their own line.

    The rest of the program should not be too difficult. Start by considering how would you solve it by hand. You have a gridded paper, with exactly ROWS rows and COLUMNS columns, and you can only put one character within each grid cell. How would you go about it? You need exact rules for each step, not just "I'd find a good starting point, then...".
    Last edited by Nominal Animal; 06-02-2013 at 01:00 PM.

  3. #3
    Registered User
    Join Date
    Jun 2013
    Posts
    11
    Quote Originally Posted by Nominal Animal View Post
    Create an array large enough to contain the entire graph. Add one extra column to the right, so you can add a '\0', as an end-of-string character:
    Code:
    #include <stdio.h>
    
    #define ROWS  25
    #define COLUMNS 60
    
    int main()
    {
        char graph[ROWS][COLUMNS + 1];
        int r, c;
    
        /* Clear the graph to spaces */
        for (r = 0; r < ROWS; r++)
            for (c = 0; c < COLUMNS; c++)
                graph[r][c] = ' ';
    
        /* Add end-of-string mark at end of each row. */
        for (r = 0; r < ROWS; r++)
            graph[r][COLUMNS] = '\0';
    
        /*
         * Draw to graph[row][col] cells, each one character large,
         * with 0 <= row < ROWS and 0 <= col < COLUMNS.
        */
    
        /* Print each row. puts() adds a newline automatically. */
        for (r = 0; r < rows; r++)
            puts(graph[r]);
    
        /* Done. */
        return 0;
    }
    In C, multi-dimensional arrays are stored in row-major order. It means that graph[r][c] and graph[r][c+1] are consecutive in memory. So, if you have the column as the rightmost index, each row will be consecutive in memory, and adding an extra '\0' at the end, you can treat it as a string. puts() is a standard function that prints that string (the "row string") to standard output, plus adds a newline, so each "row string" will be output on their own line.

    The rest of the program should not be too difficult. Start by considering how would you solve it by hand. You have a gridded paper, with exactly ROWS rows and COLUMNS columns, and you can only put one character within each grid cell. How would you go about it? You need exact rules for each step, not just "I'd find a good starting point, then...".
    Thanks for your Help friend! I will do that you said. If i have another question i'll post it here...

    But i have 2 more questions... It's about options in the program... Like this

    For output should be able to define the minimum and maximum values ​​of the axes, and grid intervals.

    It could work with two axes in the Y, for which we define a set can be associated with which of the two axes is working.

    How can i do that?

  4. #4
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Quote Originally Posted by Eduard Perez View Post
    For output should be able to define the minimum and maximum values ​​of the axes, and grid intervals.
    Those are basic scaling issues. Why don't you try it out on a gridded paper, if you don't already know how to do it?

    If you cannot write up rules for a friend to do it on a gridded paper without prior knowledge, you have no hope of writing a computer program to do the same. Computers are utterly stupid; they can only do what you tell them to do, step by step.

    I already showed you the only "trick" you need, the way to use a two-dimensional character array to represent your sheet of gridded paper. The rest is just math, and simple loops.

    If you are at a complete loss on how to start, maybe you should refresh up on basic math, functions and plotting; then read one of the many introductions to C programming freely available on the net? You already have that one logical leap that is hard to find alone; the rest should be straightforward and logical.

    Please remember that this is a discussion forum, and you're supposed to show your own efforts first. Since I showed the trick first, it's your turn to show your efforts next. Without showing your own efforts, you're not going to get much more help from here.

  5. #5
    Registered User
    Join Date
    Jun 2013
    Posts
    11
    Quote Originally Posted by Nominal Animal View Post
    Those are basic scaling issues. Why don't you try it out on a gridded paper, if you don't already know how to do it?

    If you cannot write up rules for a friend to do it on a gridded paper without prior knowledge, you have no hope of writing a computer program to do the same. Computers are utterly stupid; they can only do what you tell them to do, step by step.

    I already showed you the only "trick" you need, the way to use a two-dimensional character array to represent your sheet of gridded paper. The rest is just math, and simple loops.

    If you are at a complete loss on how to start, maybe you should refresh up on basic math, functions and plotting; then read one of the many introductions to C programming freely available on the net? You already have that one logical leap that is hard to find alone; the rest should be straightforward and logical.

    Please remember that this is a discussion forum, and you're supposed to show your own efforts first. Since I showed the trick first, it's your turn to show your efforts next. Without showing your own efforts, you're not going to get much more help from here.
    Thanks for the recommendation, the problem is that this is not the only thing I have to do, if I had much more time to solve this problem, believe me I would put all day, but I also see math, physics, discrete mathematics and other subjects more. It is by the time I could not finish the problem, therefore, is to ask for help urgently.

    I am a novice, not an expert programmer, I wish it were, but I'm a newbie and therefore to be very pressed for time, is to ask for help in this forum. I understand that I should be the first to make some progress, but as I said before, it is time.

    PD: Sorry for my english i'm not very good talking

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I feel your pain, Edward - believe me. But we don't take programming requests. If we did, we'd be swamped with students looking for a better grade, with a lot less work. That wouldn't be fair to those who worked on their own programs. Also, submitting someone else's work as your own, is plagiarism, and likely to get you kicked out of your school.

    Nearly every student has time issues that they need to deal with.

  7. #7
    Registered User
    Join Date
    Jun 2013
    Posts
    11
    Quote Originally Posted by Adak View Post
    I feel your pain, Edward - believe me. But we don't take programming requests. If we did, we'd be swamped with students looking for a better grade, with a lot less work. That wouldn't be fair to those who worked on their own programs. Also, submitting someone else's work as your own, is plagiarism, and likely to get you kicked out of your school.

    Nearly every student has time issues that they need to deal with.
    No, I'm not asking you to do the task, it is only to help me do it, I mean, I want you to advise me how to solve the problem, only that, advice, I am not referring to you do everything completely. I Only ask for help, so that can resolve this issue as quickly as possible.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Usually, the start is the part of the program that we want the student to show us. That way we help them over the problem, and we know exactly what it is, by seeing the code they post up.

    But here's a small start for you:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define MAX_Y 41
    #define MAX_X 41
    
    void printIt(char grid[MAX_Y][MAX_X],int, int, int, int);
    
    int main(void) {
       /*//highX highY would be limits X and Y where they are smaller 
       than MAX_X or MAX_Y, respectively. Not used here.
       */
       int r,c,width=10,height=5,highX=21,highY=21; 
       char grid[MAX_Y][MAX_X]={' '};
       
       //One way to set up the grid with initial values. 
       for(r=0;r<=highY;r++) {
          for(c=0;c<=highX-1;c++) {
             if(r%height==0 && c%width==0) {
                grid[r][c]='+';
             }else if(r%height==0) {
                grid[r][c]='-';
             }else if(c%width==0) {
               grid[r][c]='|';
             }
          }
       }
       
       printIt(grid, width, height, highX, highY); //check that your inital grid is OK
       //add the graph char's here, and call 
       //printIt again, to display final grid.
    
       
       return 0;
    }
    void printIt(char grid[MAX_Y][MAX_X],int width, int height, int highX, int highY) {
       int r, c;
       for(r=0;r<highY;r++) {
          if(r%height==0)
             printf("%2d ",highY-1-r);
          else
             printf("   ");
          for(c=0;c<=highX;c++) {
             putchar(grid[r][c]);
          }
          putchar('\n');
       }
       printf("  ");
       for(c=0;c<=highX;c++) {
          if(c%width==0 && c>0) {
             printf("%2d",c);
             c++;
          }else
             putchar(' ');
       }
    }


    That should help you get started.
    Last edited by Adak; 06-04-2013 at 03:22 AM.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Output is:
    Code:
    20 +---------+---------+ 
       |         |         | 
       |         |         | 
       |         |         | 
       |         |         | 
    15 +---------+---------+ 
       |         |         | 
       |         |         | 
       |         |         | 
       |         |         | 
    10 +---------+---------+ 
       |         |         | 
       |         |         | 
       |         |         | 
       |         |         | 
     5 +---------+---------+ 
       |         |         | 
       |         |         | 
       |         |         | 
       |         |         | 
     0 +---------+---------+ 
                10        20
    The program is a little odd, because you have to remember that the lowest array indices are in the upper left corner. Left to right column indices are fine, however.

    This part of the code is the part that may be unintuitive, working with the array.
    Code:
      for(r=0;r<highY;r++) {
          if(r%height==0)
             printf("%2d ",highY-1-r); //needs high left side gradient numbers, on the low rows of the array
          else                    
             printf("   ");
          for(c=0;c<=highX;c++) {
             putchar(grid[r][c]);
          }
          putchar('\n');
       }
    Last edited by Adak; 06-04-2013 at 07:44 AM.

  10. #10
    Registered User
    Join Date
    Jun 2013
    Posts
    11
    Thank you very much friend, that helps me a lot, however, is not the only thing to do, but that initial part, helps me a lot, i'll post my advances on this topic. Thank you very much for helping me!

    However, i did a code that draws a straight line, as shown in the image, however, I think there will be no problem as the graph above the grid there? I am thinking how to make the parable that appears in the Y-axis

    This is the code... for the line..

    Code:
    void line() {
    int x, y=20;
        while(y >= 0){
            x = -20;              
            while(x <= 20){
                if( x == y )
                    printf("*");
                else if( x != y)
                    printf("-");
                x++;
            }
            printf("\n");
            y--;
        }
    }
    I do not understand, is like, so I only draw the line, to X positions, for example, until X = 30 and how to match the grid you did.
    Last edited by Eduard Perez; 06-05-2013 at 10:17 AM.

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You're welcome.

  12. #12
    Registered User
    Join Date
    Jun 2013
    Posts
    11
    HELP

    Hello again, I've made ​​a breakthrough program I said, now I make the grid, and it is dynamic, in other words, I can enlarge or reduce it, also puts the numbers below and to the side of the grid, it also calculates how each script, each --- has a value, you can go 2 by 2, or 4 by 4, etc., depending on how you take the grid.

    Now what I need is to make the graph above the grid, make a function or something that allows me to make a graph of a parable, or a circle or a Tsine function or cosine, or that I because I do not come to mind how. I did the header file, the. H the. C and the main program. the source code, I just uploaded it to MEGA, so you cand download it and help me please, i did that because placing the code here in the forum, it would be rather extensive.
    It is to see that if I did something the program, anyway, here is my progress.

    Remarkably, i used the NetBeans IDE on Linux for the program.

    LINK : https://mega.co.nz/#!g55xSI7b!T3XDpE...4n98ZUR_v0XE5k

  13. #13
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    One thing you need to do is determine a scaling figure. Assume the graph is 65 characters by 65 characters, so index is 0 to 64, and the range for x and y are 0 to 1000. To scale the data you need to multiply x and y values by 64. / 1000. (float or double precision), to determine the graph index.

    If the graph involves negative numbers, then with graph size of 65 by 65, the effective index range is -32 to +32. Assuming x and y have a range of -1000 to +1000, then you scale by multiplying by 32. / 1000., then add 32 to the result to get the graph index.

    Also, there's no reason you can't make the 2 d character array go in normal order lowest to highest. To print out the graph start at the top row and work your way downwards on the graph.

    If you're looking for "pretty" graphs, try polar equations with the output converted to x,y coordinates. Some examples, r = sin(3 θ), r = a + b θ, r = a e^(b θ).
    Last edited by rcgldr; 06-08-2013 at 01:17 PM.

  14. #14
    Registered User
    Join Date
    Jun 2013
    Posts
    11
    Quote Originally Posted by rcgldr View Post
    One thing you need to do is determine a scaling figure. Assume the graph is 65 characters by 65 characters, so index is 0 to 64, and the range for x and y are 0 to 1000. To scale the data you need to multiply x and y values by 64. / 1000. (float or double precision), to determine the graph index.

    If the graph involves negative numbers, then with graph size of 65 by 65, the effective index range is -32 to +32. Assuming x and y have a range of -1000 to +1000, then you scale by multiplying by 32. / 1000., then add 32 to the result to get the graph index.

    Also, there's no reason you can't make the 2 d character array go in normal order lowest to highest. To print out the graph start at the top row and work your way downwards on the graph.

    If you're looking for "pretty" graphs, try polar equations with the output converted to x,y coordinates. Some examples, r = sin(3 θ), r = a + b θ, r = a e^(b θ).
    Ok, but how i can put that in code?? I can't graph negative because my grid only supports positive, if i wanna make the negative part, it is a lot of work.. I must do that, but i can't because i don't have time.

    Can you download the code and...help me with that? Or put an example here, because i don't understand good what are you saying, because i don't speak english very well. Please put me an example code, for making an polar graph... Because i don't know how to graph polar equations or normal equations in my code.

    Please help this is for tomorrow, and i have time up to 12pm
    PD: Sorry for my english.
    Last edited by Eduard Perez; 06-08-2013 at 04:20 PM.

  15. #15
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by Eduard Perez View Post
    I can't graph negative because my grid only supports positive.
    I tried to explain that before. Assume your grid appears to show that it goes from -32 to +32. Internally the 2d array actually goes from 0 to 64. Again using my example where the range of x and y goes from -1000. to +1000., then the formula to convert to a 2d array index is index = (int) (value * 32. / 1000. + 32.5). (The .5 is to round the number). So if the value is -1000, then using the formula it converts into is (int)(-1000. * 32. / 1000. + 32.5) = (int) (-32. + 32.5) = (int)(0.5) = 0, so the index is 0, which would be the left side of the grid if this was an x value.

    You should also have a check to prevent index from becoming < 0 or > 64.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linux Graphics Engine
    By drdroid in forum Linux Programming
    Replies: 1
    Last Post: 09-28-2003, 06:08 PM
  2. Graphics in Linux
    By ashesh in forum C Programming
    Replies: 0
    Last Post: 12-12-2002, 04:31 AM
  3. Graphics in Linux
    By Xterria in forum Game Programming
    Replies: 3
    Last Post: 08-12-2002, 03:43 PM
  4. Blitting characters in graphics modes
    By SMurf in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 04-05-2002, 12:08 PM
  5. Graphics won't print characters
    By cheesehead in forum C++ Programming
    Replies: 0
    Last Post: 11-12-2001, 11:45 AM