5

I would like to create charts of functions in C++. Can anyone help me in getting started ? Let's suppose I want to draw the function y=x ( it is a straight line passing through the points (0,0) (1,1) (2,2) ..... (20,20) etc. ) How could I do it ? ( If you also suggest how to draw the y & x axes it is even better ) Sorry but it is not on books ( good texts recommendations are welcome, by the way :-) C++ ) and on the net there are references mainly to game graphics. The only serious reference to my problem I was able to find is this: somewhat related question asked in this very forum ( God bless, we have stackoverflow ! )

Community
  • 1
  • 1
alwaystudent
  • 173
  • 1
  • 2
  • 11
  • 2
    If you want draw graphics in you program,you must create a GUI program.And the C++ standard library don't provide any function to draw graphic.But you can use functions provided by you operation system to draw. – Expressway Retrograding Apr 13 '13 at 07:02
  • just wanted to add that you don't *have* to have a GUI to create graphical output - no reason why a command line app can't generate graphical output with libgd or similar. – Paul Dixon Jun 11 '13 at 11:44

9 Answers9

8

I would consider two options:

  • If you want to do everything in C++, you will probably need an external library. OpenGL is a solution, there is also Qt, which is probably easier to learn and larger (and more powerful).
  • If you can afford to output a file and read it with another program, the easiest way is to simply write an ASCII file with the coordinates of the points and plot is with Octave (free).
Dr_Sam
  • 1,818
  • 18
  • 24
4

You could also use Gnuplot, an interactive plotting program: your C++ code handles and computes your data, and then it can export the result to a file or the standard output that can be processed by Gnuplot. For instance, you can do something like this:

my_program | gnuplot

which will call Gnuplot with the result of your program.

Gnuplot is very powerful, you can check the examples available here.

Gnuplot output example
(source: sourceforge.net)

You can find additional information on Gnuplot in C/C++ here:

How to plot graphs in Gnuplot in Real time in C++?

UPDATE

Another possibility is to use R. This language is very famous among statisticians. You may want to look at some examples.

R output example

Related question: How to run plot of R into C/C++?

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
BenC
  • 8,729
  • 3
  • 49
  • 68
  • Thanks for the tip. I wanted to follw the examples in the book but this looks like a wondeful solution for the future. – alwaystudent May 22 '13 at 10:01
  • @ BenC Yes, along with ROOT, it is the best choice, so I decided to begin to explore both and to use which is the best for every task I need. How can I begin ? How can install it ? Any easy tutorials ? – alwaystudent May 22 '13 at 13:53
  • You can check [**this tutorial**](http://physicspmb.ukzn.ac.za/index.php/Gnuplot_tutorial). After that, it really depends on what you are trying to plot. Install is straightforward. If you're using Windows, [**this**](http://www.maths.manchester.ac.uk/~pjohnson/Gnuplot/windows_gnuplot.html) might help you (I only used it on Linux). – BenC May 22 '13 at 14:06
  • @ BenC Ben, do not misunderstand me, but I would kiss you ! :-) – alwaystudent May 22 '13 at 15:56
1

i dont know of a effective fast and simple method to do what you describe in c++. you will either be outputting text to a console to show the effect of the "lines" you wish to display. or setup a proper GUI interface with either directx or opengl. Many librarys do exsist which simplify the task of setting up a GUI. but there is initial overhead.

i would suggest making a c# form application and looking into drawing lines within that. it would also easily allow you to put equations in at run time rather then hard coding them.

Zanven
  • 144
  • 6
1

I use ROOT. It's what most particle physicists use, particularly at CERN and Fermilab. It's free. They have versions for Linux, Mac, and Windows. http://root.cern.ch/drupal/

It's easy to make plots, histograms and produce output suitable for publication.

Edit: It's built on C++ but has python bindings as well.

Edit: If root is install on linux to compile code using ROOT with GCC do e.g:

g++ foo.C -o foo `root-config --cflags --libs` -O3  
  • @ raxman wow ! I have a scientific background, I love these things ! thanks for the info !! – alwaystudent May 22 '13 at 12:32
  • @ raxman it's really great !!! Unfortunately I am not a very advanced programmer. Can you tell me ( or provide a page ) how I can begin to use it ? Installation, etc... – alwaystudent May 22 '13 at 13:43
  • Try googling "ROOT cern tutorials". You could start with this one http://root.cern.ch/root/html/tutorials/hist/hsum.C.html Installation should be easy. I never used ROOT on windows though except with Cygwin. –  May 22 '13 at 18:20
