By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,605 Members | 1,391 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,605 IT Pros & Developers. It's quick & easy.

Graphics and Plotting in C++

emaghero
P: 85
Hello All,

Is there anybody out there who can tell me how I can use C++ to plot graphs?

I would like to be able to numerically solve a problem, such as a differential equation, in C++ and plot the results in the output window, rather than having to export the data to another package, such as Mathematica or MatLab.

If anybody can direct me to where this information can be found that would be fantastic.

emaghero

Sapere Aude
Nov 9 '06 #1
Share this Question
Share on Google+
7 Replies


sicarie
Expert Mod 2.5K+
P: 4,677
Hello All,

Is there anybody out there who can tell me how I can use C++ to plot graphs?

I would like to be able to numerically solve a problem, such as a differential equation, in C++ and plot the results in the output window, rather than having to export the data to another package, such as Mathematica or MatLab.

If anybody can direct me to where this information can be found that would be fantastic.

emaghero

Sapere Aude
The only graphing I have done in C/C++ was simple (both text and graphical - as in line graphs), and even that was pretty annoying to get correct. I don't know of anything besides Mathematica or MatLab that will do what they do, unless you could somehow write a C++ program to send the info to mathematica (in whatever format they use?)
Nov 9 '06 #2

Expert 100+
P: 1,510
an easy to use graph plotting package is koolplot see
http://codecutter.org/tools/koolplot/

the simplest thing is to download the Quincy IDE for C/C++ which comes with koolplot built in, see
http://codecutter.org/tools/quincy/index.html

you create a project selecting the koolplot application button - quincy will then link in the correct libraries for you

An alternative is the GNU plotutils package see
http://www.gnu.org/software/plotutils/
but it is sophisticated and difficult to use

Alternativly you can always call C/C++ functions from Matlab using MEX see
http://www.mathworks.com/support/tech-notes/1600/1605.html?BB=1
Matlab calls the C/C++ functions which return the arrays to plot. This gives you C/C++ with the excellent and easy to use graph plotting of Matlab
Nov 9 '06 #3

emaghero
P: 85
Thanks very much I'll check it out.
Nov 10 '06 #4

100+
P: 145
Another relatively easy solution is to use EasyBMP. (I know, I know--I'm biased. ;))

You can run your program, and then spit the output to a BMP file at the end. (Since those are easy enough to view.) Then, you don't have to bother with writing a GUI.

At the end of the program, suppose you have a list of N (x[i], y[i]) values from your calculation. The very easiest way to do this:

sample code:
#include "EasyBMP.h"

....

// find the min and max data values of y:

double max_y = -99999;
double min_y = 99999;
for( int i=0 ; i < N ; i++ )
{
if( y[i] > max_y ){ max_y = y[i]; }
if( y[i] < min_y ){ min_y = y[i]; }
}

// initialize the image

BMP Output;
Output.SetSize( N, N);
RGBApixel PlotColor;
PlotColor.Red = 0;
PlotColor.Green = 0;
PlotColor.Blue = 0;

// plot each point

for( int i=0 ; i < N ; i++ )
{
// map the height to the appropriate pixel.
// step 1: scale the y-value between 0 and 1

double scaled_y = ( y[i] - y_min ) / ( y_max - y_min );

// step 2: map that to the pixel range: 0 to height-1

int pixel_y = (int) ( (Output.TellHeight() - 1)*scaled_y );

// step 3: remember that on an image, the (0,0) pixel is the top
// right corner!

pixel_y = Output.TellHeight()-1 - pixel_y;

// color pixel (i,pixel_y) black

*Output(i,pixel_y) = PlotColor;
}

// write the file output

Output.WriteToFile( "output.bmp" );

// you're done. view the output.bmp in your
// favorite image viewer.

More commentary:
Generally, though, I believe that scientific computation and post processing should be separate. You want your number cruncher to be as quick and efficient as possible. Also, you don't want to have to rerun your program when you desire a different type of plot. When you start tying windows and computation together, you run into the mess where you're dependent upon one compiler or operating system. (This can cause problems when you need to run your simulation many times and would like to use a linux cluster, but somebody decided to use MFC to make plots while running the simulation.)

As such, it's ideal to have your program run once, save data, and exit, and then manipulate the data afterwards. (You don't even have to process that data on the same platform.) I do, however, find it useful to generate quick snapshots while running a simulation, to make it easier to check the status without the overhead of loading up matlab, mathematica, etc.

Lastly, if you'd like an even smoother plot, EasyBMP has addons, including line drawing. So, you could connect data points with lines. That addon package also has a simple font, which you could use to label axes. I use it for my own cancer simulations and visualizations, such as here:

my cancer multimedia page

To make axis labeling easier, I'd recommend doing the steps above to make just the plot, and then pasting it into a larger image where you do the labeling.

-- Paul
Nov 10 '06 #5

P: 3
Plotting Algaebric Curves

Well ive just written a program that plots algaebraic curves of any degree .Please download the code and modify it 4 da better.Im just an amateur programmer in class12 so bear with me for any bugs.

Compiler used: Turbo C++ DOS
Requirements :An ENIAC would suffice

DOWNLOAD THE CODE AND EXE from www.geocities.com/pavanholla
Feb 6 '07 #6

P: 3
The GD library might also contain some useful functions:
http://www.boutell.com/gd
Feb 6 '07 #7

Ganon11
Expert 2.5K+
P: 3,652
You could do it yourself, but the output would be very ugly...I tackled a problem like this for fun recently, but I used the console for output, printing asterisks at each x, y coordinate based on all integer values of x from 1 to (user defined limit). It resulted in a very basic representation of functions, but it was recognizable.
Feb 7 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.