If you have a set of data . . . say, in an array of something much like this structure . . .
Code:
struct plot_point_t {
double xpos, ypos;
};
. . . then, it's really quite simple. Well, relatively simple, anyhow . . .
Assuming that xpos and ypos are both positive, find the maximum of both . . .
Note that this is not completely valid C, just should give you an idea on how to do this kind of thing.
Code:
/* higher above:
size_t plots = 100;
struct plot_point_t *points = malloc(sizeof(struct plot_point_t)*plots)
*/
double xmax = 0.0, ymax = 0.0;
size_t x;
for(x = 0; x < plots; x ++) {
if(points[x].xpos > xmax) xmax = plots[x].xpos;
}
/* . . . repeat above for the y axis . . . */
for(x = 0; x < plots; x ++) {
if(points[x].ypos > ymax) ymax = plots[x].ypos;
}
/* . . . now determine the scale . . . */
/* assuming you've got 80x20 to work with here . . . use env[] to get the COLUMNS environment variable under *nix . . .*/
int xres = xmax/80;
int yres = ymax/20;
int plotpositions[100][2]; /* NOTE: dynamically allocate this using the plots variable. just don't feel like doing a doubly-allocated array here . . . */
for(x = 0; x < plots; x ++) {
plotpositions[x][0] = (int)(points[x].xpos/xres);
plotpositions[x][1] = (int)(points[x].ypos/yres);
}
/* now to paint them . . . */
size_t y;
for(y = 0; y < 20; y ++) {
for(x = 0; x < 20; x ++) {
size_t i;
int printdot = 0;
for(i = 0; i < plots; i ++) {
if(plotpositions[i][0] == x && plotpositions[i][1] == y) printdot = 1;
}
if(printdot) putc('.');
else putc(' ');
}
}
Little lengthy, but it should be easy enough to follow. Note that a triply-nested loop is *not* the best way to do this, but is by far the easiest and simplest to follow.