Plotting Graph in C

C

Chapman

Is it possible to plot the graph as an output of my program in C? It can be
a simple graph as quadratic curves for example
or a correlation between 2 variables only.

Thanks
 
I

Irrwahn Grausewitz

Chapman said:
Is it possible to plot the graph as an output of my program in C? It can be
a simple graph as quadratic curves for example
or a correlation between 2 variables only.
Sure, but not without using extensions to standard C (the one and only
topic in comp.lang.c). Please refer to an OS/implementation specific
newsgroup.

Regards

Irrwahn
--
do not write: void main(...)
do not use gets()
do not cast the return value of malloc()
do not fflush( stdin )
read the c.l.c-faq: http://www.eskimo.com/~scs/C-faq/top.html
 
B

Ben Pfaff

Chapman said:
Is it possible to plot the graph as an output of my program in C? It can be
a simple graph as quadratic curves for example
or a correlation between 2 variables only.

Sure. Just output a text file in PostScript format that plots
the graph.
 
R

Rene Girard

I would like to suggest that instead of having your C program plotting
results of the calculation it performs you
should write the results of your program in a tabular format in a text file
and use Gnuplot which is free, easy
to use and produces high quality plots.
 
G

Gordon Burditt

Is it possible to plot the graph as an output of my program in C? It can be
Sure, but not without using extensions to standard C (the one and only
topic in comp.lang.c).

What extensions to standard C are required to do "ASCII Art" (which
doesn't really have to be done in ASCII)? I claim this qualifies
as "graphics". Commonly, each "pixel" is represented as one character
and is something like a space or a '*'. Fancier programs use several
"grey levels" in a progression of characters that use more ink,
say, space to '-' to '+' to '*'. You can draw horizontal and
vertical lines (say, graph axes), sort of, with characters like '|'
and '_' or '-'. You can even do diagonal lines, sort of, with '/'
and '\\'.

This is really, really crude compared with modern graphics, but it
does work. I've even seen fonts using 8 x 11 inches per character
(mostly on an EBCDIC line printer). Fonts maybe 4 inches high
were used on the front page of line printer output so you could
spot your output from a distance.

There is also nothing particularly OS-specific or that requires C
extensions to generate a file that some other program can use to
display the graph. Granted, the file format is application-specific,
but aren't *ALL* output files application-specific in some way (even
the one from the "hello, world" program)? Take your pick of output
format: PostScript, PNG, GIF, JPEG, PBM, BMP, etc. PostScript is
a text-file format, as are some of the "netpbm" formats. The others
are defined in terms of octets in a binary file.

Gordon L. Burditt
 
I

Irrwahn Grausewitz

What extensions to standard C are required to do "ASCII Art" (which
doesn't really have to be done in ASCII)? I claim this qualifies
as "graphics". Commonly, each "pixel" is represented as one character
and is something like a space or a '*'. Fancier programs use several
"grey levels" in a progression of characters that use more ink,
say, space to '-' to '+' to '*'. You can draw horizontal and
vertical lines (say, graph axes), sort of, with characters like '|'
and '_' or '-'. You can even do diagonal lines, sort of, with '/'
and '\\'.
^
| *
| *
| *
| *
| *
| * Ah, I love those high-definition graphs... :)
| *
| *
| *
+--------------------------------------------------------------->
This is really, really crude compared with modern graphics, but it
does work. I've even seen fonts using 8 x 11 inches per character
(mostly on an EBCDIC line printer). Fonts maybe 4 inches high
were used on the front page of line printer output so you could
spot your output from a distance.

##### ## ## ###### ##### ## ##
## ## ## ## ## ## ## ## ## ##
## ## ## ## ## ## ## ## ####
## ## ####### ##### ## ## ##
## ## ## ## ## ## ## ## ##
## ## ## ## ## ## ## ## ## ###
##### ## ## ###### ##### ## ###
##
and I thought those days were over... ;-) #
There is also nothing particularly OS-specific or that requires C
extensions to generate a file that some other program can use to
display the graph. Granted, the file format is application-specific,
but aren't *ALL* output files application-specific in some way (even
the one from the "hello, world" program)? Take your pick of output
format: PostScript, PNG, GIF, JPEG, PBM, BMP, etc. PostScript is
a text-file format, as are some of the "netpbm" formats. The others
are defined in terms of octets in a binary file.

Gordon L. Burditt

You are of course right.

Irrwahn
 
S

Simon Biber

Chapman said:
Is it possible to plot the graph as an output of my program in C? It can be
a simple graph as quadratic curves for example
or a correlation between 2 variables only.

This program plots three graphs; sin, cos and tan; and can be easily expanded
to plot any other function just by defining and passing the function to
plotfunc().

#include <stdio.h>
#include <math.h>

void moveto(double x, double y)
{
printf("%f %f moveto\n", x, y);
}

void lineto(double x, double y)
{
printf("%f %f lineto\n", x, y);
}

void color(double r, double g, double b)
{
if(r < 0) r = 0;
if(g < 0) g = 0;
if(b < 0) b = 0;
if(r > 1) r = 1;
if(g > 1) g = 1;
if(b > 1) b = 1;
printf("%f %f %f setrgbcolor\n", r, g, b);
}

void plotfunc(double (*func)(double),
double min, double max, double step,
double x1, double x2,
double y1, double y2, double scale)
{
int inbounds = 0;
double i;
for(i = min; i < max; i += step)
{
double x = x1 + (i - min) / (max - min) * (x2 - x1);
double y = (y1 + y2) / 2 + scale * func(i);
if(y < y1 || y > y2)
{
if(inbounds) puts("stroke");
inbounds = 0;
}
else
{
if(inbounds)
{
lineto(x, y);
}
else
{
moveto(x, y);
inbounds = 1;
}
}
}
if(inbounds) puts("stroke");
}

int main(void)
{
/* header */
puts("%!PS-Adobe-3.0");
puts("1 setlinewidth");

/* three plots */
color(1, 0, 0);
plotfunc(sin, -4, 4, 0.01, 0, 400, 0, 400, 50);
color(0, 1, 0);
plotfunc(cos, -4, 4, 0.01, 0, 400, 0, 400, 50);
color(0, 0, 1);
plotfunc(tan, -4, 4, 0.01, 0, 400, 0, 400, 50);

/* axes */
color(0, 0, 0);
moveto(000, 200); lineto(400, 200);
puts("stroke");
moveto(200, 000); lineto(200, 400);
puts("stroke");

/* footer */
puts("showpage");
puts("%%EOF");

return 0;
}
 
J

Jordan

Ben said:
Sure. Just output a text file in PostScript format that plots
the graph.
Where can I read about out putting to a PS file in C to plot a graph? What
is this called?
 
M

Morris Dovey

Chapman said:
Is it possible to plot the graph as an output of my program in
C? It can be a simple graph as quadratic curves for example or
a correlation between 2 variables only.

It is possible; but the interface to a device capable of
rendering an image of the graph will necessarily involve
platform- and/or device-specific code.

An example of a such a program is available for inspection at
http://www.iedu.com/mrd/c/plot_pkg.c - The functions provided
produce a bit-per-pixel file image; and there is a utility that
reads the file and produces the image of the graph on a printer
(your choice of Epson MX-80 or FX-80 :) with 3-way interleaving
to produce a 216 DPI vertical resolution with the normal 240 DPI
horizontal resolution in graphics mode. All things considered,
the image quality wasn't bad.

Warning: The program pre-dates formal C standards and may cause
severe indigestion for some viewers because of its use of
(currently) non-standard library functions.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top