# plot a graph on the screen

• 04-15-2004
dionys
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; }```
• 04-15-2004
Devil Panther
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.
• 04-15-2004
Hunter2
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).
• 04-15-2004