Thread: plot a graph on the screen

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    35

    start printing my graph starting from the left down corner

    --------------------------------------------------------------------------------

    How i can start printing my graph starting from the left down corner
    and not from the top left corner?
    ( (0,0) is oriented in the left down corner)
    Code:
    #include <stdio.h>
    #include <math.h>
    
    void plot_point (double, int); 
    /* Plots a '*' part way across a screen of a given width */
    
    double plotting_function (double);
    /* A function mapping [0,1]->[0,1] to plot*/
    
    int main(int argc, char* argv[])
    {
    	int i;
    	int width_l= 80;	/* Width of the screen */
    	int height_c= 24; /* Height of the screen */
    	double x_pos, y_pos; /* x value and y value on our graph */
    
    	/* First print the "y-axis" of our graph  */
    	for (i= 0; i < width_l-1; i++) {
    		printf ("*");
    	}
    	printf ("\n");
    	/* Now loop round for all our possible x positions */
        for (i= 1; i < height_c; i++) {
    		x_pos= (double)i/(double)height_c;  /* Calculate our x posn */
    		y_pos= plotting_function(x_pos);
    		plot_point(y_pos, width_l);
    	}
    	return 0;  
    }
     
    void plot_point (double y_pos, int screen_width)
    /* Plot the point at "y_pos" */
    {
    	int i;
    	int y_int;   /* The y_position as an integer position on the screen */
    	printf ("*");  /* Print the "x-axis"*/
    	y_int= (int) (y_pos * screen_width);
    
    	/* If we cannot print this point then just return */
    	if (y_int <= 0 || y_int >= screen_width -1) {
    		printf ("\n");
    	    return;  
    	}
    
    	/* Print a * in the correct place */
    	for (i= 1; i < y_int; i++) {
    		printf (" ");
    	}
    	printf ("*\n");
    
    }
    
    
    double plotting_function (double x)
    /* Replace this with any function - but remember that only x and y
    values in the range [0,1] will be plotted */
    {
    	double y;
    
    	y=x*x;  
    	return y; 
    }
    Last edited by dionys; 04-16-2004 at 04:18 AM.

  2. #2
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768

    Lightbulb

    well as I see it... you have two options:

    1. either you turn the real number (float) into an integer, and use gotoxy( ) to place the *.

    2. or, you can can make an app that uses some sort of graphics;
    you can use VGA, or windows api to draw the graph.


    I suggest you go with 1., because it will take you too much time to learn either of the graphics.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    An alternate approach to gotoxy() would be to create a 2D array of characters, initialize all the characters to (space), and then set each point of the graph to '*'. After you've 'drawn' the graph on your array, just print the whole thing.

    Code:
    int i; //used in for loop
     
    //2D array in the form [y][x], for convenience
    char screen[11][17]; //17: 16 points, 1 '\n' (also '\0' in the beginning)
     
    //Initialize the array
    for(i = 0; i < 11; ++i)
    {
    	 strcpy(screen[i], "				");
    	 screen[i][16] = '\n';
    }
    screen[10][16] = '\0';
     
    //Do your graphing; be sure your graph doesn't go farther than (10, 15),
    //and remember that the array is in format [y][x], and finally,
    //remember that the y coordinate is upside down.
     
    //Print the array (interpret it as a huge long string)
    printf("%s", (char*)screen); //I'm used to C++, not sure if this is how you print it...
    Something like that would be a neat solution, I think. And, it's standard (which I don't think gotoxy() is).
    Last edited by Hunter2; 04-15-2004 at 11:22 AM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #4
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    >>> I suggest you go with 1., because it will take you too much time to learn either of the graphic

    Actually, he would still have the same problem. He can only plot pixels using a graphics solution, there may be more pixels then character cells, but ultimately he'll need an integer X,Y coordinate.

    If you are programming in a Windows Console, a "DOS box", you may find my tutorials helpful, there are 6 parts starting here. They have been converted to C++ but in most cases that just means I replaced printf() with cout!
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    35
    thank you all i will start to do all the things you set to me
    i have a long way....i'm a biginner

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error help making no sense
    By tunerfreak in forum C++ Programming
    Replies: 5
    Last Post: 04-17-2007, 07:55 PM
  2. Feedback: Functional Specification Wording
    By Ragsdale85 in forum C++ Programming
    Replies: 0
    Last Post: 01-18-2006, 04:56 PM
  3. char copy
    By variable in forum C Programming
    Replies: 8
    Last Post: 02-06-2005, 10:18 PM
  4. i am not able to figure ot the starting point of this
    By youngashish in forum C++ Programming
    Replies: 7
    Last Post: 10-07-2004, 02:41 AM
  5. get the data and plot the graph
    By lwong in forum Windows Programming
    Replies: 3
    Last Post: 01-01-2004, 09:00 AM