1

I had this same question about three months ago. The best way I found to plot for my application was utilizing the Qt framework (because it already creates a GUI for you, and then leveraging this tool:

Qt plotting widget

This widget, when compiled and linked with your project, allow you to plot 2d graphs directly from your interface. This saves you the trouble of writing to a file, loading with matlab, etc.

Currently, it only handles 2 d plots, but it wouldn't be difficult to extend it to three dimensions or maybe a surface plot.

Best of luck!

Tyler Jandreau
  • 4,245
  • 1
  • 22
  • 47
0

In these days I discovered a couple of things: 1. the book "introduction to C++ programming and graphics" C.Pozrikidis 2. Visual Studio C++ 2010 express and OpenGL ( see the last comment in that page ) Can anyone suggest better solutions ? ( textbooks, free dlls etc. )

Community
  • 1
  • 1
alwaystudent
  • 173
  • 1
  • 2
  • 11
  • 1
    I'd suggest using Qt (in particular, subclassing a QWidget and reimplementing the paintEvent(QPaintEvent*) method. The QPainter class has all the drawing commands you'd want to use, e.g. drawLine()) The advantage of Qt is that it's easier to use than most graphics APIs, and once you've written your program it can be compiled and run on almost any operating system. Qt is available for free under LGPL. – Jeremy Friesner Apr 19 '13 at 14:59
  • by the way, in the above mentioned book the author uses the freeglut library. I visited their site, could anyone teach me how to install it ? Actually there is a page devoted to it but I did not understand anything. Here is their site: http://freeglut.sourceforge.net/ – alwaystudent Apr 19 '13 at 15:56
  • Found ! http://lazyfoo.net/tutorials/OpenGL/01_hello_opengl/windows/msvsnet2010u/index.php – alwaystudent Apr 20 '13 at 08:17
  • VOGLE library ( a simpler graphic library used in the book; Unix ): http://home.comcast.net/~urbanjost/CLONE/LIBS/LIBRARY/libvogle/html/index.html – alwaystudent Apr 22 '13 at 10:54
  • @JeremyFriesner. Thank you a lot. If freeglut does not work as expected, I will give Qt a try. For the time being I prefer to follow the examples in the book. – alwaystudent Apr 23 '13 at 19:11
0

You can use some functions that are there in Windows.h. Start with the console functions. They are a good place to start and enable us to work on the console as if it were a Cartesian plane.

Though I have never tried to draw something with them. I have been able to create nice tables and layouts for text presentation. Like the name and telephone number of a contact as a table in a contacts application.

The functions enable moving the cursor to specific coordinates. And write something there. And overall they quite work like a Cartesian plane. But only the coordinate system is not the normal one which we use(4 quadrants) it is more of a computer type system with only one quadrant and labelled from left o right on the x axis and from up to down on the y axis. You may have to build some functions so as to optimise this or the normal system that we use.

But all in all, it is useful and you can try it.

IcyFlame
  • 5,059
  • 21
  • 50
  • 74
0

I installed gnuplot ( very easy: create a folder in the hard disk, unzip and send a link to the destop from wgnuplot.exe ) and now I export the data to be plotted from Visual Studio ( C++ ) in .txt files and then I call those files from the gnuplot window. Just to save to others the same waste of time and headache I experienced :-)

I have also installed ROOT, very easy, there is a regular Microsoft installer in the cern page. Here is the link: http://root.cern.ch/drupal/content/production-version-534 and scroll to the bottom of the page then select the MSI version compatible with your Visual Studio. I must still learn how to use ROOT from Visual Studio, anyway. Help would be appreciated. There is also this page helping a lot with the creation of projects in Visual Studio using ROOT: http://root.cern.ch/phpBB3/viewtopic.php?f=3&t=11641

All of the above is for WINDOWS users.

Benjamin Bannier
  • 55,163
  • 11
  • 60
  • 80
alwaystudent
  • 173
  • 1
  • 2
  • 11
0
i was just plotting a simmilar function y=x²
in windows, or any library based on windows you will probably intercept message WM_PAINT

     case WM_PAINT:
     HDC hdc = beginpaint();

// change device contexts      hdc y      axys orientation
// change logical coordinates of point (0,0) to half of width and half of //eight of your client area

// u might want to change from pixels to mm
// for each x from -10 to 10 y = x²
// draw a little line for each point in this range

// end painting, this will release     hcd